StructureMap 确实允许您通过扫描程序集并应用约定将接口连接到类型来以编程方式执行此操作。这是一个例子:
public class RepositoryRegistry : StructureMap.Configuration.DSL.Registry
{
public RepositoryRegistry()
{
Scan(s =>
{
s.AssemblyContainingType<ApplicationRepository>();
s.Convention<TypeNamingConvention>();
});
}
}
和:
public class TypeNamingConvention : IRegistrationConvention
{
public void Process(Type type, Registry registry)
{
Type interfaceType = type.GetInterfaces()
.ToList()
.Where(t => t.Name.ToLowerInvariant().Contains("i" + type.Name.ToLowerInvariant()))
.FirstOrDefault();
if (interfaceType != null)
{
registry.AddType(interfaceType, type);
}
}
}
并且您在初始化时调用注册表,如下所示:
ObjectFactory.Initialize(x => x.Scan(s =>
{
s.TheCallingAssembly();
s.LookForRegistries();
}));
This convention just assumes the standard that your type matches the interface + "I". Hopefully that gives you enough to go on.