How to avoid Dependency Injection constructor madness?

前端 未结 9 1822
别跟我提以往
别跟我提以往 2020-11-22 03:59

I find that my constructors are starting to look like this:

public MyClass(Container con, SomeClass1 obj1, SomeClass2, obj2.... )

with ever

9条回答
  •  一向
    一向 (楼主)
    2020-11-22 04:32

    Injecting the container is a shortcut that you will ultimately regret.

    Over injection is not the problem, it is usually a symptom of other structural flaws, most notably separation of concerns. This is not one problem but can have many sources and what makes this so difficult to fix is that you are going to have to deal with all of them, sometimes at the same time (think of untangling spaghetti).

    Here is an incomplete list of the things to look out for

    Poor Domain Design (Aggregate root’s …. etc)

    Poor separation of concerns (Service composition, Commands, queries) See CQRS and Event Sourcing.

    OR Mappers (be careful, these things can lead you into trouble)

    View Models and other DTO’s (Never reuse one, and try to keep them to a minimal !!!!)

提交回复
热议问题