简体   繁体   English

n层应用程序中的依赖注入?

[英]Dependency Injection in an n-tier application?

I have a 3-tier .NET service app, which follows the standard approach: 我有一个3层的.NET服务应用程序,遵循标准方法:

Frontend -> Object Model / Business Logic -> Data Access

I'm trying to learn about dependency injection along the way, and thus far have found it great (using Autofac). 我一直在努力学习依赖注入,到目前为止已经发现它很棒(使用Autofac)。 Each of the 3 tiers needs to create an assortment of objects, sometimes with extra configuration/etc. 3层中的每一层都需要创建各种各样的对象,有时需要额外的配置/等。 It seems like the DI container should be the ideal thing to solve this, but I'm having some issues seeing where it should live in relation to the rest of the system. 似乎DI容器应该是理想的解决方案,但是我遇到了一些问题,看看它应该与系统的其他部分相关。

Currently I have a class in the frontend which configures the DI container. 目前我在前端有一个配置DI容器的类。 It is basically a big bunch of code saying container.Register<SomeType>() and so on. 它基本上是一大堆代码,说container.Register<SomeType>()等等。

The problem is, it is configuring the container for all 3 tiers, and hence must have fairly invasive knowledge of the data access layer. 问题是,它正在为所有3层配置容器,因此必须具有对数据访问层的相当侵入性的知识。 Having code in my frontend with such knowledge sets off alarm bells in my head as the point of separating the app into tiers is to avoid this exact situation. 在我的前端有这样的知识的代码在我的头脑中引起了警钟,因为将应用程序分成层级的关键是避免这种情况。
This is also made worse by the fact that my Data access layer isn't just SQL server being a dumb bucket of bits, but is made up of lots of complex COM interop and P/Invoke calls, so has quite an impact on the DI configuration. 由于我的数据访问层不只是SQL服务器是一个笨拙的桶,而是由许多复杂的COM互操作和P / Invoke调用组成,所以这也变得更糟,因此对DI有相当大的影响组态。

I have given some thought to breaking it up - perhaps having one container per tier, or having a "Setup" class in each tier which talks to the global DI container to register it's own bits, but I'm not sure if that will cause more problems than it solves... 我已经考虑过打破它 - 可能每层有一个容器,或者每层都有一个“Setup”类与全局DI容器对话以注册它自己的位,但我不确定这是否会导致比它解决的问题更多......

I would really appreciate it if anyone could share their experiences in using DI with multitiered apps. 如果有人可以分享他们使用DI与多层应用程序的经验,我将非常感激。

Thanks, Orion. 谢谢,猎户座。

This depends on if you have have three tiers (physical separation) or if all your logical layers are deployed together. 这取决于您是否有三层(物理分离)或所有逻辑层是否一起部署。 If the frontend is separate from your BL and communicates through a webservice or WCF then the frontend and the backend needs their own containers because they're running in separate processes or separate machines. 如果前端与BL分开并通过Web服务或WCF进行通信,那么前端和后端需要自己的容器,因为它们在不同的进程或单独的机器上运行。 The containers would only register its own components and the interfaces of the 'next' layer. 容器只会注册自己的组件和“下一个”层的接口。

On the other hand if all the layers are running in the same process then you should only have one container. 另一方面,如果所有图层都在同一个进程中运行,那么您应该只有一个容器。 The container would be initialized and hosted in the starting point of the application like global.asax for a web app. 容器将初始化并托管在应用程序的起始点,如web应用程序的global.asax。

The problem with the container host knowing to much of all the different parts of the system could be solved by not registering the classes one by one, but instead register all types in an assembly. 容器主机知道系统的所有不同部分的问题可以通过不逐个注册类来解决,而是在程序集中注册所有类型。 That way you don't need strong references to all assemblies in your solution just to configure the container. 这样,只需配置容器,就不需要对解决方案中的所有程序集进行强引用。 Example of how that can be done with Castle Winsdor: 如何使用Castle Winsdor完成此操作:

Kernel.Register(AllTypes.Pick().FromAssemblyName("DataAccessLayer.dll"));
Kernel.Register(AllTypes.Pick().FromAssemblyName("BusinessLogic.dll"));

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

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