简体   繁体   English

OWIN可以替换ASP.NET MVC应用程序中的DI吗?

[英]Can OWIN replace DI in a ASP.NET MVC application?

About a year ago, the automatically generated MVC project upon creation in Visual Studio included nothing about OWIN. 大约一年前,在Visual Studio中创建时自动生成的MVC项目不包含任何关于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. 作为再次申请的人,试图了解变化,我想知道OWIN是否可以取代我的DI。

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. 根据我的理解,Startup.Auth.cs中的以下内容集中了用户管理器的创建(处理身份),以及为应用程序创建数据库连接。

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...
    }
 }

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

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...
}

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. 如果我理解了所有内容,我可以做的就是从使用StructureMap转移到OWIN(处理DI)只是构造我的控制器,如上面的AccountController。 Is there anything I'm missing or do I still need DI in my application/does OWIN give me DI? 有什么我缺少或做的我仍然需要DI在我的应用程序/ OWIN给我DI吗?

I personally don't like the idea of using OWIN to resolve dependencies. 我个人不喜欢使用OWIN解决依赖关系的想法。

The default implementation of AccountController.UserManager (as well as some other AccountManager properties) demonstrates an example of service locator as an anti-pattern . AccountController.UserManager的默认实现(以及一些其他AccountManager属性)演示了服务定位器作为反模式的示例。 So I prefer to remove all that stuff and follow DI principles. 所以我更喜欢删除所有内容并遵循DI原则。 This blog post shows how the default project template could be refactored to follow these principles. 此博客文章显示了如何重构默认项目模板以遵循这些原则。

I hope that dependency injection will be improved in the next versions of ASP.NET. 我希望在下一版本的ASP.NET中改进依赖注入。 They actually promised support of dependency injection out of the box. 他们实际上承诺支持开箱即用的依赖注入。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM