I’m using a class library that can be reused by other components. In this class library I’m using unity for dependency injection. For this class library I create a test project. The caller also gets a test project. One thing I’m uncertain about is the location of the bindings. Should I incorporate this in the class library or should I do this from the calling application?
Share
This is an interesting problem. How can you dependency inject re-usable assemblies that do not have an entry point. I would really like to see other people’s answer.
Dependency injection is the responsibility of the entry-point assembly. Yet, if you have a lot of classes and assemblies each needing DI, then it is possible that they are left-off for some classes/assemblies and the task becomes onerous.
Solution One
Using convention over configuration. You stick to a rule of class
FooimplementingIFoo, etc. A lot of DI frameworks have the means to set it up using convention.Solution two
Above solution does not solve all problems since sometimes you need to parameterise the setup of the injection. Here is how I have solved the problem (especially for those assemblies loaded by MEF and this is for
AutoFac):Created an interface
IIocInstallerwhere container (or builder is passed)Created an assembly attribute that flags assemblies needing DI:
In each assembly, I create a class that sets up DI:
Then in the entry point, I have a common code which looks through all loaded assemblies (including MEFed ones) that have the assembly attribute and then look for the type implementing the
IIocInstallerand then call Setup on them.