我正在开发一个使用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
。我在实际的解决方案中有很多类,所以如果可能的话我想使用一个拦截器选择器来做到这一点,以避免手动应用拦截器的大量重复代码。
这就是我被卡住的地方。显然,我不想将它硬编码到IModelInterceptorsSelector
中,因为每当选择装饰器或它们的应用顺序发生变化时,需要提供拦截器的特定对象可能会有所不同。在那一点上,我确信坚持手动注册会更容易。而且我无法弄清楚任何可以完成这项工作的惯例。
那么,可以吗?或者我应该接受我的命运,将.Interceptors(InterceptorReference.ForType<. . .
放入我的剪贴板,然后开始敲打一些IWindsorInstallers?