2010-01-09 4 views
1

나는이 게시물의 코드를 Event Driven Architecture (매우 흥미 롭습니다)에 표시하려고합니다. 그의 IOC 컨테이너는 Unity이지만 컨테이너 맵을 사용하여이 작업을 수행하고 싶습니다.구조지도와의 일치

그의 코드는 다음과 같습니다

public class SubscriptionService : ISubscriptionService 
{ 
    public static void Add<T>() 
    { 
     var consumerType = typeof(T); 

     consumerType.GetInterfaces() 
      .Where(x => x.IsGenericType) 
      .Where(x => x.GetGenericTypeDefinition() == typeof (IConsumer<>)) 
      .ToList().ForEach(x => ObjectFactory.Inject(consumerType, x));         
    } 

    public IEnumerable<IConsumer<T>> GetSubscriptions<T>() 
    { 
     var consumers = ObjectFactory.GetAllInstances(typeof(IConsumer<T>)); 
     return consumers.Cast<IConsumer<T>>(); 
    } 
} 

내가 분명히 구조지도 너무 익숙하지 않다 :

public class EventSubscriptions : ISubscriptionService 
{ 
    public static void Add<T>() 
    { 
     var consumerType = typeof(T); 

     consumerType.GetInterfaces() 
        .Where(x => x.IsGenericType) 
        .Where(x => x.GetGenericTypeDefinition() == typeof(IConsumer<>)) 
        .ToList() 
        .ForEach(x => IoC.Container.RegisterType(x, 
                  consumerType, 
                  consumerType.FullName)); 
    } 

    public IEnumerable<IConsumer<T>> GetSubscriptions<T>() 
    { 
     var consumers = IoC.Container.ResolveAll(typeof(IConsumer<T>)); 
     return consumers.Cast<IConsumer<T>>(); 
    } 
} 

내가 작동하지 않는 것 다음이있다. 제가 잘못하고있는 것에 관한 어떤 링크 나 설명은 정말로 감사 할 것입니다.

public static void ConfigureStuctureMap() 
     { 
      ObjectFactory.Initialize(x => 
      {  
       x.Scan(y => 
       { 
        y.Assembly("Domain");   
        y.Assembly("Website"); 
        y.AddAllTypesOf(typeof(IConsumer<>)); 
        y.WithDefaultConventions(); 
       }); 
      }); 
     } 

답변

3

: - :

업데이트 내가 가지고있는 응용 프로그램 시작에라고 내 부트 스트랩 클래스의 다음

public class SubscriptionService : ISubscriptionService 
{ 
    public IEnumerable<IConsumer<T>> GetSubscriptions<T>() 
    { 
     var consumers = ObjectFactory.GetAllInstances(typeof(IConsumer<T>)); 
     return consumers.Cast<IConsumer<T>>(); 
    } 
} 

그리고 헤닝의 대답에서

, 내가 함께 끝났다 비록 내가 구조지도 전문가는 아니지만, 나는 당신이 다른 방식으로 그것을 할 수 있다고 믿는다.

구조체 맵에는 주어진 인터페이스에 대해 지정된 어셈블리를 검색하고 모든 구현을 자동으로 등록하는 기능이 있습니다. 우리는 현재 진행중인 프로젝트에서 그렇게하며 정말 훌륭하게 작동합니다.

나는 우리가 사용하는 정확한 코드를 기억하지 않는다, 그러나 당신은 조립 스캔 ITypeScanner 인터페이스를 구현

http://structuremap.sourceforge.net/ScanningAssemblies.htm

0

빌드 정의 TypeScanner 클래스를 문서를 확인할 수 있습니다. 레지스트리 후

public class EventSubConventionScanner : ITypeScanner 
{ 
    public void Process(Type type, PluginGraph graph) 
    { 
     Type interfaceType = type.FindInterfaceThatCloses(typeof(IConsumer<>)); 

     if (interfaceType != null) 
     { 
      graph.AddType(interfaceType, type); 
     } 
    } 
} 

, 또는 일상 쓰기 초기화 :

Scan(x => 
     { 
      x.With<EventSubConventionScanner>(); 
     });