2014-09-09 2 views
2

안녕하세요. 화합 차단을 사용하려고합니다. (화합 컨테이너를 사용하고 싶지 않습니다.) 런타임에서 구성 할 수는 있지만 구성에서 구성하는 방법을 모릅니다.컨테이너를 사용하지 않고 구성에서 유니티 가로 채기

내 코드 :

public interface ICalculator 
{ 
    int Add(int first, int second); 

    int Multiply(int first, int second); 
} 

동작 :

internal class LogBehavior : IInterceptionBehavior 
    { 
     public IMethodReturn Invoke(IMethodInvocation input, GetNextInterceptionBehaviorDelegate getNext) 
     { 
      // My Code 
      IMethodReturn result = getNext()(input, getNext); 
      return result; 
     } 

     public IEnumerable<Type> GetRequiredInterfaces() 
     { 
      return Type.EmptyTypes; 
     } 

     public bool WillExecute { 
      get { return true; } 
     } 
    } 

그리고 이것은 내가이가 작동

public static void Main(string[] args) 
    { 

     var calculator = new Calculator(); 
     var calculatorProxy = Intercept.ThroughProxy<ICalculator>(calculator, 
      new InterfaceInterceptor(), new[] { new LogBehavior() }); 
     Console.WriteLine(calculatorProxy.Add(2, 2)); 

     Console.ReadKey(); 
    } 

를 호출하고 방법이다. 구성 파일에서이 파일을 구성해야합니다. 도와주세요

+0

해당 URL에 무엇을하려 했습니까 : http://msdn.microsoft.com/en-us /library/ff660932%28v=PandP.20%29.aspx – samy

답변

0

컨테이너가 없으면 직접 프록시 인스턴스를 만들어야합니다. 전화 번호는 Intercept.ThroughProxy<>입니다. 다른 컨테이너를 사용하는 경우 확장하여 구성을 읽고 이에 따라 처리 할 수 ​​있습니다.

는하지만 그냥 .NET/연합이 구성 파일을 읽고 단지 new Calculator()을하고 Calculator는 프록시와 설정 파일에 정의 된 인터셉터로 차단됩니다 수 있습니다 마법 없다. new은이를 처리하기 위해 확장 할 수 없으므로 항상 구성 요소가 있으므로 ICalculator의 인스턴스를 요청해야합니다. 그런 다음 프록시가 필요한지와 어떤 인터셉터가 필요한지를 확인합니다.

물론 예외가 있습니다. 직조 (Fody, PostSharp)를 사용하면 동적 프록시를 사용하지 않고도 AOP를 수행 할 수 있습니다. 그리고 나서 실제로는 new Calculator()을 수행 할 수 있습니다. 핵심 구현뿐만 아니라 측면을 가질 것입니다 ...