Clone wiki

NRoles / Home



NRoles is now maintained in google code. Please go there for latest versions:

NRoles is an experiment with roles for C#. Roles are high level constructs that promote better code reuse through easier composition. NRoles is a post-compiler that transforms an assembly to enable roles and compositions.


Grab the latest NRoles binaries here, then reference the NRoles.dll assembly and create some roles and compositions:

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 assembly...

var radio = new Radio();

Run the post-compiler (nutate.exe) on the compiled assembly:

C:\some_path\nroles-vX.Y.Z-bin\nutate.exe Devices.dll

You can also run it as a Visual Studio post-build event:

"C:\some_path\nroles-vX.Y.Z-bin\nutate.exe" "$(TargetPath)"

The roles are now composed in the assembly.

When you reference it from another assembly:

// somewhere in some other assembly...

var radio = new Radio();

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. Conflicts in a composition must be resolved explicitly.

You can also check my blog for some examples with NRoles.

Share and Enjoy :-)