Using Dependency Injection frameworks for classes with many dependencies

后端 未结 6 410
无人及你
无人及你 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:28

    First:

    You might approach it by creating a container to hold your "uninteresting" dependencies (ILog, ICache, IApplicationSettings, etc), and use constructor injection to inject that, then internal to the constructor, hydrate the fields of the service from container.Resolve() ? I'm not sure I'd like that, but, well, it's a possibility.

    Alternatively, you might like to use the new IServiceLocator common interface (http://blogs.msdn.com/gblock/archive/2008/10/02/iservicelocator-a-step-toward-ioc-container-service-locator-detente.aspx) instead of injecting the dependencies?

    Second:

    You could use setter injection for the optional/on-demand dependencies? I think I would go for injecting factories and new up from there on-demand.

提交回复
热议问题