2013-09-25 1 views
2

Ninject와 AOP를 사용하여 캐싱을 수행하고 있습니다. 내 저장소의 모든 메서드에 적용 할 수있는 특성이 있고 BeforeInvoke에 캐시 된 개체가있는 경우 캐시 된 개체를 반환하고 AfterInvoke는 캐시 된 개체를 만듭니다. 이 모든 위대한 작동하지만 초기 메서드를 호출하는 중지하는 방법을 알아낼 수 없습니다. 즉 캐시 된 개체를 반환하는 대신 intyercepted 메서드를 호출하는 경우. 내 인터셉터는 여기에 있습니다 :AOP Ninject Stop Intercepted Method 호출 중임

else 문에서 나는 명확하게 호출 방법 전화 말하고 있지 않다하더라도
public class CacheInterceptor : SimpleInterceptor 
{ 
    protected override void BeforeInvoke(IInvocation invocation) 
    { 
     Type returnType = invocation.Request.Method.ReturnType; 
     string cacheKey = CacheKeyBuilder.GetCacheKey(invocation, serializer); 
     object cachedValue = cache.Get(cacheKey); 
     if (cachedValue == null) 
     { 
      invocation.Proceed(); 
     } 
     else 
     { 
      object returnValue = serializer.Deserialize(returnType, cachedValue); 
      invocation.ReturnValue = returnValue; 
      returnedCachedResult = true; 
     } 
    } 
} 

'invocation.Proceed를();' 그것은 여전히 ​​그것을 호출합니다. 어떻게하면 ninject가 호출과 함께 반환하도록 지시할까요? 반환 값? 그는 당신이 이전 또는 실제 메서드 호출 후 작업을 수행 할 가장 일반적인 시나리오에 대한 기본 클래스로 의미하기 때문에

+0

당신이 당신의 인터셉터가 호출되고 있는지 확인 있습니까? 디버거에서 단계별로 진행할 수 있습니까? 인터셉터가 호출되고 예 –

+0

, 나는 invocation.ReturnValue = ReturnValue를 볼 수 있습니다; setbut 되 고 다음 메서드를 호출합니다. – Joshy

답변

5

이 경우 SimpleInterceptor을 사용할 수 없습니다. 또한 Proceed으로 전화를 걸 수 없습니다. 대신 IInterceptor 인터페이스를 구현하고 코드를 Intercept 방법에 넣으십시오.

하지만 실제 메서드가 호출되는 것을 방지 할 수 있도록 아마 우리는 향후 버전에서 SimpleInterceptor을 확장해야합니다 :

public abstract class SimpleInterceptor : IInterceptor 
{ 
    private bool proceedInvocation = true; 

    /// <summary> 
    /// Intercepts the specified invocation. 
    /// </summary> 
    /// <param name="invocation">The invocation to intercept.</param> 
    public void Intercept(IInvocation invocation) 
    { 
     BeforeInvoke(invocation); 
     if (proceedInvocation) 
     { 
      invocation.Proceed(); 
      AfterInvoke(invocation); 
     } 
    } 

    /// <summary> 
    /// When called in BeforeInvoke then the invokation in not proceeded anymore. 
    /// Or in other words the decorated method and AfterInvoke won't be called anymore. 
    /// Make sure you have assigned the return value in case it is not void. 
    /// </summary> 
    protected void DontProceedInvokation() 
    { 
     this.proceedInvocation = false; 
    } 

    /// <summary> 
    /// Takes some action before the invocation proceeds. 
    /// </summary> 
    /// <param name="invocation">The invocation that is being intercepted.</param> 
    protected virtual void BeforeInvoke(IInvocation invocation) 
    { 
    } 

    /// <summary> 
    /// Takes some action after the invocation proceeds. 
    /// </summary> 
    /// <param name="invocation">The invocation that is being intercepted.</param> 
    protected virtual void AfterInvoke(IInvocation invocation) 
    { 
    } 
} 
+0

Brilliant, Thankyou! – Joshy

관련 문제