简体   繁体   English

通过接口和项目结构进行依赖注入

[英]Dependency Injection through Interface and project structure

This question is a bit difficult to explain but I will try my best. 这个问题很难解释,但我会尽力而为。

I have a project where I have to make an UI which gets data from a third party service and does deserialization and some thread management work. 我有一个项目,我必须在其中创建一个UI,该UI从第三方服务获取数据并进行反序列化和一些线程管理工作。

Now my project structure is under one solution in visual studio: 现在,我的项目结构在Visual Studio中处于一种解决方案下:

Project A : The UI 项目A:UI

Project B :The API to get data from third party service 项目B:从第三方服务获取数据的API

Project C: The Thread Manager API 项目C:线程管理器API

Note: Project B has an IB interface and C has an IC interface to help dependency injection. 注意:项目B具有IB接口,而项目C具有IC接口以帮助依赖性注入。 projects B and C will be used by other teams in future. 项目B和C将来会被其他团队使用。

Project A is using both IB and IC interfaces for dependency injection. 项目A使用IB和IC接口进行依赖项注入。

Now I will state my understanding of IOC : DIP says a high-level module should not depend on low-level module and both high-level and low-level module should depend on an abstraction. 现在,我将陈述对IOC的理解:DIP表示高级模块不应依赖于低级模块,而高级模块和低级模块均应依赖于抽象。 If you want to prevent the changes in high-level module on changing of low-level module you need to invert the control so that low-level module will not control the interface and creation of objects that the high-level module needs. 如果要防止在更改低级模块时更改高级模块,则需要反转控件,以使低级模块不会控制高级模块所需的接口和对象的创建。

According to the above definition both the IB and IC interfaces should be defined in project A right? 根据以上定义,IB和IC接口都应在项目A中定义,对吗? If they are in project A then how will other teams use IB and IC interfaces? 如果他们在项目A中,那么其他团队将如何使用IB和IC接口? Do I make another separate project for storing the interfaces? 我是否要制作另一个单独的项目来存储接口?

Consider the following example project organization for your scenario: 考虑以下针对您的方案的示例项目组织:

Project A: Bootstrapper, in charge of running the application, configuring DI and setting up the UI (this could also go in a dedicated Bootstrapper project decoupled from the app and UI). 项目A:Bootstrapper,负责运行应用程序,配置DI和设置UI(这也可以在与应用程序和UI分离的专用Bootstrapper项目中进行)。 Knows B, C, D, E, F 知道B,C,D,E,F

Project B: the UI (or as many projects as required to make it modular). 项目B:UI(或使其模块化所需的项目数)。 Knows C 知道C

Project C: business logic ( gets data from a third party service and does deserialization and some thread management work ). 项目C:业务逻辑( gets data from a third party service and does deserialization and some thread management work )。 Knows D 知道D

Project D: interfaces, containing IB and IC (they could also have separate dedicated projects). 项目D:包含IB和IC的接口(它们也可以有单独的专用项目)。

Project E: The API to get data from third party service Knows D 项目E: The API to get data from third party service 知道D

Project F: The Thread Manager API Knows D 项目F: The Thread Manager API 知道D

Notice the decoupling from the UI and the implementation through the business logic layer. 注意,通过业务逻辑层从UI和实现中分离。 This way, you can switch the implementation details without having to change either the business logic or the UI. 这样,您可以切换实现细节,而不必更改业务逻辑或UI。 Also, if your business logic varies, the impact is minimized. 此外,如果您的业务逻辑有所不同,那么影响也将降至最低。

Regarding DI, the bootstrapper project is the only one that has the whole picture, the rest should just use interfaces that the container set up by the bootstrapper will resolve (ie inject) for them. 关于DI,bootstrapper项目是唯一具有整体情况的项目,其余项目应仅使用由bootstrapper设置的容器将为它们解析(即注入)的接口。

According to the above definition both the IB and IC interfaces should be defined in project A right? 根据以上定义,IB和IC接口都应在项目A中定义,对吗?

No. You should replace the term 'module' with 'class'. 不能。您应该将“模块”一词替换为“类”。 In that case it starts to make more sense: 在这种情况下,它变得更加有意义:

high-level [class] should not depend on low-level [class] and both high-level and low-level [classes] should depend on an abstraction. 高级[类]不应依赖于低级[类],高级和低级[类]均应依赖于抽象。

To be able to do dependency injection correctly, it doesn't matter in which assemblies those classes and abstractions are located. 为了能够正确进行依赖注入,这些类和抽象位于哪个程序集中都没有关系。 When the project gets bigger (ie multiple millions of lines of code, one or multiple teams working on it), it becomes important to adhere to the Principles of Component Design . 当项目变大时(例如,数百万行代码,一个或多个团队在其中工作),遵守组件设计原则就变得很重要。

In your case, since both assembly A and B use interface IB , and assembly A depends on assembly B, IB can never be located in assembly A, since that would case a circular reference in the assembly. 在您的情况下,由于程序集A和B都使用接口IB ,而程序集A依赖于程序集B,因此IB永远都不能位于程序集A中,因为这样会在程序集中引用一个圆形引用。 You will either have to place IB in assembly B or in a new assembly that both A and B refer to. 您将不得不将IB放置在装配B中,或者放置在A和B都引用的新装配中。

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

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