views:

37

answers:

2

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 whilst CachedEmployeeLookup gets an EmployeeLookup injected to it?

A: 

I think something like this would work:

For<IEmployeeLookup>().Add<EmployeeLookup>().
   Named("employeeLookup");

For<IEmployeeLookup>().Use<CachedEmployeeLookup>()
  .Ctor<IEmployeeLookup>().Is(
     d => d.TheInstanceNamed("employeeLookup"));
PHeiberg
Oops! Had the wrong default object. Changed default to be correct.
PHeiberg
+1  A: 

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

Lee