Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

castle windsor interceptor on method that is calling by another method


Interceptor

public class CachingInterceptor : IInterceptor
{

    public void Intercept(IInvocation invocation)
    {
       // code comes here.... 
    }

}

Business Layer

public class Business : IBusiness
{
     public void Add(string a)
        {

             var t= GetAll();             
             // code comes here.... 

        }

     [CacheAttribute]
     public string GetAll()
        {
             // code comes here.... 

        }

}

Class

public class JustForTest
{

     public JustForTest(IBusiness business)
            {

               //if GetAll is invoked directly caching works fine.
               business.GetAll();

               //if GetAll is invoked over Add method caching doesn't work.
               business.Add();                      

            }

  }

add method calls GetAll method. If I invoke GetAll method directly, caching works. If Add method calls GetAll Method, caching doesn't work.

Thank You for helping.

like image 707
Murat Cabuk Avatar asked Jul 07 '15 08:07

Murat Cabuk


2 Answers

Interface proxies are created by wrapping proxy target object so with interfaces this is not possible.

You can intercept calls on the same objects, but only for class proxy (provided the method is virtual). See answer to a similar question.

You could also try to structure your code differently, move logic that needs to be cached to services that can be cached without using it's own functions.

like image 104
Jakob Avatar answered Nov 14 '22 23:11

Jakob


The problem here is that the line

var t= GetAll();

is inside the class Business. It can be more clearly written as

var t = this.GetAll();

this is not an intercepted/wrapped instance.

Try dividing the responsibilities of the Business class as suggested here and here

like image 25
qujck Avatar answered Nov 14 '22 23:11

qujck