我目前有一堆像这样的存储库
我的存储库
I另一个存储库
它们都继承自 IRepository (如果这有帮助的话)
如何获取结构图以使用 IRegistryConvention 扫描仪来注册名为的具体类型
SQLMyRepository
SqlAnother存储库
我读过那篇文章,但它没有给我我所需要的东西。 AddAllTypesOf 针对 IRepositoryInterface 注册了所有具体类型,但我要求每个具体类型都针对具有等效命名的接口进行注册。
IE。
For<IMyRepository>().Use<SqlMyRepository>();
我还需要为测试存储库创建一些命名实例。
For<IMyRepository>().Use<TestMyRepository>().Named("Test");
这是我想出的方法,它似乎可以满足我的需要。
public class SqlRepositoryConvention : StructureMap.Graph.IRegistrationConvention
{
public void Process(Type type, Registry registry)
{
// only interested in non abstract concrete types that have a matching named interface and start with Sql
if (type.IsAbstract || !type.IsClass || type.GetInterface(type.Name.Replace("Sql", "I")) == null)
return;
// Get interface and register (can use AddType overload method to create named types
Type interfaceType = type.GetInterface(type.Name.Replace("Sql","I"));
registry.AddType(interfaceType, type);
}
}
并实现如下
Scan(cfg =>
{
cfg.TheCallingAssembly();
cfg.Convention<SqlRepositoryConvention>();
});
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)