Skip to content

sagifogel/NCop

Repository files navigation

NCop

What is NCop?

NCop is a framework that implements Composite/Aspect Oriented Programming that encapsulates the concepts of Mixins, Aspects and Dependency Injection, using the standard .NET.
The main purpose of composites is separation of concerns. You achieve it by defining different roles within different entities and combine all of them using NCop (much like multiple inheritance).

Installing

Install-Package NCop

Please visit the Wiki page for explanations and demos.

Mixins

A mixin is an interface with implemented methods.
Mixins exists in .NET in the form of object oriented interfaces implementation.

public interface IDeveloper
{
    void Code();
}
public class CSharpDeveloperMixin : IDeveloper
{
    public void Code() {
        Console.WriteLine("C# coding");
    }
}

Composites

Composites are the artifacts of NCop processing. A composite is a combination of Mixins and Aspects.
In order to create a composite type you need to annotate one of the interfaces with a CompositeAttribute
and also to tell NCop to match between interface and implementation by annotating the composite type with a MixinsAttribute.

[TransientComposite]
[Mixins(typeof(CSharpDeveloperMixin))]
public interface IDeveloper
{
    void Code();
}

Aspects

Aspects aims to increase modularity by allowing the separation of cross-cutting concerns.
In order to create an aspect you should first create an aspect class that will hold your desired join points.

public class StopwatchActionInterceptionAspect : ActionInterceptionAspect
{
    private readonly Stopwatch stopwatch = null;

    public StopwatchActionInterceptionAspect() {
        stopwatch = new Stopwatch();
    }

    public override void OnInvoke(ActionInterceptionArgs args) {
        stopwatch.Restart();
        args.Proceed();
        stopwatch.Stop();
        Console.WriteLine("Elapsed Ticks: {0}", stopwatch.ElapsedTicks);
    }
}

The second thing that you will need to do is to annotate the method which you want to apply the aspect on.

[TransientComposite]
[Mixins(typeof(CSharpDeveloperMixin))]
public interface IDeveloper
{
    [MethodInterceptionAspect(typeof(StopwatchActionInterceptionAspect))]
    void Code();
}

Dependency Injection

NCop IoC is based on Funq - which was adopted because of its excellent performance and memory characteristics.
In order to resolve an artifact of NCop you need to create a composite IoC Container,
and call the Resolve function.

static void Main(string[] args) {
    IDeveloper developer = null;
    var container = new CompositeContainer();

    container.Configure();
    developer = container.Resolve<IDeveloper>();
    developer.Code();
}

The expected output should be:

"C# coding"
"Elapsed Ticks: [Number of ticks]"