2017-05-09 11 views
0

여러 규칙 호출을 처리 할 수있는 단일 규칙이 있으므로 규칙 클래스에 배치 된 사용자 정의 속성을 만들었습니다. 이 속성은 처리가 허용 된 이름을 나열합니다. 구조 맵에서 나는 사용자 정의 속성을 읽음으로써이 동일한 규칙을 여러 이름으로 등록하려고합니다.이름 목록을 사용하여 사용자 정의 속성을 기반으로 다중 인스턴스 등록

[RuleIdentifer(new string[] { "RunAction1","RunAction2","RunAction3" })] 

나는 MissingNamedInstanceIs 클래스를 사용하려했지만 양방향성 종속성 오류로 실행하려고했습니다.

x.AddAllTypesOf<Rules.IRule>().NameBy(t => t.Name); 

답변

0

때 앞서 내 자신을 만들어 : 전화 NameBy

_.For<Rules.IRule>().MissingNamedInstanceIs.ConstructedBy("Pull Rule by Name from Attribute",r => 
{ 
    return r.GetAllInstances<Rules.IRule>().FirstOrDefault<Rules.IRule>(r1 => 
    { 
        var dnAttribute = r1.GetType().GetCustomAttributes(typeof(RuleIdentifer), true).FirstOrDefault() as RuleIdentifer; 
        if (dnAttribute != null && dnAttribute.Names.Contains<string>(r.RequestedName)) return true; 
        return true; 
    }); 
}); 

이 있는가 검사 섹션에서이 작업을 수행 할 수있는 더 좋은 방법 : 다음은 스캔 후 용기의 창조에 배치되었습니다 RegistrationConvention. 예상대로 작동하십시오.

public class RuleAttributeConvention : IRegistrationConvention 
    { 
     public void ScanTypes(TypeSet types, Registry registry) 
     { 
      // Only work on concrete types 
      types.FindTypes(TypeClassification.Concretes | TypeClassification.Closed).Where(typ => typeof(Rules.IRule).IsAssignableFrom(typ)).ToList().ForEach(t => 
      { 
       var dnAttribute = t.GetCustomAttributes(typeof(RuleIdentifer), true).FirstOrDefault() as RuleIdentifer; 
       if (null == dnAttribute) return; 
       foreach (var nm in dnAttribute.Names) 
       {        
        registry.For<Rules.IRule>().Use(t.Name).Name = nm; 
       } 
      }); 
     } 
    }