为温莎城堡中的组件依赖项添加符号
我有 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您希望使用命名组件和服务覆盖。请参阅 Windsor 文档以获取帮助: http://docs.castleproject .org/Windsor.Registering-components-one-by-one.ashx
使用流畅的注册,您可以执行以下操作:
然后:
当然,这将导致 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:
Then:
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.