Search code examples
castle-dynamicproxymixins

How can I aggregate interfaces into with castle dynamic proxy


I would like to allow for declarative mixin management in my codebase. I would like to declare an interface like

public interface IMyRepo : IRepository, ICanFindPeopleByName, ICantSing {}

So my classes can consume only the bits of the data access layer they need. In my IoC container I would like to aggregate the implementations of these interfaces into a single instance. However when I do things similar to the referenced threads, the generator throws an exception stating that interfaces are implemented in multiple places. What can I do, other than implementing my own interceptor and passing through?

Relevant Threads:


Better Example (wall of code)

public interface IIceCream {
    void Eat();
}
public class IceCream : IIceCream {
    public void Eat() { Console.WriteLine("Yummy!"); }
}
public interface ICake {
    void NomNom();
}
public class Cake : ICake {
    public void NomNom() { Console.WriteLine("Cakey!"); }
}
public interface ISprinkles {
    void Oogle();
}
public class Sprinkles : ISprinkles {
    public void Oogle(){ Console.WriteLine("Its Pretty!");}
}

public interface IIceCreamWithCakeAndSprinkles : IIceCream, ICake, ISprinkles {}

public class Program
{
    public static void Main()
    {
        var gen = new ProxyGenerator();
        var options = new ProxyGenerationOptions();

        options.AddMixinInstance(new IceCream());
        options.AddMixinInstance(new Cake());
        options.AddMixinInstance(new Sprinkles());

        var result =
            gen.CreateClassProxy(typeof (object), new[] {typeof (IIceCreamWithCakeAndSprinkles)}, options) as IIceCreamWithCakeAndSprinkles;

    }
}

throws

InvalidMixinConfigurationException: "The mixin IceCream adds the interface 'ConsoleApplication1.IIceCream' to the generated proxy, but the interface already exists in the proxy's additional interfaces. A mixin cannot add an interface already implemented in another way."

Solution

  • Update to Dynamic Proxy 2.2 or 2.5 It is more permissive and it will let the mixin own the interface and ignore that it was passed again as "additional interface".