-
Notifications
You must be signed in to change notification settings - Fork 7
Mixins
A mixin is an interface with implemented methods.
Mixins exists in .NET in the form of object oriented interfaces implementation.
In object-oriented programming languages, a mixin is a class which contains a combination of methods from other classes. How such a combination is done depends on the language, but it is not by inheritance. If a combination contains all methods of combined classes it is equivalent to multiple inheritance.
NCop allows the notion of multiple inheritance as opposed to .NET object oriented.
Mixins fits well with the concept of composition over inheritance.
#####Single mixin example
Let's create a new interface type that will inherit its functionality using a 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#");
}
}
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.
[Mixins(typeof(CSharpDeveloperMixin))]
public interface IPerson : IDeveloper
{
void Code();
}
#####Mutliple mixins example
Now Let's create a new interface type that will inherit its functionality using multiple mixins.
Composite Oriented Programming strive for decoupling and separation of concerns which does not addressed by Object Oriented.
As a person we have multiple roles/tasks in life.
For example a person could be a developer and a musician.
Let's define these roles as types.
public interface IDeveloper
{
void Code();
}
public interface IMusician
{
void Play();
}
public interface IPerson : IDeveloper, IMusician
{
}
Now we need to define concrete types that will implement each interface.
IPerson will serve the role of the composite.
We need to define concrete types only for IDeveloper and IMusician NCop will do the rest and craft the
IPerson composite.
Let's define the concrete types.
public class CSharpDeveloperMixin : IDeveloper
{
public void Code() {
Console.WriteLine("C#");
}
}
public class GuitarPlayerMixin : IMusician
{
public void Play() {
Console.WriteLine("I am playing C# accord with Fender Telecaster");
}
}
Again we need to order NCop to match between interfaces and implementations using MixinsAttribute attribute.
[Mixins(typeof(CSharpDeveloperMixin), typeof(GuitarPlayerMixin))]
public interface IPerson : IDeveloper, IMusician
{
}