当使用服务层时,配置你的IoC容器的正确层是什么?[英] What is the correct layer to configure your IoC container when using a service layer?

本文是小编为大家收集整理的关于当使用服务层时,配置你的IoC容器的正确层是什么?的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我有一个中型ASP.NET MVC应用程序.它消耗了一个处理所有存储库使用,调用域服务等的服务层.我的控制器操作非常苗条 - 他们基本上称为服务类,获得响应并显示出来.大多数组件都是基于一些穷人的DI的接口.该应用程序正在增长,需要更好的测试支持,并开始呼吁使用IOC容器.

我阅读的所有内容(例如因此,问题)指出,我应该在应用程序根上配置IOC.如果我从控制器的操作中使用存储库,并且在控制器级别需要DI,这对我来说是有道理的,但我不是.似乎我希望我的组成词根在我的服务层中.我一直认为我不希望我的web.config(或其他配置)在UI层中甚至提及/查看/听到有关存储库,信用卡处理器等

我是正确地考虑这一点还是我只需要克服它?

推荐答案

您只需要克服它:)

在应用程序root中,将您的构图root 在web.config中包含很多DI容器的东西.如果可以的话,可以,但这是可选的.将构图根放在应用程序根中时,什么是可选的是您需要在global.asax中使用一些DI代码.

您可能会发现它无关紧要,因为您的控制器是如此薄,但这不是真正的观点.实际观点是您(摘要的"您")想推迟耦合课程,直到最后一个负责任的时刻.您越早夫妇类型,您的灵活性就越少.

如果您在服务层中求助了类,则在此时做出不可逆转的决定.如果以后您需要以不同的方式撰写这些服务,那么您不能 - 不是不进行重新编译,也就是说.

如果这样做有很大的好处,我可以理解您为什么想要的,但没有.您不妨等待制作所有组件,直到您绝对必须这样做 - 这就是应用程序的入口处.

其他推荐答案

我的情况与您和我的情况相同,我对此进行了处理.

我使用的一般规则是具有global.asax或类似的东西,它需要执行注册IOC组件的代码.提出它的另一种方法是,您需要为正在运行的每个不同过程中运行一个(即网站处于一个过程中,服务在另一个过程中).

就我而言,我一次为MVC网站global.asax和服务器再次执行此操作.在这种情况下,进行的注册将在服务和网站之间有所不同.

此外,我还要做一件事.由于我在MVC应用程序和服务(即记录)之间重复使用组件,因此我有一个第三个核心组件,该组件会为系统的核心IOC组件注册核心IOC组件,并且网站和服务注册都调用了该组件.因此,我在服务和网站之间共有的任何东西都进入核心注册,然后在"接口"特定的注册中进行.

.

希望会有所帮助.

其他推荐答案

从Java的角度来看,我为IOC容器使用弹簧框架.有了它,容器确实在整个应用程序中.尽管您可以为不同层(持久性配置文件,服务配置文件,控制器配置文件等)具有不同的配置文件,但所有对象(Java Lingo中的Bean)都进入容器中.

我认为这仍然可以,因为您提到的类之间没有耦合.视图不需要知道信用卡处理器,只是因为它们在同一IOC容器中.这些类只会(通过注射)仅收到所需的依赖项,并且与容器中的其他对象无关.

本文地址:https://www.itbaoku.cn/post/627312.html

问题描述

I have a medium sized asp.net MVC app. It consumes a service layer that handles all the repository use, calling domain services, etc. My controller actions are very slim -- they basically call a service class, get a response and show that respose. Most components are interface based with some poor man's DI. The app is growing, needs better testing support, and starting to call out for an IoC container.

Everything I read (such as this SO question) states that I should configure the IoC at the application root. This makes sense to me if I were using repositories right from my controller actions and needed DI at the controller level, but I'm not. It seems like I'd want my composition root in my service layer. I keep thinking that I don't want my web.config (or another config) at the UI layer even mentioning/seeing/hearing about a repository, credit card processor, etc.

Am I thinking about this the right way or do I just need to get over it?

推荐答案

You just need to get over it :)

Having your Composition Root in the application root doesn't require you to have a lot of DI Container stuff in web.config. You can if you will, but it's optional. What is not optional when putting the Composition Root in the application root is that you need to have some DI code in Global.asax.

You may find it irrelevant because your Controllers are so thin, but that's not the real point. The actual point is that you (the abstract 'you') want to postpone coupling classes until the last responsible moment. The earlier you couple types, the less flexibility you have.

If you couple classes in the service layer, you make an irreversible decision at that point. If it later turns out that you need to compose those services differently, you can't - not without recompiling, that is.

If there was a major benefit of doing it, I can understand why you would want to, but there isn't. You may as well wait composing all components untill you absolutely must do so - and that's in the application's entry point.

其他推荐答案

I have the same situation as you and I tackle it as follows.

The general rule I use is what ever has a global.asax or something similar, it needs to execute the code that registers the IoC components. Another way of putting it is that you need to run it one for each different process that is running (i.e. the website is in one process and the service is in another).

In my case I do this once for the mvc website global.asax and again for the server. In this case the registrations that get made would be different between the service and the website.

In addition I do one more thing. Due to the fact that I reuse components between the mvc app and the service (i.e. logging) I have a third core component that registers the core IoC components for the system and this component is called by the both the website and services registrations. Hence I anything that is common between the service and the website go into the core registration and then anything that is different goes into the 'interface' specific registration.

Hope that helps.

其他推荐答案

Coming from a Java perspective, I use the Spring framework for my IoC container. With it, the container really is application-wide. Although you can have different configuration files for different layers (a persistence config file, a services config file, a controller config file, etc), all of objects (beans in Java lingo) go into the container.

I think this still ok though because there is no coupling between classes as you mentioned. A view does not need to know about a credit card processor, just because they are in the same IoC container. These classes will receive (by injection) only the dependencies they need, and are not concerned with other objects in the container.