StructureMap RegistrationConvention for decorator pattern

286 Views Asked by At

I'm using the decorator pattern to implement caching for my Repositories as such:

IFooRepository()
IFooRepository FooRepository()
IFooRepository CachedFooRepository(IFooRepository fooRepository)

The Cached repository checks the cache for the requested object and if it doesn't exist, calls the FooRepository to retrieve and store it. I'm currently registering these types with StructureMap using the following method:

For<IFooRepository>().Use<CachedFooRepository()
    .Ctor<IFooRepository>().Use<FooRepository>();

This works fine, but as the number of cached repositories grows, registering each one individually is becoming unwieldy and is error prone. Seeing as I have a common convention, I'm trying to scan my assembly using a custom IRegistrationConvention, but I can't seem to figure out how to pass the FooRepository to the constructor of CachedFooRepository in the void Process(Type type, Registry registry) function.

I've found examples to do something like:

Type interfaceType = type.GetInterface(type.Name.Replace("Cached", "I"));
registry.AddType(interfaceType, type);

or

Type interfaceType = type.GetInterface(type.Name.Replace("Cached", "I"));
registry.For(interfaceType).Use(type);

But neither method will allow me to chain the .Ctor. What am I missing? Any ideas?

0

There are 0 best solutions below