What are the best practices for class libraries using dependency injection for internal operations? [duplicate]

戏子无情 提交于 2019-12-17 20:10:03

问题


What should I be careful about when building a class library complex enough to use internal dependency injection?

Assuming that it will use Castle Windsor (as an example), what would be the best place/method to configure the container, given that the library will be used by simple console application (with no DI), web forms using the same container (Castle Windsor), and web apps using a different container (NInject)?


回答1:


I would use the facade pattern here: in the library, expose a public method on a public class that does the container initialization (such as a simple Initialize()), and use Castle Windsor only internally within the library, so that the library clients don't even know that you are using it.




回答2:


Not that the answer would not work but I think anyone who lands here should take a look at this Q/A. After reading over it I MUST agree that using an IoC within a class library smells like a ServiceLocator (anti-pattern) and that Coupling a library to a container is a smell.

Initially I thought I would be doing a good thing, best thing I did was to look it up first.



来源:https://stackoverflow.com/questions/1831455/what-are-the-best-practices-for-class-libraries-using-dependency-injection-for-i

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!