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)?
Enterprise Library is a good example of a library that really takes advantage of a dependency injection container without being hard coupled to one. If you'd like to write a library that uses a DI container but doesn't force your choice on your consumer, we hope you can find some design inspiration from our example.
There are three types of dependency injection — constructor injection, method injection, and property injection.
The fact your class has so many dependencies indicates there are more than one responsibilities within the class. Often there is an implicit domain concept waiting to be made explicit by identifying it and making it into its own service. Generally speaking, most classes should never need more than 4-5 dependencies.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With