Hi,
We are using StructureMap as our Dependency Injection (DI) framework while we are creating a layered library. Our goals are to:
- Make it easy for us to Unit Test classes (and using mock classes instead of the real dependencies) while we develop and maintain the library.
- Make it easy for the library user to:
- Customize the library behavior by implementing an interface them selfs and configuring the library to use their implementation instead of our own.
- Unit Test their implemented classes (it's the same goal we have internally, but we want it to be fulfilled for the library user as well).
By layered library, we mean that we are building two DLL`s:
- The core library. It should be usable from any .NET application, be it a Console application or an MVC one.
- A WebForms library. It includes the core library but also provides WebForms Control`s to make life easier for WebForms users.
Our questions:
Where should we call the code to map interfaces with concrete classes for the core library? It has no single entry point, there are numerous classes which may be instantiated first.
Currently we force the user to call
DependencyHandler.Init()
before doing any other library call. Is there a nicer way, like a piece of code which gets executed after loading the DLL so that we won't force the user to write one line of boiler-plate code?Which is the recommended way to let library users change an interface implementation into their own type? Currently the user may retrieve the Registry by
StructureMap.Configuration.DSL.Registry theRegistry = DependencyHandler.Instance().GetConfiguration()
and then change things by for exampletheRegistry.For<IFoo>().Use<MyOwnFooImplementation>();
Would it be nicer to use XML? And if so, where should we put said XML? We choose the programmatic approach as Visual Studio will be able to give the user some help.If the current approach in 2 above is used, the library user is required (at least now) to add a Reference to StructureMap.DLL as well as to our DLL. Could we remove that burden from the user, maybe by using XML for dependency setup instead?
Is there a nice central location in WebForms we should use, which solves problem 1 for the WebForms library DLL?
How do you recommend we structure things for production and test?
The current idea is to use DI in all places necessary to enable Unit Testing where we and supposedly our users want it, as well as enabling the users to change the library behavior by providing their own implementations.
To test things, we and the library users will have to create mock classes to replace dependencies we want to break away. The idea is that we use the normal configuration but override the classes we want to mock instead of using their normal implementation. Is that a good way to proceed?