装饰模式的StructureMap RegistrationConvention

时间:2011-10-25 04:39:01

标签: repository-pattern structuremap decorator convention-over-configur auto-registration

我正在使用装饰器模式为我的存储库实现缓存:

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

Cached存储库检查所请求对象的缓存,如果它不存在,则调用FooRepository来检索并存储它。我目前正在使用StructureMap使用以下方法注册这些类型:

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

这样可以正常工作,但随着缓存存储库数量的增加,单独注册每个存储库变得难以处理并且容易出错。看到我有一个共同的约定,我正在尝试使用自定义IRegistrationConvention扫描我的程序集,但我似乎无法弄清楚如何将FooRepository传递给void Process(Type type, Registry registry)函数中的CachedFooRepository的构造函数。 / p>

我找到了一些例子:

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

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

但这两种方法都不允许我链接.Ctor。我错过了什么?有什么想法吗?

0 个答案:

没有答案
相关问题