[pMixins]

A lightweight open source Mixin and AOP framework for C#


Hello World

Welcome to [pMixins]!

[pMixins] allows code reuse through composition by mixing in members from a Mixin into a Target.

So let's create a class that contain methods that we want to reuse in multiple places:

public class HelloWorldMixin
{
   public string GetHelloWorld()
   {
      return "Hello World";
   }
}

The HelloWorldMixin contains the method GetHelloWorld. So let's mix it in to a Target using [pMixins].

[pMixin(Mixin = typeof(HelloWorldMixin))]
public partial class Target
{
}

That's it. Now as soon as you press save in Visual Studio [pMixins] will run behind the scenes and add the GetHelloWorld method to the code behind file. Which means you can immediately use it and even see it in IntelliSense:

Going Further

Lets expand upon the Hello World exmaple, by defining a new Mixin with a new method, AnswerToTheUniverse:

public class AnswerToTheUniverseMixin
{
   public int AnswerToTheUniverse()
   {
      return 42;
   }
}

And let's mixin both the AnswerToTheUniverse and HelloWorldMixin:

[pMixin(Mixin = typeof(AnswerToTheUniverseMixin))]
[pMixin(Mixin = typeof(HelloWorldMixin))]
public partial class BasicMixinExample
{
}

So now BasicMixinExample will have both the AnswerToTheUniverse and GetHelloWorld methods.

So let's test it with the BasicMixinTest class.

[TestFixture]
public class BasicMixinTest
{
   [Test]
   public void CanCallHelloWorld()
   {
      Assert.AreEqual(new BasicMixinExample().GetHelloWorld(), "Hello World");
   }
   [Test]
   public void CanGetAnswerToTheUniverse()
   {
      Assert.AreEqual(new BasicMixinExample().AnswerToTheUniverse(), 42);
   }
   [Test]
   public void CanDoImplicitConversion()
   {
      HelloWorldMixin helloWorld = new BasicMixinExample();
      Assert.AreEqual(helloWorld.GetHelloWorld(), "Hello World");
   }
   [Test]
   public void CanDoExplicitConversion()
   {
      var answerToTheUniverse = (AnswerToTheUniverseMixin)new BasicMixinExample();
      Assert.AreEqual(answerToTheUniverse.AnswerToTheUniverse(), 42);
   }
}

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

CanCallHelloWorld

This shows that BasicMixinExample both has a method, GetHelloWorld, and that calling it returns the result defined in HelloWorldMixin ("Hello World").

CanGetAnswerToTheUniverse

This shows that BasicMixinExample both has a method, AnswerToTheUniverse, and that calling it returns the result defined in AnswerToTheUniverseMixin (42).

CanDoImplicitConversion

This shows that BasicMixinExample can be implicitly converted to the HelloWorldMixin. This means that BasicMixinExample could be passed to any method or property expecting a HelloWorldMixin without an explicit cast.

CanDoExplicitConversion

This shows that BasicMixinExample can be explicitly converted to the AnswerToTheUniverseMixin. This means that BasicMixinExample can be cast to a AnswerToTheUniverseMixin, even if it's only considered an object at the current scope.


Download the source for this and all other recipes Here!