依赖注入,组合根和入口点 [英] Dependency injection, composition root, and entry points

查看:76
本文介绍了依赖注入,组合根和入口点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经花了很多时间阅读这些文章(以及许多其他文章):





,而我仍在努力围绕DI以及IoC容器的连接依赖项和自动接线功能的概念。



我想我了解依赖注入和控制反转的理论,并且从2016年开始实施此处显示的示例(我更新了代码以使用 PSR-11 ,并不需要 container-interop 软件包):




  • > https://www.sitepoint.com/how-to-build-your-own-dependency-injection-container/



  • 容器示例的应用程序显示在GitHub链接上: https://github.com/sitepoint-editors/Container



    请注意,尽管此示例使用PHP,但我试图独立于语言来理解DI的详细信息,因此欢迎使用任何语言。



    有人可以解释手动连接依赖项与使用容器的自动连接功能之间的区别吗? SitePoint文章简要提到,更高级的容器添加了自动接线功能,这意味着示例中尚未包含此功能。有人可以解释GitHub页面上显示的应用程序,以及它与核心DI和IoC概念(例如合成根)之间的关系。

    解决方案


    有人可以解释手动连接依赖项与使用容器的自动连接功能之间的区别吗?


    纯DI是在不使用DI容器的情况下应用DI的实践。这意味着您可以使用编程语言的 new 结构来更新对象,从而构建对象图。例如,参见C#中的此示例(来自列出Mark的书依赖注入原理,实践和模式的12.2

     新HomeController(
    新ProductService(
    新SqlProductRepository(
    新CommerceContext( connectionString)),
    新的AspNetUserContextAdapter()));

    根据该书,自动装配为:


    利用编译器和[运行时环境]提供的类型信息,根据抽象和具体类型之间的映射自动组成对象图的功能。 (请参见 12.1。 2


    换句话说,使用DI容器,您将能够仅仅告诉容器有关



    考虑前面的示例,清单12.3显示了您的类型,并确定了该类型具有哪些依赖关系,并将能够连接该类型及其依赖关系。您只需要指定容器中抽象和具体类型之间的映射:

      var容器=新的AutoWireContainer() ; 

    container.Register(typeof(IUserContext),typeof(AspNetUserContextAdapter));
    container.Register(typeof(IProductRepository),typeof(SqlProductRepository));
    container.Register(typeof(IProductService),typeof(ProductService));
    container.Register(typeof(CommerceContext),()=> new CommerceContext(connectionString));

    当您要求 HomeController 时,


    SitePoint文章简要地提到,更高级的容器增加了自动接线功能


    对我来说,自动装配是使图书馆成为DI容器的原因。如果某种东西至少不支持自动装配,就不能称为DI容器。


    I've spent a lot of time reading these articles (along with many others):

    and I'm still trying to wrap my head around DI and the concept of "wiring up the dependencies" and the "auto wiring" functionality of an IoC container.

    I think I understand the theory of Dependency Injection and Inversion of Control and I've implemented the example shown here from 2016 (I updated the code to use PSR-11 and eliminate the need for the container-interop package):

    The application of the container example is shown at the GitHub link: https://github.com/sitepoint-editors/Container .

    Note that while this example uses PHP, I'm trying to understand the details of DI independently from language, so any language is welcome.

    Can someone explain the difference between manually wiring up dependencies, and using a container's auto wiring functionality? The SitePoint article briefly mentions that more advanced containers add the automatic wiring functionality, implying that the example doesn't contain this function already. Can someone explain the application shown on the GitHub page and how that relates to core DI and IoC concepts, like the Composition Root.

    解决方案

    Can someone explain the difference between manually wiring up dependencies, and using a container's auto wiring functionality?

    Pure DI is the practice of applying DI without using a DI Container. This means that you build a graph of objects by newing up objects using the new construct of your programming language. See for instance this example in C# (from listing 12.2 of Mark's book Dependency Injection Principles, Practices, and Patterns):

    new HomeController(
        new ProductService(
            new SqlProductRepository(
                new CommerceContext(connectionString)),
            new AspNetUserContextAdapter()));
    

    According to that book, Auto-Wiring is:

    the ability to automatically compose an object graph from maps between Abstractions and concrete types by making use of type information supplied by the compiler and the [runtime environment]. (see 12.1.2)

    In other words, with a DI Container, you will be able to 'just' tell the container about your types, and it will figure out which dependencies a type has and will be able to 'wire' that type with its dependencies.

    Considering the previous example, listing 12.3 shows how you only have to specify the mappings between abstractions and concrete types in a container:

    var container = new AutoWireContainer();
    
    container.Register(typeof(IUserContext), typeof(AspNetUserContextAdapter));
    container.Register(typeof(IProductRepository), typeof(SqlProductRepository));
    container.Register(typeof(IProductService), typeof(ProductService));
    container.Register(typeof(CommerceContext), () => new CommerceContext(connectionString));
    

    And when you ask for a HomeController, the container knows how to construct the entire graph.

    The SitePoint article briefly mentions that more advanced containers add the automatic wiring functionality

    To me, Auto-Wiring is what makes a library into a DI Container. Something can't be called a DI Container if it doesn't, at the very least, support Auto-Wiring.

    这篇关于依赖注入,组合根和入口点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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