2010-11-22 3 views
1

일치하는 이름을 가진 구체적인 클래스와 인터페이스 StructureMap은 인터페이스 이름이 일치하지 않는 구체적인 클래스를 구성합니다.

Harvester_JohnDeere_Parsley : AbstractMachine, IParsleyHarvester 
Harvester_NewHolland_Sage : AbstractMachine, ISageHarvester 
Harvester_Kubota_Rosemary : AbstractMachine, IRosemaryHarvester 

인터페이스의 공통 부모

IParsleyHarvester : ISpiceHarvester 
ISageHarvester : ISpiceHarvester 
IRosemaryHarvester : ISpiceHarvester 

어떻게 구조 파일을 구성하여 I ... Harvester가 될 수 있습니까? 생성자에 주입

public ParsleyField(IParsleyHarvester parsleyHarvester) 

레지스트리에서 각 쌍을 개별적으로 구성 할 필요없이? 예 :

For<ISageHarvester>().Use<Harvester_NewHolland_Sage>(); 

Scan(x => 
{ 
    x.AssemblyContainingType<Harvester_Kubota_Rosemary>(); 
    x.AddAllTypesOf<ISpiceHarvester>(); 

을 스캔 해 보았습니다.하지만 ... Harvester 인터페이스가 매핑되지 않았습니다.

고마워!

편집 :

두 답변 모두 작동합니다. @ jeroenh의 장점은 어떤 이유로 든 클래스를 제외하기 위해 guard 절을 추가 할 수 있다는 것입니다. 다음은 this question에 대한 @Mac의 대답을 기반으로 한 예입니다.

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>(); 

답변

1

StructureMap은 국제 대회에 대해 알고하지 않습니다. 사용자 정의 등록 규칙을 추가하여이 사실을 알릴 필요가 있습니다. IRegistrationConvention 인터페이스를 구현하고, 조립 스캐너에 규칙을 추가 :

Scan(x => 
    { 
    x.Convention<MyConvention>(); 
    } 
1

내가 this question에 Kirschstein의 대답 @에서 솔루션을 적용.

Scan(x => 
{ 
    x.AssemblyContainingType<Harvester_Kubota_Rosemary>(); 
    x.AddAllTypesOf<ISpiceHarvester>() 
    .NameBy(type => "I" + type.Name.Split('_').Last() + "Harvester"); 

콘크리트 클래스의 이름을 조회 목적으로 인터페이스 이름으로 변환하는 방법을 정의합니다.

관련 문제