应该给依赖项注入许多“级别”吗?超过所需? [英] Should a dependency be injected many "levels" up than it is needed?

查看:54
本文介绍了应该给依赖项注入许多“级别”吗?超过所需?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用SOLID原理编写C#ASP.NET MVC Web应用程序。

I'm writing a C# ASP.NET MVC web application using SOLID principles.

我已经编写了 ViewModelService ,这取决于 AccountService RepositoryService ,因此我将这两个服务注入了 ViewModelServer

I've written a ViewModelService, which depends on a AccountService and a RepositoryService, so I've injected those two services in the the ViewModelServer.

PermissionService 取决于 HttpContextBase 以便使用 GetOwinContext()来获取 UserManager 。该控制器有一个需要使用的 HttpContextBase 实例-因此似乎我必须注入 HttpContextBase 实例放入 ViewModelService ,然后将其注入 PermissionService

The PermissionService depends on the HttpContextBase in order to use GetOwinContext() to get an instance of the UserManager. The controller has an instance of HttpContextBase that needs to be used - so it seems like I have to inject the HttpContextBase instance into the ViewModelService which then injects it into the PermissionService.

因此,就代码而言,我有:

So, in terms of code I have:

public ViewModelService

public CategoryRepository(ApplicationDbContext context, IPermissionService permissionservice)

public AccountService(HttpContextBase httpcontext, IPrincipal securityprincipal)

实例化 ViewModelService ,然后执行以下操作:

to instantiate the ViewModelService, I then do this:

new ViewModelService(
    new CategoryRepository(
            new ApplicationDbContext(), 
            new PermissionService(
                new AccountService(HttpContext, Thread.CurrentPrincipal),
                new UserPasswordRepository(new ApplicationDbContext()),
                new ApplicationSettingsService())),
    new PasswordRepository(
            new ApplicationDbContext(), 
            new PermissionService(
                new AccountService(HttpContext, Thread.CurrentPrincipal), 
                new UserPasswordRepository(new ApplicationDbContext()),
                new ApplicationSettingsService())),
    new ModelValidatorService());

应该从这么多级别中注入依赖项,还是有更好的方法? / p>

Should a dependency be injected from that many "levels" up, or is there a better way?

推荐答案

还有一个平衡点。

一方面,您有一种流派,即坚持要求所有依赖项必须由类公开,以便正确注入。 (这是一门思想流派,认为诸如服务定位器之类的东西是一种反模式。)这样做有其优点,但走到极端,您会发现自己现在所处的位置。在某些本身具有复合模型的复合模型中,恰到好处的复杂性导致聚合根需要仅注入个依赖性来满足更深层模型的依赖性。

On the one hand, you have the school of thought which would insist that all dependencies must be exposed by the class to be "properly" injected. (This is the school of thought which considers something like a Service Locator to be an anti-pattern.) There's merit to this, but taken to an extreme you find yourself where you are now. Just the right kind of complexity in some composite models, which themselves have composite models, results in aggregate roots which need tons of dependencies injected solely to satisfy dependencies of deeper models.

我个人发现这会在这种情况下产生耦合。 DI是要解决的问题,而不是创建DI的问题。

Personally I find that this creates coupling in situations like this. Which is what DI is intended to resolve, not to create.

另一方面,您的思路允许服务定位器方法,其中模型可以在内部调用一些公共域服务来解决对它的依赖关系。这样做是有好处的,但是如果发现极端的话,您会发现您的依赖项鲜为人知,如果无法解决任何给定的依赖项,则可能会发生运行时错误。 (基本上,您会在更高级别上出错,因为使用对象永远不会知道所使用的对象需要未提供的东西。)

On the other hand, you have the school of thought which allows for a Service Locator approach, where models can internally invoke some common domain service to resolve a dependency for it. There's merit to this, but taken to an extreme you find that your dependencies are less known and there's a potential for runtime errors if any given dependency can't be resolved. (Basically, you can get errors at a higher level because consuming objects never knew that consumed objects needed something which wasn't provided.)

我个人使用过一项服务定位器的方法很多(主要是因为这是将DI引入到旧域中的一种非常方便的模式,这是较大的重构工作的一部分,这是我的专业工作),并且从未遇到过此类问题。

Personally I've used a service locator approach a lot (mostly because it's a very handy pattern for introducing DI to a legacy domain as part of a larger refactoring exercise, which is a lot of what I do professionally) and have never run into such issues.

有阴阳两种。而且我认为每个解决方案领域都有自己的平衡。如果您发现直接注入使系统难以维护,则可能值得调查服务位置。相反,如果整个域模型本身是固有耦合的,并且DI问题只是该耦合的症状而不是原因,则可能值得研究。

There's yin and yang either way. And I think each solution space has its own balance. If you're finding that direct injection is making the system difficult to maintain, it may be worth investigating service location. Conversely, it may also be worth investigating if the overall domain model itself is inherently coupled and this DI issue is simply a symptom of that coupling and not the cause of it.

这篇关于应该给依赖项注入许多“级别”吗?超过所需?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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