根据决心定制城堡生活方式
对于 Castle Windsor,假设我有以下类:
public class LowLevelComponent
{
}
public class HighLevelComponent
{
readonly LowLevelComponent LowLevelComponent;
public HighLevelComponent(LowLevelComponent lowLevelComponent)
{
LowLevelComponent = lowLevelComponent;
}
}
public class ComponentBeingResolved
{
readonly LowLevelComponent LowLevelComponent;
readonly HighLevelComponent HighLevelComponent;
public ComponentBeingResolved(LowLevelComponent lowLevelComponent,
HighLevelComponent highLevelComponent)
{
LowLevelComponent = lowLevelComponent;
HighLevelComponent = highLevelComponent;
}
}
以最简单的方式注册:
container.Register(Component.For<LowLevelComponent>());
container.Register(Component.For<HighLevelComponent>());
container.Register(Component.For<ComponentBeingResolved>());
每次我都希望在所有依赖项中使用相同的 LowLevelComponent
实例调用Resolve。
因此,在这些调用之后:
var instance1 = container.Resolve<ComponentBeingResolved>();
var instance2 = container.Resolve<ComponentBeingResolved>();
以下断言应该是正确的:
instance1.LowLevelComponent == instance1.HighLevelComponent.LowLevelComponent
instance1.LowLevelComponent != instance2.LowLevelComponent
instance1.HighLevelComponent != instance2.HighLevelComponent
我还将“你做的一切错了,这就是你应该做的”作为回答 :-)
With Castle Windsor, let's say I have the following classes:
public class LowLevelComponent
{
}
public class HighLevelComponent
{
readonly LowLevelComponent LowLevelComponent;
public HighLevelComponent(LowLevelComponent lowLevelComponent)
{
LowLevelComponent = lowLevelComponent;
}
}
public class ComponentBeingResolved
{
readonly LowLevelComponent LowLevelComponent;
readonly HighLevelComponent HighLevelComponent;
public ComponentBeingResolved(LowLevelComponent lowLevelComponent,
HighLevelComponent highLevelComponent)
{
LowLevelComponent = lowLevelComponent;
HighLevelComponent = highLevelComponent;
}
}
Registered in the easiest possible way:
container.Register(Component.For<LowLevelComponent>());
container.Register(Component.For<HighLevelComponent>());
container.Register(Component.For<ComponentBeingResolved>());
I'd like to get the same instance of LowLevelComponent
used in all dependencies each time I call Resolve.
So, after these calls:
var instance1 = container.Resolve<ComponentBeingResolved>();
var instance2 = container.Resolve<ComponentBeingResolved>();
The following assertions should be true:
instance1.LowLevelComponent == instance1.HighLevelComponent.LowLevelComponent
instance1.LowLevelComponent != instance2.LowLevelComponent
instance1.HighLevelComponent != instance2.HighLevelComponent
I'll also take "you're doing everything wrong, this is what you should do instead" as an answer :-)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您一定听说过人们建议只为您的应用入口点(Main、Controller 等)调用 Resolve 一次。 Castle 文档对此有很好的指南。
You must've heard people recommending to only call Resolve once for your app entry point (Main, Controller, etc.). Castle documentation has good guidelines on this.
根据 Mauricio 的链接,我使用工厂让它工作:
用法:
不确定这是一个好的黑客还是一个丑陋的黑客,但它工作得非常好。
Based on Mauricio's link, I got it working using factories:
Usage:
Not sure if this is a good hack or an ugly one, but it works wonderfully.