我正在使用装饰器模式为我的存储库实现缓存,如下所示:
IFooRepository()
IFooRepository FooRepository()
IFooRepository CachedFooRepository(IFooRepository fooRepository)
Cached 存储库检查所请求对象的缓存,如果它不存在,则调用 FooRepository 来检索和存储它。我目前正在使用以下方法向 StructureMap 注册这些类型:
For<IFooRepository>().Use<CachedFooRepository()
.Ctor<IFooRepository>().Use<FooRepository>();
这很好用,但是随着缓存存储库数量的增加,单独注册每个存储库变得笨拙并且容易出错。看到我有一个共同的约定,我正在尝试使用自定义 IRegistrationConvention 扫描我的程序集,但我似乎无法弄清楚如何将 FooRepository 传递给函数中 CachedFooRepository 的构造void Process(Type type, Registry registry)函数。
我找到了一些例子来做类似的事情:
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. 我错过了什么?有任何想法吗?