城堡windsor拦截器在另一种方法调用的方法上

Mur*_*buk 6 c# aop castle-windsor interceptor aspect


拦截器

public class CachingInterceptor : IInterceptor
{

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

}
Run Code Online (Sandbox Code Playgroud)

业务层

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

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

        }

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

        }

}
Run Code Online (Sandbox Code Playgroud)

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

            }

  }
Run Code Online (Sandbox Code Playgroud)

添加方法调用GetAll方法.如果我直接调用GetAll方法,缓存工作.如果Add方法调用GetAll方法,则缓存不起作用.

谢谢你的帮忙.

Jak*_*kob 4

接口代理是通过包装代理目标对象来创建的,因此对于接口来说这是不可能的。

您可以拦截对相同对象的调用,但仅限于类代理(前提是该方法是虚拟的)。请参阅类似问题的答案。

您还可以尝试以不同的方式构建代码,将需要缓存的逻辑移动到可以在不使用其自身功能的情况下进行缓存的服务。