问题描述
问题(摘要)
给定寄存依赖性X的模块.依赖性x在MVC3应用程序中具有不同的生命周期(每个HTTPREQUEST),然后在控制台应用程序中(每个Lifetimescope具有名称的依赖项).在哪里或如何指定依赖关系的生命周期x?
案例
我已经将所有数据库相关的代码放在一个与模块中的程序集中注册到所有存储库中.现在,模块中也在模块中的issession(nhibernate)注册.
isesess是依赖性x(在给定的问题情况下). Isession在MVC3应用程序(每个请求的生命周期)中有不同的寿命,然后在Console应用程序中,我定义一个命名的LifetImescope.
是否应该在模块之外登记?它是奇怪的,因为它是一种实现细节.
在这里做的最好的情况是什么?设计漏洞或有智能结构:)?
推荐答案
给出了您的用例描述,我会说你有一些选项.
首先,您可以只需要每个应用程序注册自己的依赖项,包括寿命范围.在这方面有一个或两个"复制"代码并不是考虑到应用程序之间的差异以及注册出现得相当小的差异的大量的交易.
第二,您可以将公共部分(减号寿命范围)包装为可以在每个应用程序中使用的ContainerBuilder扩展方法.它仍然是指每个应用程序都有一点"重复代码",但常见的逻辑将被包裹在简单的扩展中.
public static IRegistrationBuilder<TLimit, ScanningActivatorData, DynamicRegistrationStyle> RegisterConnection<TLimit, ScanningActivatorData, DynamicRegistrationStyle>(this ContainerBuilder builder) { // Put the common logic here: builder.Register(...).AsImplementedInterfaces(); }
在每个应用程序中消耗这种扩展会如下所示:
builder.RegisterConnection().InstancePerHttpRequest(); // or builder.RegisterConnection().InstancePerLifetimeScope();最后,如果您知道它是Web或非Web,您可以制作一个处理交换机:的自定义模块
public class ConnectionModule : Autofac.Module { bool _isWeb; public ConnectionModule(bool isWeb) { this._isWeb = isWeb; } protected override void Load(ContainerBuilder builder) { var reg = builder.Register(...).AsImplementedInterfaces(); if(this._isWeb) { reg.InstancePerHttpRequest(); } else { reg.InstancePerLifetimeScope(); } } }
在每个应用程序中,您可以注册模块:
// Web application: builder.RegisterModule(new ConnectionModule(true)); // Non-web application: builder.RegisterModule(new ConnectionModule(false));
或者,您提到您的其他应用程序中的终身范围有一个名称. 您可以让您的模块获取:
public class ConnectionModule : Autofac.Module { object _scopeTag; public ConnectionModule(object scopeTag) { this._scopeTag = scopeTag; } protected override void Load(ContainerBuilder builder) { var reg = builder.Register(...) .AsImplementedInterfaces() .InstancePerMatchingLifetimeScope(this._scopeTag); } }
消费类似:
// Web application (using the standard tag normally provided): builder.RegisterModule(new ConnectionModule("httpRequest")); // Non-web application (using your custom scope name): builder.RegisterModule(new ConnectionModule("yourOtherScopeName"));
我会推荐在Web应用程序中使用InstancePerLifetimeScope,除非您的意图实际上是什么.如在其他答案/注释中所述,InstancePerHttpRequest使用特定的命名寿命范围,以便它安全创造儿童终身范围;使用InstancePerLifetimeScope没有这样的限制,因此每个子女范围而不是一个连接请求,您实际上会得到一个连接.我个人,不要假设其他开发人员不会利用孩子终身范围(这是一个推荐的练习),所以在我的应用程序中,我非常具体.如果您在完全控制您的应用程序中,您可以确保您没有创建其他子范围,或者您实际确实想要每个范围的连接,那么也许InstancePerLifetimeScope将解决您的问题.
其他推荐答案
常常使用每个HTTP请求使用一个连接.存在这种情况,将使用.instanseperlifetimescope()将注册连接.例如,您可能会做一些如下:
builder .Register(c => { var conn = new SqlConnection(GetConnectionString()); conn.Open(); return conn; }) .AsImplementedInterfaces() .InstancePerLifetimeScope();
问题描述
Problem (abstract)
Given a module which registers dependency X. The dependency X has a different lifetime in a MVC3 app (lifetime per HttpRequest) then in a console application (dependency per lifetimescope with a name). Where or how to specify the lifetime of dependency X?
Case
I've put all my database related code in a assembly with a module in it which registers all repositories. Now the ISession (Nhibernate) registration is also in the module.
ISession is dependency X (in the given problem case). ISession has different lifetime in a MVC3 app (lifetime per request) then in a console app where I define a named lifetimescope.
Should the registration of ISession be outside the module? Would be strange since it's an implementation detail.
What is the best case to do here? Design flaw or are there smart constructions for this :) ?
推荐答案
Given your use case description, I'd say you have a few of options.
First, you could just have each application register their own set of dependencies including lifetime scope. Having one or two "duplicate" pieces of code in this respect isn't that big of a deal considering the differences between the application and the fact that the registrations appear fairly small.
Second, you could wrap the common part (minus lifetime scope) into a ContainerBuilder extension method that could be used in each application. It would still mean each app has a little "duplicate code" but the common logic would be wrapped in a simple extension.
public static IRegistrationBuilder<TLimit, ScanningActivatorData, DynamicRegistrationStyle> RegisterConnection<TLimit, ScanningActivatorData, DynamicRegistrationStyle>(this ContainerBuilder builder) { // Put the common logic here: builder.Register(...).AsImplementedInterfaces(); }
Consuming such an extension in each app would look like:
builder.RegisterConnection().InstancePerHttpRequest(); // or builder.RegisterConnection().InstancePerLifetimeScope();
Finally, if you know it's either web or non-web, you could make a custom module that handles the switch:
public class ConnectionModule : Autofac.Module { bool _isWeb; public ConnectionModule(bool isWeb) { this._isWeb = isWeb; } protected override void Load(ContainerBuilder builder) { var reg = builder.Register(...).AsImplementedInterfaces(); if(this._isWeb) { reg.InstancePerHttpRequest(); } else { reg.InstancePerLifetimeScope(); } } }
In each application, you could then register the module:
// Web application: builder.RegisterModule(new ConnectionModule(true)); // Non-web application: builder.RegisterModule(new ConnectionModule(false));
Alternatively, you mentioned your lifetime scope in your other apps has a name. You could make your module take the name:
public class ConnectionModule : Autofac.Module { object _scopeTag; public ConnectionModule(object scopeTag) { this._scopeTag = scopeTag; } protected override void Load(ContainerBuilder builder) { var reg = builder.Register(...) .AsImplementedInterfaces() .InstancePerMatchingLifetimeScope(this._scopeTag); } }
Consumption is similar:
// Web application (using the standard tag normally provided): builder.RegisterModule(new ConnectionModule("httpRequest")); // Non-web application (using your custom scope name): builder.RegisterModule(new ConnectionModule("yourOtherScopeName"));
I would recommend against simply using InstancePerLifetimeScope in a web application unless that's actually what you intend. As noted in other answers/comments, InstancePerHttpRequest uses a specific named lifetime scope so that it's safe to create child lifetime scopes; using InstancePerLifetimeScope doesn't have such a restriction so you'll actually get one connection per child scope rather than one connection for the request. I, personally, don't assume that other developers won't make use of child lifetime scopes (which is a recommended practice), so in my applications I'm very specific. If you're in total control of your application and you can assure that you aren't creating additional child scopes or that you actually do want one connection per scope, then maybe InstancePerLifetimeScope will solve your problem.
其他推荐答案
It's common practice to use a one connection per http request. That being the case, connections would be registered using .InstansePerLifetimeScope(). For example, you might do something like:
builder .Register(c => { var conn = new SqlConnection(GetConnectionString()); conn.Open(); return conn; }) .AsImplementedInterfaces() .InstancePerLifetimeScope();