[pMixins]

A lightweight open source Mixin and AOP framework for C#


Abstract Mixins

Want to Mixin in an abstract class? No problem! [pMixins] can do that. It automatically generates a Requirements interface that forces the Target to implement any abstract members.

So given the following Mixin definition with the abstract Property Name and a method PrintName which uses Name:

public abstract class PrinterMixin
{
   public abstract string Name {
      get;
   }
   public string PrintName()
   {
      return "Hi My Name is " + Name;
   }
}

Let's see the target AbstractMixinExmaple:

[pMixin(Mixin = typeof(PrinterMixin))]
public partial class AbstractMixinExample
{
   string IPrinterMixinRequirements.NameImplementation {
      get {
         return "pMixins";
      }
   }
}

[pMixins] has autogenerated an interface IPrinterMixinRequirements and the Target is responsible for satisfying it.

Best Practice: Requriements intefaces should be implemented explicitly so that it is not exposed to consumers of Target. There is a current Issue exploring if this syntax can be simplified.

And let's see the AbstractMixinExample in action:

[TestFixture]
public class AbstractMixinTest
{
   [Test]
   public void CanCallAbstractProperty()
   {
      Assert.AreEqual("pMixins", new AbstractMixinExample().Name);
   }
   [Test]
   public void CanPrintName()
   {
      Assert.AreEqual("Hi My Name is pMixins", new AbstractMixinExample().PrintName());
   }
}

Let's take a look at each test method in turn:

CanCallAbstractProperty

In this test we can see that the target AbstractMixinExample has the abstract Property Name Mixed in and it operates as expected.

CanPrintName

In this test we can see that the normal method PrintName is Mixed in operates as expected; working with the abstract Name Property.


Download the source for this and all other recipes Here!