How to avoid Dependency Injection constructor madness?

前端 未结 9 1823
别跟我提以往
别跟我提以往 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:35

    I came across a similar question about constructor based dependency Injection and how complex it was getting to pass in all the dependencies.

    One of the approach, I have used in past is to use the application facade pattern using a service layer. This would have a coarse API. If this service depends on repositories, It would use a setter injection of the private properties. This requires creating an abstract factory and moving the logic of creating the repositories into a factory.

    Detailed code with explanation can be found here

    Best practices for IoC in complex service layer

提交回复
热议问题