Using Dependency Injection frameworks for classes with many dependencies

后端 未结 6 645
深忆病人
深忆病人 2021-01-30 07:05

I have been looking at various dependency injection frameworks for .NET as I feel the project I am working on would greatly benefit from it. While I think I have a good grasp of

6条回答
  •  后悔当初
    2021-01-30 07:32

    Well, while you can do this as described in other answers I believe there is more important thing to be answered regarding your example and that is that you are probably violating SRP principle with class having many dependencies.

    What I would consider in your example is breaking up the class in couple of more coherent classes with focused concerns and thus the number of their dependencies would fall down.

    Nikola's law of SRP and DI

    "Any class having more than 3 dependencies should be questioned for SRP violation"

    (To avoid lengthy answer, I posted in detail my answers on IoC and SRP blog post)

提交回复
热议问题