我怎样才能在温莎城堡实现这一目标? (从 StructureMap 迁移)
我需要修改现有的 Web 应用程序以使用 Castle.Windsor 作为 IOC 容器。它最初是用 StructureMap 开发的。
我遇到了以下问题。
假设我已经注册了几个接口及其相应的实现:
IFoo -> Foo
IBar -> Bar
调用 container.Resolve
或 container.Resolve
效果很好。这意味着服务已正确注册。
我有一个依赖于其他服务的 Web Api 类,例如 IFoo
public class BadRequestErrorHandler : HttpErrorHandler
{
// services
public BadRequestErrorHandler(IFoo foo) {...} // has dependency on IFoo
}
在 StructureMap 中我可以调用:
var test = ObjectFactory.GetInstance<BadRequestErrorHandler>();
这将解决 IFoo 依赖关系。
现在这不适用于温莎。
温莎如何实现这一目标?
谢谢!
* 编辑* 我只能通过显式注册 BadRequestErrorHandler 来使其工作。
container.Register(Component.For<BadRequestErrorHandler>());
我只是希望有一种更好的方法来实现这一点,而不涉及注册具有依赖项的类。我有很多...
* 编辑 2 **
为了减轻痛苦,我添加了一种特殊的方法来处理这些具体类型。
public T GetInstanceWithAutoRegister<T>()
{
if (container.Kernel.GetHandler(typeof(T)) == null)
{
container.Register(Component.For<T>());
}
return container.Resolve<T>();
}
public object GetInstanceWithAutoRegister(Type pluginType)
{
if (container.Kernel.GetHandler(pluginType) == null)
{
container.Register(Component.For(pluginType));
}
return container.Resolve(pluginType);
}
并不理想,但至少比必须显式注册每种类型要好。希望有人有更好的解决方案
I need to modify an existing web app to use Castle.Windsor as IOC container. It was originally developed with StructureMap.
I am stuck with the following problem.
Lets say I have registered a couple of interfaces and their corresponding implementations:
IFoo -> Foo
IBar -> Bar
Calling container.Resolve<IFoo>()
or container.Resolve<IBar>()
works just fine. This means that the services are registered correctly.
I have a Web Api class with dependencies on other services, such as IFoo
public class BadRequestErrorHandler : HttpErrorHandler
{
// services
public BadRequestErrorHandler(IFoo foo) {...} // has dependency on IFoo
}
In StructureMap I can call:
var test = ObjectFactory.GetInstance<BadRequestErrorHandler>();
this will resolve the IFoo dependency.
Now this does not work with windsor.
How can this be achieved with windsor?
Thanks!
* EDIT *
I was just able to make it work by explicitely registering the BadRequestErrorHandler.
container.Register(Component.For<BadRequestErrorHandler>());
I am just hoping there is a better way to achieve this, that does not involve registering classes that have dependencies. I have a bunch of them...
* EDIT 2 **
To ease the pain, I added a special method to deal with these concrete types.
public T GetInstanceWithAutoRegister<T>()
{
if (container.Kernel.GetHandler(typeof(T)) == null)
{
container.Register(Component.For<T>());
}
return container.Resolve<T>();
}
public object GetInstanceWithAutoRegister(Type pluginType)
{
if (container.Kernel.GetHandler(pluginType) == null)
{
container.Register(Component.For(pluginType));
}
return container.Resolve(pluginType);
}
not ideal, but at least better than having to explicetly register each type. Hope someone has a better solution
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以通过注册一个
ILazyComponentLoader
来实现您想要的目的,这是一个钩子,当组件无法解析时,Windsor 会调用该钩子作为“最后的手段”。在您的情况下,实现可能看起来有点像这样:
-然后它应该这样注册:
您可以阅读有关它的更多信息 在文档中。
You can achieve what you want by registering an
ILazyComponentLoader
which is a hook that gets called by Windsor as a "last resort" when a component cannot be resolved.In your case, the implementation would probably look somewhat like this:
-and then it should be registered as such:
You can read more about it in the docs.