依赖注入:ASP vNext。这是如何工作? [英] Dependency Injection: ASP vNext. How is this working?

查看:181
本文介绍了依赖注入:ASP vNext。这是如何工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以在我的CQRS基于错误跟踪Web的API,我重构我的代码进展和执行单元测试(其中,无可否认,应该是第一位的)之前;我有这个类和构造:

 公共类BugCommandHandler:IBugCommandHandler 
{
私人BugContext分贝;

公共BugCommandHandler(BugContext bugContext)
{
DB = bugContext;
}

//接口实现
}

在我的控制器,我有这样的:

 公共类BugsController:控制器
{
私人IBugCommandHandler commandHandler ;
私人BugContext分贝;

公共BugsController(BugContext bugContext,IBugCommandHandler bugCommandHandler)
{
DB = bugContext;
commandHandler = bugCommandHandler;
}
}



最后,在我的启动类,我有注射



依赖

  services.AddSingleton< IBugCommandHandler,BugCommandHandler>(); 



我的单元测试和手动集成测试都工作正常,因为他们是我手工调用这个没有时DI。




如何在 BugCommandHandler 实施现在的工作,就好像它一直被称为与数据库上下文在其构造(幕后魔力背后的)?什么是它的'过程实现这一目标?



我已经签出(不喜欢的的),有的在Github上回购的源代码,但不能真正找到在哪里这可能会发生。


我可能会忽视的东西是至关重要的,或者它可能只是良好的隐藏,因为它仍处于预发布。


解决方案

  1. 当你调用 AddSingleton ,该类型注册存储在DI容器。该代码是这里

  2. 当通过调用 AddMvc 添加MVC服务,他们在步骤1中添加相同的DI容器的类型(S) 。神奇这里发生。这是该容器是如何通过在堆栈中向上和组件之间共享

  3. 当MVC激活控制器,将创建使用在容器的种类的实例。这里发生。最终,这段代码被称为。它会尝试解决该服务及其所有使用登记在容器中的依赖关系。



在你的具体情况,还需要 BugContext 中注册。



您可能会发现有用的这篇文章,我在ASP.NET写了前段时间关于DI 5.这是一个有点过时的代码方面,但原理是一样的:的 http://blogs.msdn.com/b/webdev/archive/2014/06/17/dependency-injection-in-asp-net-vnext.aspx



另外,如果你真的想看看会发生什么,看看我写的是调试在ASP.NET 5.框架代码你可以一步一步的其他物品MVC和看到确切的代码路径:
http://blogs.msdn.com/b/webdev/archive/2015/02/06/debugging-asp-net-5-framework-code-using-visual-工作室2015.aspx 。如果你想看到你的方案中的所有代码,你将需要 DependencyInjection 并的MVC


So in my CQRS-based bug-tracking web-API, I am refactoring my code before progressing and implementing unit tests (which, admittedly, should have come first); I have this class and constructor:

public class BugCommandHandler : IBugCommandHandler
{
    private BugContext db;

    public BugCommandHandler(BugContext bugContext)
    {
        db = bugContext;
    }

    //Interface implementation
}

In my controller, I have this:

public class BugsController : Controller
{
    private IBugCommandHandler commandHandler;
    private BugContext db;

    public BugsController(BugContext bugContext, IBugCommandHandler bugCommandHandler)
    {
        db = bugContext;
        commandHandler = bugCommandHandler;
    }
}

And, finally, in my Startup class, I have injected the dependency with

services.AddSingleton<IBugCommandHandler, BugCommandHandler>();

My Unit Tests and manual Integration Tests are all working fine as they were when I was manually calling this without DI.

How does the BugCommandHandler implementation now work as though it has been called with the database context in its constructor (behind the scenes 'magic')? What is its' process to achieve this?

I've checked out (not like that) some of the source code in the Github repo, but can't really find where this may be happening.
I may be overlooking something crucial, or it may just be well-hidden as it is still in pre-release.

解决方案

  1. When you call AddSingleton, the type registration is stored in the DI container. The code is here.
  2. When you add the MVC services by calling AddMvc, they are added to the same DI container as the type(s) at step 1. The magic happens here. That's how the container is passed up the stack and shared between components.
  3. When MVC activates your controller, it will create an instance using the types in the container; that happens here. Eventually, this code is called. It will try to resolve that service and all its dependencies using the registrations in the container.

In your particular case, you also need BugContext to be registered.

You might find useful this article that I wrote a while ago about DI in ASP.NET 5. It is a little outdated in terms of code but the principles are the same: http://blogs.msdn.com/b/webdev/archive/2014/06/17/dependency-injection-in-asp-net-vnext.aspx

Also, if you really want to see what happens, take a look at the other article that I wrote about debugging the framework code in ASP.NET 5. You can step in MVC and see the exact code path: http://blogs.msdn.com/b/webdev/archive/2015/02/06/debugging-asp-net-5-framework-code-using-visual-studio-2015.aspx . If you want to see all the code in your scenario you will need the sources for DependencyInjection and MVC.

这篇关于依赖注入:ASP vNext。这是如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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