gpt4 book ai didi

c# - 由另一个方法调用的方法上的 caSTLe windsor 拦截器

转载 作者:太空狗 更新时间:2023-10-29 21:22:01 25 4
gpt4 key购买 nike


拦截器

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 方法调用 GetAll 方法。如果我直接调用 GetAll 方法,则缓存有效。如果 Add 方法调用 GetAll 方法,则缓存不起作用。

谢谢你的帮助。

最佳答案

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

您可以拦截对相同对象的调用,但仅限于类代理(前提是该方法是虚拟的)。查看类似 question 的答案.

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

关于c# - 由另一个方法调用的方法上的 caSTLe windsor 拦截器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31262970/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com