Castle windsor interceptor using a method that calls another method


interceptor

public class CachingInterceptor : IInterceptor { public void Intercept(IInvocation invocation) { // code comes here.... } } 

Business level

 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(); } } 

The add method calls the GetAll method. If I call the GetAll method directly, caching works. If the Add method calls the GetAll method, caching does not work.

Thanks for the help.

+5
source share
2 answers

Proxy interfaces are created by moving the proxy target, so this is not possible with interfaces.

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

You can also try to structure your code in different ways, move the logic that needs to be cached for services that can be cached without using their own functions.

+3
source

The problem is that the line

 var t= GetAll(); 

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

 var t = this.GetAll(); 

this not an intercepted / wrapped instance.

Try to separate the responsibilities of the Business class as suggested here and here.

+3
source

All Articles