Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/sagifogel/ncop

Composite-aspect oriented framework for .NET
https://github.com/sagifogel/ncop

aop aspect aspect-oriented-framework aspect-oriented-programming composite csharp mixins

Last synced: about 1 month ago
JSON representation

Composite-aspect oriented framework for .NET

Awesome Lists containing this project

README

        

NCop
===================
What is **NCop**?

**NCop** is a framework that implements Composite/Aspect Oriented Programming that encapsulates the concepts of [Mixins](https://github.com/sagifogel/NCop/blob/master/README.md#mixins), [Aspects](https://github.com/sagifogel/NCop/blob/master/README.md#aspects) and [Dependency Injection](https://github.com/sagifogel/NCop/blob/master/README.md#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](https://github.com/sagifogel/NCop/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.

```csharp
public interface IDeveloper
{
void Code();
}
```

```csharp
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`.

```csharp
[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.

```csharp
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.

```csharp
[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.

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

container.Configure();
developer = container.Resolve();
developer.Code();
}
```

The expected output should be:
```
"C# coding"
"Elapsed Ticks: [Number of ticks]"
```