给定具有不匹配名称的具体类和接口
Harvester_JohnDeere_Parsley : AbstractMachine, IParsleyHarvester
Harvester_NewHolland_Sage : AbstractMachine, ISageHarvester
Harvester_Kubota_Rosemary : AbstractMachine, IRosemaryHarvester
接口有一个共同的父
IParsleyHarvester : ISpiceHarvester
ISageHarvester : ISpiceHarvester
IRosemaryHarvester : ISpiceHarvester
如何配置StructureMap,以便可以将I ... Harvester的实例注入到构造函数中,例如。
public ParsleyField(IParsleyHarvester parsleyHarvester)
无需在注册表中单独配置每一对? e.g。
For<ISageHarvester>().Use<Harvester_NewHolland_Sage>();
我试过扫描
Scan(x =>
{
x.AssemblyContainingType<Harvester_Kubota_Rosemary>();
x.AddAllTypesOf<ISpiceHarvester>();
但是I ... Harvester接口没有被映射。
谢谢!
修改:
这两个答案都有效。 @ jeroenh的优点是可以添加保护条款以排除类(无论出于何种原因)。以下是基于@Mac对this question的回答的示例。
public class HarvesterConvention : StructureMap.Graph.IRegistrationConvention
{
public void Process(Type type, Registry registry)
{
// only interested in non abstract concrete types
if (type.IsAbstract || !type.IsClass)
return;
// Get interface
var interfaceType = type.GetInterface(
"I" + type.Name.Split('_').Last() + "Harvester");
if (interfaceType == null)
throw new ArgumentNullException(
"type",
type.Name+" should implement "+interfaceType);
// register (can use AddType overload method to create named types
registry.AddType(interfaceType, type);
}
}
用法:
Scan(x =>
{
x.AssemblyContainingType<Harvester_Kubota_Rosemary>();
x.Convention<HarvesterConvention>();
x.AddAllTypesOf<ISpiceHarvester>();
答案 0 :(得分:1)
StructureMap不了解您的约定。您需要通过添加自定义注册约定来告诉它。实现IRegistrationConvention接口,并将约定添加到汇编扫描器:
Scan(x =>
{
x.Convention<MyConvention>();
}
答案 1 :(得分:1)
我根据@ Kirschstein对this question的回答改编了一个解决方案。
Scan(x =>
{
x.AssemblyContainingType<Harvester_Kubota_Rosemary>();
x.AddAllTypesOf<ISpiceHarvester>()
.NameBy(type => "I" + type.Name.Split('_').Last() + "Harvester");
定义将具体类名称转换为其接口名称以进行查找的方法。