Mixin aspect

BLToolkit.NET

Business Logic Toolkit for .NET www.bltoolkit.net
 

A mixin is a... well, Wiki describes it much better.

MixinAspect.cs
using System;

using NUnit.Framework;

using BLToolkit.Aspects;
using BLToolkit.Reflection;

namespace HowTo.Aspects
{
    [TestFixture]
    public class MixinAspectTest
    {
        public interface ITestInterface1
        {
            int TestMethod(int value);
        }

        public class TestInterface1Impl : ITestInterface1
        {
            public int TestMethod(int value) { return value; }
        }

        public interface ITestInterface2
        {
            int TestMethod1(int value);
            int TestMethod2(int value);
        }

        public class TestInterface2Impl : ITestInterface2
        {
            public int TestMethod1(int value) { return value; }
            public int TestMethod2(int value) { return value; }
        }

        [Mixin(typeof(ITestInterface1), "_testInterface1")]
        [Mixin(typeof(ITestInterface2), "TestInterface2", "'{0}.{1}' is null.")]
        public abstract class TestClass
        {
            public TestClass()
            {
                _testInterface1 = new TestInterface1Impl();
            }

            protected object          _testInterface1;

            private   ITestInterface2 _testInterface2;
            public    ITestInterface2  TestInterface2
            {
                get { return _testInterface2 ?? (_testInterface2 = new TestInterface2Impl()); }
            }

            [MixinOverride(typeof(ITestInterface2))]
            protected int TestMethod1(int value) { return 15; }
        }

        [Test]
        public void Test()
        {
            TestClass       tc = TypeAccessor<TestClass>.CreateInstance();
            ITestInterface1 i1 = (ITestInterface1)tc;
            ITestInterface2 i2 = (ITestInterface2)tc;

            Assert.AreEqual(10, i1.TestMethod (10));
            Assert.AreEqual(15, i2.TestMethod1(20));
            Assert.AreEqual(30, i2.TestMethod2(30));
        }
    }
}
BLToolkit type builder will generate the following for the class above:
[BLToolkitGenerated]
public sealed class TestClass : MixinAspectTest.TestClass, MixinAspectTest.ITestInterface1, MixinAspectTest.ITestInterface2
{
    int MixinAspectTest.ITestInterface1.TestMethod(int value)
    {
        if (base._testInterface1 == null)
            throw new InvalidOperationException("'ITestInterface1._testInterface1' is not initialized.");

        return base._testInterface1.TestMethod(value);
    }

    int MixinAspectTest.ITestInterface2.TestMethod1(int value)
    {
        // The MixinOverride attribute enforces direct method call.
        //
        return base.TestMethod1(value);
    }

    int MixinAspectTest.ITestInterface2.TestMethod2(int value)
    {
        if (base.TestInterface2 == null)
            throw new InvalidOperationException("'ITestInterface2.TestInterface2' is null.");

        return base.TestInterface2.TestMethod2(value);
    }
}
 
© 2010 www.bltoolkit.net
[email protected]