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

查看:133
本文介绍了在使用服务层时配置IoC容器的层次是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个中等大小的asp.net MVC应用程序。它消耗了处理所有存储库使用,调用域服务等的服务层。我的控制器操作非常苗条 - 它们基本上称为服务类,获取响应并显示复制。大多数组件都是基于界面的,一些穷人的DI。该应用正在增长,需要更好的测试支持,并开始调用IoC容器。



我读取的一切(如这个SO问题)表示我应该在应用程序根目录下配置IoC 。这对我来说是有道理的,如果我正在从控制器操作中使用存储库,并且需要在控制器级别进行DI,但我不是。看来我想要我的组合根在我的服务层。我一直认为我不想在UI层的web.config(或另一个配置),甚至提到/看/了解一个存储库,信用卡处理器等。



我想这是正确的方法,或者我只需要解决它。

解决方案

你只需要解决它:)



在应用程序根目录中使用构成根不需要在Web中拥有大量的DI Container的.config。你可以,如果你愿意,但它是可选的。将组合根放在应用程序根目录中是什么是不可选的,那就是你需要在Global.asax中有一些DI代码。



你可能会发现它不相关,因为您的控制器太薄了,但这不是真正的一点。实际的一点是,你(抽象的'你')想推迟耦合类,直到最后负责时刻。你们之间的耦合类型越少,灵活性越低。



如果你在服务层中耦合类,那么在这一点上做出不可逆转的决定。如果以后证明您需要以不同的方式撰写这些服务,那么您不能 - 不需要重新编译就可以了。



如果有一个主要的好处,我可以理解你为什么要,但是没有。你可以等到撰写所有的组件,直到你绝对必须这样做 - 那就是在应用程序的入口点。


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.

这篇关于在使用服务层时配置IoC容器的层次是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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