我可以用IoC容器替换app.CreatePerOwinContext吗?

时间:2016-09-14 14:55:02

标签: c# asp.net asp.net-mvc owin

使用用户身份验证的默认ASP.NET MVC5模板正在使用app.CreatePerOwinContext注册服务。喜欢:

app.CreatePerOwinContext<DbContext>(DbContext.Create)
app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create)

然后在很多地方我们只能看到服务定位器模式(或反模式):

UserManager = _userManager ?? HttpContext.GetOwinContext().Get<ApplicationUserManager>();

它使代码不可测试并扩展例如。 ApplicationUserManager注入一些自定义服务当然也需要服务位置。

我是否可以通过使用AutofacNinject等IoC容器完全摆脱此代码?我的意思是我会删除app.CreatePerOwinContext内容,并将其注册到我使用的IoC容器中。然后我通常会通过构造函数注入将它们注入控制器/服务。

app.CreatePerOwinContext替换container.Register然后将我的容器设置为默认依赖性解析器是否可以?

0 个答案:

没有答案
相关问题