简体   繁体   English

依赖注入实现

[英]Dependency Injection Implementation

I have three layers in my solution, Presentation (winforms project), Domain (class lib) and Persistence (class lib). 我的解决方案包含三层,分别是Presentation(winforms项目),Domain(类库)和Persistence(类库)。 I am trying to implement dependency injection to decouple them. 我正在尝试实现依赖项注入以将它们分离。 My application root is in my presentation layer. 我的应用程序根目录位于我的表示层中。 I understand the only time the DI container (unity in this case) should be referenced is in my application root, otherwise I would be simply replacing class dependencies all over the place with a dependency on my DI container (which I suppose is still slightly better). 我知道唯一一次应引用DI容器(在这种情况下为统一)是在我的应用程序根目录中,否则我将简单地将整个类的依赖关系替换为对我的DI容器的依赖关系(我想这仍然会更好一些) )。

So with these foundation concepts in mind, I am really struggling with the specific implementation. 因此,考虑到这些基础概念,我确实在为具体的实现而苦苦挣扎。 Perhaps my application root should be in its own seperate project - perhaps a console application. 也许我的应用程序根目录应该在其自己的单独项目中-也许是控制台应用程序。 I can then resolve the first 'overallApplication' class, listing IPresentation, IDomain and IPersistence in its constructors. 然后,我可以解析第一个“ overallApplication”类,在其构造函数中列出IPresentation,IDomain和IPersistence。 I understand (assuming actual implementations have been registered) the unity framework would then recursively solve all respective sub-dependencies. 我了解(假设已经注册了实际的实现)统一框架将递归解决所有各自的子依赖关系。

In your experience - would you be able to advise if this was a sound approach. 根据您的经验-您是否可以建议这是否是一种合理的方法。 I really understand the concept and importance of decoupling, and how this is solved by DI conceptually at a high level, but I am struggling to tie it all together in a real application solution with multiple layers (organised in VS as seperate projects). 我真的很了解去耦的概念和重要性,以及去耦在概念上如何从高层次解决DI的问题,但是我正在努力将它们全部结合在一个具有多层(在VS中作为独立项目组织的)的实际应用程序解决方案中。

Any help or pointers towards examples of proper implementations would be greatly appreciated. 任何帮助或指向适当实现示例的指针将不胜感激。

Some thoughts for you in hope that they help. 为您提供一些想法,希望对您有所帮助。

Throughout your question you make statements such as: 在整个问题中,您都会发表以下声明:

"I really understand the concept and importance of decoupling, and how this is solved by DI..." “我真的很了解去耦的概念和重要性,以及DI如何解决这个问题……”

The first thing I would evaluate is the understanding that DI != IoC Container (eg Unity). 我要评估的第一件事是了解DI!= IoC容器(例如Unity)。

An IoC container is used to remove boilerplate code that results from an existing DI structure. IoC容器用于删除现有DI结构产生的样板代码。 As such I would suggest you refactor without Unity /first/. 因此,我建议您在没有Unity / first /的情况下进行重构。 Then go back and add Unity to reduce your code. 然后返回并添加Unity以减少代码。

So that: 以便:
-1. -1。 Make application Di via manual Ctor, Property, Method, Service Locator injection methods. 通过手动Ctor,Property,Method,Service Locator注入方法创建应用程序Di。
-2. -2。 After this is setup you should see things like: 设置完成后,您应该会看到类似以下内容:

public View() {
  var controller = new Controller(new IView(), new model(), new IService(new Dal(ISession(connectionString))), new , new ILogger(), etc.){}
}

-3. -3。 Then once you have something like this in your code you can then use Unity to inject all that fun: 然后,一旦您的代码中包含类似的内容,就可以使用Unity注入所有乐趣:

public View() {}
Controller Controller {get;set;}  //<- Unity auto builds & populates this for you with all the "new" items normally found in your constructor (or wherever).

While not a production example, it should give an idea of some steps to refactor. 尽管不是生产示例,但应该给出一些重构步骤的想法。 Going straight to Unity will put the cart before the horse, so to speak. 可以这么说,直奔Unity会将车推到马的前面。

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

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