本文介绍了可替换OWIN DI在ASP.NET MVC应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 大约一年前,在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,它看起来好像我们可以用code一样随时访问用户的经理或的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 followingpublic 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 (以及一些其他的的AccountManager 属性)演示了一个例子默认实现服务定位为一个反模式。所以我preFER删除所有的东西,并按照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 DI在ASP.NET MVC应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-21 02:51