2013-07-14 1 views
2

초보자이며 Autofac입니다. 누구든지 컨테이너에서 사용하는 방법을 알고 있습니까? 모듈에서 해결 하시겠습니까?컨테이너에서 사용하는 방법. 모듈에서 해결 하시겠습니까?

public class MyClass 
{ 
    public bool Test(Type type) 
    { 
     if(type.Name.Begin("My")) return true; 
     return false; 
    } 
} 

public class MyModule1 : Autofac.Module 
{ 
    protected override void AttachToComponentRegistration(IComponentRegistry componentRegistry, IComponentRegistration registration) 
    { 
     var type = registration.Activator.LimitType; 
     MyClass my = container.Resolve<MyClass>(); //How to do it in Module? 
     my.Test(type); 
     ... 
    } 
} 

모듈을 컨테이너로 가져 오는 방법은 무엇입니까?

+0

달성하려는 목표는 무엇입니까? 왜 당신은'AttachToComponentRegistration'에서 이것을 필요로합니까? 왜냐하면'AttachToComponentRegistration'가 호출 될 때 Autofac은 여전히 ​​컨테이너를 "phase"로 만들고 있기 때문에 동일한 컨테이너를 사용하여 그 타입을 해결할 수 없기 때문입니다 ... – nemesv

+0

@nemesv에 동의합니다. 건물 단계에서 무언가를 해결하고 싶다면, 뭔가 잘못하고있는 것입니다. 우리가 성취하고자하는 바와 그 이유를 설명함으로써 의견을 보내주십시오. – Steven

+0

Autofac 목적은 동적 프록시를 제공합니다. 건물 단계에서 무언가를 해결하고 싶습니다. 건물 단계의 모든 코드가 동적으로 일부 클래스를 resolve 메소드로 호출하기를 바랍니다. (점점 더 다이나믹하게) 내 아이디어가 잘못 되었나요? IOC 자체는 동적 일 수 없습니까? – Flash

답변

0

모듈의 컨테이너에서 구성 요소를 확인할 수 없습니다. 그러나 각 구성 요소 해결 이벤트를 개별적으로 첨부 할 수 있습니다. 그래서 재미있는 구성 요소를 만날 때 무엇이든 할 수 있습니다. 개념적으로 당신은 구성 요소를 해결한다고 말할 수 있습니다.

public class MyModule : Autofac.Module 
{ 
    protected override void AttachToComponentRegistration(IComponentRegistry registry, IComponentRegistration registration) 
    { 
     registration.Activated += (sender, args) => 
     { 
      var my = args.Instance as MyClass; 
      if (my == null) return; 

      var type = args.Component.Activator.LimitType; 
      my.Test(type); 
      ... 
     }; 
    } 
} 
관련 문제