public class CachingInterceptor : IInterceptor
{
public void Intercept(IInvocation invocation)
{
// code comes here....
}
}
public class Business : IBusiness
{
public void Add(string a)
{
var t= GetAll();
// code comes here....
}
[CacheAttribute]
public string GetAll()
{
// code comes here....
}
}
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With