如何将接口聚合到城堡动态代理中

时间:2010-08-10 20:14:44

标签: castle-dynamicproxy mixins

我想在我的代码库中允许声明性的mixin管理。我想声明一个像

这样的界面
public interface IMyRepo : IRepository, ICanFindPeopleByName, ICantSing {}

所以我的类只能消耗他们需要的数据访问层的位。在我的IoC容器中,我想将这些接口的实现聚合到一个实例中。但是,当我执行与引用的线程类似的操作时,生成器会抛出一个异常,说明接口是在多个位置实现的。我可以做什么,除了实现我自己的拦截器并通过?

相关主题:


更好的例子(代码墙)

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;

    }
}

抛出

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."

1 个答案:

答案 0 :(得分:2)

更新到动态代理2.2或2.5它更宽松,它会让mixin拥有该接口并忽略它再次作为“附加接口”传递。