Using Dependency Injection frameworks for classes with many dependencies

后端 未结 6 426
无人及你
无人及你 2021-01-30 07:21

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:37

    First: Add the simple dependencies to your constructor as needed. There is no need to add every type to every constructor, just add the ones you need. Need another one, just expand the constructor. Performance should not be a big thing as most of these types are likely to be singletons so already created after the first call. Do not use a static DI Container to create other objects. Instead add the DI Container to itself so it can resolve itself as a dependency. So something like this (assuming Unity for the moment)

    IUnityContainer container = new UnityContainer();
    container.RegisterInstance(container);
    

    This way you can just add a dependency on IUnityContainer and use that to create expensive or seldom needed objects. The main advantage is that it is much easier when unit testing as there are no static dependencies.

    Second: No need to pass in a factory class. Using the technique above you can use the DI container itself to create expensive objects when needed.

    Three: Add the DI container and the light singleton dependencies to the main form and create the rest through the DI container as needed. Takes a little more code but as you said the startup cost and memory consumption of the mainform would go through the roof if you create everything at startup time.

提交回复
热议问题