如何使用城堡动态代理将接口聚合到一起

发布于 2024-09-13 22:38:14 字数 1949 浏览 3 评论 0原文

我想在我的代码库中允许声明式混合管理。我想声明一个像这样的接口

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

暗地喜欢 2024-09-20 22:38:14

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文