OWIN可以在ASP.NET MVC应用程序中替换DI吗? [英] Can OWIN replace DI in a ASP.NET MVC application?

查看:77
本文介绍了OWIN可以在ASP.NET MVC应用程序中替换DI吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大约一年前,在Visual Studio中创建的自动生成的MVC项目不包括OWIN。作为再次申请应用程序的人,想了解这些更改,我想知道OWIN是否可以替换我的DI。

About a year ago, the automatically generated MVC project upon creation in Visual Studio included nothing about OWIN. As someone who is making an application again, trying to understand the changes, I'd like to know if OWIN can replace my DI.

根据我的理解,来自Startup.Auth.cs是集中创建用户管理器(处理身份)以及为应用程序创建数据库连接。

From what I understand, this following bit from Startup.Auth.cs is what centralizes the creation of the user manager (to handle identities), as well as the creation of the database connection for the application.

public partial class Startup
{
    // For more information on configuring authentication, please visit http://go.microsoft.com/fwlink/?LinkId=301864
    public void ConfigureAuth(IAppBuilder app)
    {
        // Configure the db context and user manager to use a single instance per request
        app.CreatePerOwinContext(ApplicationDbContext.Create);
        app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);

        // Other things...
    }
 }

从一个非常有用的来源: http://blogs.msdn.com/b/webdev/archive/2014/02/12/per-request-lifetime-management-for-usermanager -class-in-asp-net-identity.aspx ,看起来我们可以随时使用如下代码访问用户管理器或dbcontext $ /

From a very helpful source: http://blogs.msdn.com/b/webdev/archive/2014/02/12/per-request-lifetime-management-for-usermanager-class-in-asp-net-identity.aspx, it appears as if we can access the user manager or dbcontext at any time with code like the following

public class AccountController : Controller
{
    private ApplicationUserManager _userManager;

    public AccountController() { }

    public AccountController(ApplicationUserManager userManager)
    {
        UserManager = userManager;
    }

    public ApplicationUserManager UserManager {
        get
        {
            // HttpContext.GetOwinContext().Get<ApplicationDbContext>(); // The ApplicationDbContextis retrieved like so
            return _userManager ?? HttpContext.GetOwinContext().GetUserManager<ApplicationUserManager>();                
        }
        private set
        {
            _userManager = value;
        }
    }

    // Other things...
}

如果我正确地了解所有内容,我可以从使用StructureMap转移到OWIN(以处理DI)所做的一切只是像上面的AccountController一样构建我的控制器。有没有什么我失踪或还需要DI在我的应用程序/ OWIN给我DI?

If I understand everything correctly, all that I can do to transfer from using StructureMap to OWIN (to handle the DI) is simply structure my controllers like the AccountController from above. Is there anything I'm missing or do I still need DI in my application/does OWIN give me DI?

推荐答案

我个人不喜欢使用OWIN解决依赖关系的想法。

I personally don't like the idea of using OWIN to resolve dependencies.

默认实现 AccountController.UserManager (as以及其他一些 AccountManager 属性)演示了一个示例:服务定位器作为反模式。所以我宁愿删除所有的东西,并遵循DI原则。 此博文显示了默认项目模板可以被重构以遵循这些原则。

The default implementation of AccountController.UserManager (as well as some other AccountManager properties) demonstrates an example of service locator as an anti-pattern. So I prefer to remove all that stuff and follow DI principles. This blog post shows how the default project template could be refactored to follow these principles.

我希望在下一个版本的ASP.NET中改进依赖注入。实际上承诺支持依赖注入开箱即用。

I hope that dependency injection will be improved in the next versions of ASP.NET. They actually promised support of dependency injection out of the box.

这篇关于OWIN可以在ASP.NET MVC应用程序中替换DI吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆