DI图案限制wrt昂贵的对象创建加上不常见的依赖关系使用吗? [英] Is the DI pattern limiting wrt expensive object creation coupled with infrequent dependency usage?

查看:115
本文介绍了DI图案限制wrt昂贵的对象创建加上不常见的依赖关系使用吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当涉及到典型的构造函数依赖注入时,我很难把头摆在一个明显的模式问题/限制之下。例如,让我说我有一个ASP.NET MVC3控制器,如下所示:

I'm having a hard time getting my head around what seems like an obvious pattern problem/limitation when it comes to typical constructor dependency injection. For example purposes, lets say I have an ASP.NET MVC3 controller that looks like:

Public Class MyController
    Inherits Controller

    Private ReadOnly mServiceA As IServiceA
    Private ReadOnly mServiceB As IServiceB
    Private ReadOnly mServiceC As IServiceC

    Public Sub New(serviceA As IServiceA, serviceB As IServiceB, serviceC As IServiceC)
        Me.mServiceA = serviceA
        Me.mServiceB = serviceB
        Me.mServiceC = serviceC
    End Sub

    Public Function ActionA() As ActionResult
        ' Do something with Me.mServiceA and Me.mServiceB
    End Function

    Public Function ActionB() As ActionResult
        ' Do something with Me.mServiceB and Me.mServiceC
    End Function
End Class

我很难过的事情是DI的事实容器被要求实例化所有三个依赖关系,在任何给定时间,只有一部分依赖关系可能需要此控制器上的操作方法。

The thing I'm having a hard time getting over is the fact that the DI container was asked to instantiate all three dependencies when at any given time only a subset of the dependencies may be required by the action methods on this controller.

似乎假设对象构造是污垢,并且没有来自对象构造的副作用或者所有的依赖关系被一致地使用。如果物体的构造没有吱吱声或有副作用怎么办?例如,如果构建 IServiceA 涉及打开连接或分配其他重要资源,那么当 ActionB 被调用。

It's seems assumed that object construction is dirt-cheep and there are no side effects from object construction OR all dependencies are consistently utilized. What if object construction wasn't cheep or there were side effects? For example, if constructing IServiceA involved opening a connection or allocating other significant resources, then that would be completely wasted time/resources when ActionB is called.

如果这些操作方法使用了服务位置模式(或其他类似的模式),则永远不会有机会不必要地构造一个对象实例这将不会使用,当然使用这种模式有附加的其他问题使它没有吸引力。

If these action methods used a service location pattern (or other similar pattern), then there would never be the chance to unnecessarily construct an object instance that will go unused, of course using this pattern has other issues attached making it unattractive.

使用规范的构造函数注入+接口模式DI基本上将开发人员锁定在一个限制中,依赖关系的实现必须要被实例化或实例必须被显着利用?我知道所有的模式都有其利弊,这只是DI的缺点之一吗?我从来没有见过它,我觉得好奇。

Does using the canonical constructor injection + interfaces pattern of DI basically lock the developer into a "limitation" of sorts that implementations of the dependency must be cheep to instantiate or the instance must be significantly utilized? I know all patterns have their pros and cons, is this just one of DI's cons? I've never seen it mentioned before, which I find curious.

推荐答案

如果你有很多字段不是每个成员使用这意味着凝聚力低。这是一个一般的编程概念 - 构造函数注入只是使其更可见。通常是违反单一责任原则的相当好的指标。

If you have a lot of fields that aren't being used by every member this means that the class' cohesion is low. This is a general programming concept - Constructor Injection just makes it more visible. It's usually a pretty good indicator that the Single Responsibility Principle is being violated.

如果是这种情况,那么重构(例如 Facade服务)。

If that's the case then refactor (e.g. to Facade Services).

创建对象图时,您不必担心性能。

You don't have to worry about performance when creating object graphs.

当涉及到副作用时,(DI)构造函数应该很简单,不会有副作用

When it comes to side effects, (DI) constructors should be simple and not have side effects.

这篇关于DI图案限制wrt昂贵的对象创建加上不常见的依赖关系使用吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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