Composition With StructureMap
Looking at a post by Karl Seguin where he show show designing for testability leads to a better design, in this case, composition over inheritance.
In the example, his CachedEmployeeLookup
has a dependency on an EmployeeLookup
, which both implement the same interface, IEmployeeLookup
How you would configure this in StructureMap so that the default class used by the program is CachedEmployeeLookup
whils开发者_高级运维t CachedEmployeeLookup
gets an EmployeeLookup
injected to it?
I think something like this would work:
For<IEmployeeLookup>().Add<EmployeeLookup>().
Named("employeeLookup");
For<IEmployeeLookup>().Use<CachedEmployeeLookup>()
.Ctor<IEmployeeLookup>().Is(
d => d.TheInstanceNamed("employeeLookup"));
You can use EnrichWith
when setting up the type mappings, e.g.
ObjectFactory.Initialize(i =>
{
i.For<IDecorator>().Use<Inner>().EnrichWith(d => new Decorator(d));
});
This page has some examples of interception in StructureMap
精彩评论