Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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)?

like image 612
alexandrul Avatar asked Dec 02 '09 08:12

alexandrul


People also ask

Which library is used for dependency injection?

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.

Which of the following are methods of dependency injection?

There are three types of dependency injection — constructor injection, method injection, and property injection.

How many dependencies should a class have?

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.


1 Answers

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.

like image 194
Konamiman Avatar answered Nov 01 '22 17:11

Konamiman