Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/308.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 仅拦截最外层的装饰器_C#_Castle Windsor - Fatal编程技术网

C# 仅拦截最外层的装饰器

C# 仅拦截最外层的装饰器,c#,castle-windsor,C#,Castle Windsor,我正在从事一个使用Castle Windsor的项目,该项目混合使用了装饰器和动态拦截来向根实现添加功能 例如,假设我正在处理数据访问层的一部分,该部分需要进行一些缓存和通用错误记录,如下所示: interface IRepository { /* . . . */ } // base implementation class SqlRepository : IRepository { /* . . . */ } // decorator to implement

我正在从事一个使用Castle Windsor的项目,该项目混合使用了装饰器和动态拦截来向根实现添加功能

例如,假设我正在处理数据访问层的一部分,该部分需要进行一些缓存和通用错误记录,如下所示:

interface IRepository 
{ 
    /* . . . */ 
}

// base implementation
class SqlRepository : IRepository
{ 
    /* . . . */ 
}

// decorator to implement caching
class CachingRepository : IRepository
{ 
    private readonly IRepository _base;

    /* . . . */ 
}

// interceptor to implement generic logging
class LoggingInterceptor : IInterceptor
{ 
    /* . . . */ 
}
到目前为止还不错,只是我不确定如何最好地实现切入点。我希望确保拦截器不会应用于装饰器链中的每个对象。相反,它应该只应用于最外层的装饰器。在这种情况下,它应该应用于
CachingRepository
,而不是
SqlRepository
。在实际的解决方案中,我有很多类,所以如果可能的话,我想使用拦截器选择器来实现这一点,以避免手动应用拦截器的大量重复代码

这就是我被卡住的地方。显然,我不想将其硬编码到
imodeinterceptorsselector
,因为需要提供拦截器的特定对象在装饰器的选择或应用它们的顺序发生变化时可能会有所不同。在这一点上,我相信坚持手动注册会容易得多。我想不出哪种惯例能起作用

那么,这能做到吗?或者我应该接受我的命运,获取
.Interceptors(InterceptorReference.ForType