Is the DI pattern limiting wrt expensive object creation coupled with infrequent dependency usage?

前端 未结 4 1959
甜味超标
甜味超标 2020-12-16 04:19

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

4条回答
  •  情深已故
    2020-12-16 04:51

    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.

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

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

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

提交回复
热议问题