为温莎城堡中的组件依赖项添加符号

发布于 2024-12-02 05:06:03 字数 1363 浏览 0 评论 0原文

我有 2 个组件(AWriter 和 BWriter),它们都实现 IWriter 接口。我有 2 个组件(AComponent 和 BComponent),它们都实现接口 IComponent 并且都依赖于 IWriter。但在 AComponent 中我想告诉 Windsor 给我 AWriter 的实例,在 BComponent 中我想告诉 Windsor 给我 BWriter 的实例。怎么做呢?

下面是代码:

public interface IWriter
    {
        void Write();
    }

    public class AWriter : IWriter
    {
        public void Write()
        {
            Console.Write("A writer");
        }
    }

    public class BWriter : IWriter
    {
        public void Write()
        {
            Console.Write("B writer");
        }
    }

    public interface IComponent
    {
        void Do();
    }

    public class AComponent : IComponent
    {
        IWriter writer;

        public AComponent(IWriter writer)
        {
            this.writer = writer;
        }

        public void Do()
        {
            writer.Write();
        }
    }

    public class BComponent : IComponent
    {
        IWriter writer;

        public BComponent(IWriter writer)
        {
            this.writer = writer;
        }

        public void Do()
        {
            writer.Write();
        }
    }

注意: 这只是简化的问题,将 AWriter 放在 AComponent 中,将 BWriter 放在 BComponent 中作为依赖项不是一个选项。此外,AComponent 和 BComponent 在此示例中可能看起来相同,但这不是真实情况。在实际情况下,我有更多的依赖项和组件,我不想为此烦恼。

那么,有没有办法通过某种属性指定 Castle 应该给我哪个实现?

I have 2 components (AWriter and BWriter) where both implement IWriter interface. And I have 2 components (AComponent and BComponent) where both implement interface IComponent and both have dependency on IWriter. But in AComponent I want to tell Windsor to give me instance of AWriter, and in BComponent I want to tell Windsor to give me instance of BWriter. How to do that?

Here is the code:

public interface IWriter
    {
        void Write();
    }

    public class AWriter : IWriter
    {
        public void Write()
        {
            Console.Write("A writer");
        }
    }

    public class BWriter : IWriter
    {
        public void Write()
        {
            Console.Write("B writer");
        }
    }

    public interface IComponent
    {
        void Do();
    }

    public class AComponent : IComponent
    {
        IWriter writer;

        public AComponent(IWriter writer)
        {
            this.writer = writer;
        }

        public void Do()
        {
            writer.Write();
        }
    }

    public class BComponent : IComponent
    {
        IWriter writer;

        public BComponent(IWriter writer)
        {
            this.writer = writer;
        }

        public void Do()
        {
            writer.Write();
        }
    }

NOTE: This is just simplified problem and putting AWriter in AComponent and BWriter in BComponent as dependency is not an option. Also AComponent and BComponent might look same at this example, but this is not real situation. I have much more dependencies and components in real situation and I didn't wanted to bother with that.

So, is there a way to somehow specify by some attribute which implementation should Castle give me?

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

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

发布评论

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

评论(1

执笏见 2024-12-09 05:06:03

您希望使用命名组件和服务覆盖。请参阅 Windsor 文档以获取帮助: http://docs.castleproject .org/Windsor.Registering-components-one-by-one.ashx

使用流畅的注册,您可以执行以下操作:

container.Register(Component.For<IComponent>()
                            .ImplementedBy<AWriter>().Named("AWriter"),
                   Component.For<IComponent>()
                            .ImplementedBy<BWriter>().Named("BWriter"));

然后:

container.Register(Component.For<IComponent>()
                            .ImplementedBy<AComponent>()
                            .ServiceOverrides(
                                ServiceOverride.ForKey("writer").Eq("AWriter"));
container.Register(Component.For<IComponent>()
                            .ImplementedBy<BComponent>()
                            .ServiceOverrides(
                                ServiceOverride.ForKey("writer").Eq("BWriter"));

当然,这将导致 IComponent 的两次注册,但如果再次您需要注入特定的实例,您可以命名它们并使用服务覆盖将每个实现注入到依赖关系结构中的适当位置。

You want to be using named components and service overrides. See here in Windsor's documentation for help: http://docs.castleproject.org/Windsor.Registering-components-one-by-one.ashx

Using fluent registration you'd do something like:

container.Register(Component.For<IComponent>()
                            .ImplementedBy<AWriter>().Named("AWriter"),
                   Component.For<IComponent>()
                            .ImplementedBy<BWriter>().Named("BWriter"));

Then:

container.Register(Component.For<IComponent>()
                            .ImplementedBy<AComponent>()
                            .ServiceOverrides(
                                ServiceOverride.ForKey("writer").Eq("AWriter"));
container.Register(Component.For<IComponent>()
                            .ImplementedBy<BComponent>()
                            .ServiceOverrides(
                                ServiceOverride.ForKey("writer").Eq("BWriter"));

Of course this will result in two registrations for IComponent, but again if you need to inject the specific instances you can name them and use service overrides to inject each implementation into its appropriate spot in your dependency structure.

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