-
Notifications
You must be signed in to change notification settings - Fork 7
Single mixin
sagifogel edited this page Mar 25, 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.
public interface IPerson : IDeveloper
{
}
In order for NCop to match between interface and implementation we need to annotate the interface with
a MixinsAttribute attribute.
Please note that we used TransientCompositeAttribute attribute to indicate that IPerson is a composite type.
[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:
- Craft the real implmentation for IPerson in runtime.
- Act as a Dependency Injection Container that will resolve our type.
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();
}
}
}