Skip to content
Sagi edited this page Apr 3, 2015 · 11 revisions

Let's create a new interface type that will inherit its functionality using a single mixin.
The first thing that we have to do is define an interface and a class that implements the interface.

public interface IDeveloper
{
    void Code();
}

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

Now we need to define a composite type.
IPerson will act as the composite type.
In order for NCop to identify this type as a composite we should annotate the type with TransientCompositeAttribute attribute.

[TransientComposite]
public interface IPerson : IDeveloper
{
}

In order for NCop to match between interface and implementation we need to annotate the composite type with a MixinsAttribute attribute.

[TransientComposite]
[Mixins(typeof(CSharpDeveloperMixin))]
public interface IPerson : IDeveloper
{
}

That's all, we've finished creating the composite type.
Now we need to create a CompositeContainer which will handle two things:

  1. Craft the real implementation for IPerson in runtime.
  2. Act as a Dependency Injection Container that will resolve our type.
using System;
using NCop.Mixins.Framework;
using NCop.Composite.Framework;

class Program
{
    static void Main(string[] args) {
       IPerson person = null;
       var container = new CompositeContainer();

       container.Configure();
       person = container.Resolve<IPerson>();
       person.Code();
    }
}

The expected output should be "C# coding".
Your end result of the code should be similar to this:

using System;
using NCop.Composite.Framework;
using NCop.Mixins.Framework;

namespace NCop.Samples
{
	[TransientComposite]
	[Mixins(typeof(CSharpDeveloperMixin))]
	public interface IPerson : IDeveloper
	{
	}

	public interface IDeveloper
	{
		void Code();
	}

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

	class Program
	{
		static void Main(string[] args) {
			IPerson person = null;
			var container = new CompositeContainer();

			container.Configure();
			person = container.Resolve<IPerson>();
			person.Code();
		}
	}
}