简体   繁体   English

是否可以在Castle Windsor使用范围生活方式而不通过容器?

[英]Is it possible to use a scoped lifestyle in Castle Windsor without passing the container around?

It's generally accepted that passing an IoC container around your application and using it like a service locator is bad practice. 人们普遍认为,在您的应用程序周围传递IoC容器并像服务定位器一样使用它是不好的做法。

I prefer to use the container only in the composite root of my application and tend to make a single call to Resolve() - resolving the top level object in my application and replying on the container to inject dependencies to classes lower in the object graph. 我更喜欢只在我的应用程序的复合根中使用容器,并倾向于只调用Resolve() - 解析我的应用程序中的顶级对象并回复容器以将依赖关系注入对象图中较低的类。

Castle Windsor has recently added a scoped lifestyle, where you can call container.BeginScope() within a "using" block. Castle Windsor最近添加了一个scoped生活方式,您可以在“using”块中调用container.BeginScope()。 From within this "using" block, resolving a component that was registered with a scoped lifestyle will return the same instance each time, for the duration of the "using" block. 在此“使用”块中,解析使用范围生活方式注册的组件将在“使用”块的持续时间内每次返回相同的实例。

container.Register(Component.For<A>().LifestyleScoped());

using (container.BeginScope())
{
    var a1 = container.Resolve<A>();
    var a2 = container.Resolve<A>();
    Assert.AreSame(a1, a2);
}

Question: Given that BeginScope() is an extension method on the container, I fail to see how a scoped lifestyle could be used in an application unless the container is passed around (which I really don't want to do). 问题:鉴于BeginScope()是容器上的扩展方法,我没有看到如何在应用程序中使用范围生活方式,除非容器被传递(我真的不想这样做)。 Does anyone have any examples of where/how the scoped lifestyle can be used? 有没有人有任何关于范围生活方式可以使用的地方/方式的例子?

Thanks, 谢谢,

Tom 汤姆

I think the use would typically be inside of a factory, which does generally get to see the container. 我认为使用通常是在工厂内部,通常可以看到容器。 Think of a web application: in a sense, each invocation of a controller in an MVC app, or a "page", is running a semi-independent program. 想想一个Web应用程序:从某种意义上说,MVC应用程序或“页面”中每个控制器的调用都在运行一个半独立的程序。 It's not unreasonable to have that "program" resolve its own dependencies. 让“程序”解决自己的依赖关系并不是没有道理的。 That is, each controller invocation should resolve its dependencies using the container. 也就是说,每个控制器调用都应该使用容器来解析其依赖关系。

In the scope of a particular web request, or TCP request, or user session, you may want the container to resolve objects differently. 在特定Web请求,TCP请求或用户会话的范围内,您可能希望容器以不同方式解析对象。 This is a way for you to cleanly do so inside one of your own factories. 这是一种让您在自己的工厂内干净利落的方式。 As with all uses of IoC, you have to be care not to abuse it such that business logic ends up sneaking into your registration code. 与IoC的所有用途一样,您必须小心不要滥用它,以便业务逻辑最终潜入您的注册码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 WCF城堡温莎堡生活方式酒店? - Castle Windsor LifeStyle for WCF? Castle Windsor流利配置:在不使用具体实施的情况下,是否可以为特定服务制定特定的生活方式? - Castle Windsor Fluent Configuration: Is it possible to make a specific lifestyle for a given service without using the concrete implementation? 如何在OWIN中使用Castle Windsor的PerWebRequest生活方式 - How to use Castle Windsor's PerWebRequest lifestyle with OWIN 温莎集装箱生活风格问题 - Windsor Container LifeStyle issue 如何使用城堡温莎在诸如PerWebRequest之类的范围内的生活方式中注册服务? - How to register services in scoped lifestyle behaving like PerWebRequest using castle windsor? 将构造函数参数的一部分传递给城堡windsor容器 - passing part of constructor parameters to castle windsor container 将数据传递到构造函数参数以城堡温莎容器 - passing data to constructor parameters to castle windsor container web api控制器和城堡windsor生活方式 - web api controller and castle windsor lifestyle Castle.Windsor生活方式取决于具体情况? - Castle.Windsor lifestyle depending on context? 温莎城堡的网络服务生活方式 - Castle Windsor lifestyle for web-service
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM