NRoles is an experiment with roles in C#. Roles, very similar to traits, are high level constructs that promote better code reuse through easier composition. NRoles is a post-compiler created with Mono.Cecil that transforms an assembly to enable roles and compositions.
NRoles is available as a NuGet package. Once installed in a project, it will add a custom MSBuild post-compilation step that will transform the target assembly to enable roles and compositions.
Roles are classes marked with the interface NRoles.Role
, and compositions with the interface NRoles.Does<TRole>
, with the generic parameter TRole
set to a role:
using NRoles;
namespace Devices {
public class RSwitchable : Role {
private bool on = false;
public void TurnOn() { on = true; }
public void TurnOff() { on = false; }
public bool IsOn { get { return on; } }
public bool IsOff { get { return !on; } }
}
public class RTunable : Role {
public int Channel { get; private set; }
public void Seek(int step) { Channel += step; }
}
public class Radio : Does<RSwitchable>, Does<RTunable> { }
}
// somewhere in the same project...
var radio = new Radio();
radio.As<RSwitchable>().TurnOn();
radio.As<RTunable>().Seek(42);
Note that there are also other ways to use compositions in the same project they're defined.
When you reference it from another project:
// somewhere in some other project...
var radio = new Radio();
radio.TurnOn();
radio.Seek(42);
Roles cannot be instantiated, cannot inherit from any classes (only Object
) and cannot have parameterized constructors. They can implement interfaces, and these implementations will be carried over to the classes that compose them. They can also compose other roles and declare self-types. Conflicts in a composition must be resolved explicitly.
More examples with NRoles can be found here and here. Some backstory for its inception can be found here and here.
NRoles is an experiment, it can perform extensive changes to target assemblies, and might not work well with other tools and processes, most notably debugging.
Licensed under the MIT license.