【发布时间】:2019-05-01 09:46:20
【问题描述】:
我有以下继承,我想用 Simple Injector 装饰(重命名的东西使其更具可读性):
interface IGetData<T,U> { }
interface ICustomerService : IGetData<Customer, Guid> { }
class CustomerServiceImpl : ICustomerService { }
我有一个用于IGetData<T,U> 的装饰器,名为GetDataDecorator,我还有另一个用于ICustomerService 的装饰器,名为CustomerServicePermissionDecorator。我的目标是为CustomerServiceImpl 提供两个(链接的)装饰器,一个基于IGetData<T,U> 接口,一个基于ICustomerService 接口。我在启动时注册了两个装饰器:
container.RegisterDecorator<ICustomerService, CustomerServicePermissionDecorator>();
container.RegisterDecorator(typeof(IGetData<,>), typeof(GetDataDecorator<,>));
第一次注册工作正常,CustomerServiceImpl 中的断点表明那里的方法是从CustomerServicePermissionDecorator 调用的。但是,GetDataDecorator 方法永远不会执行。
我想这是我的一个误解 - 我做错了什么?
【问题讨论】:
-
我对你的问题有点困惑,我可以建议你包含足够的代码来让复制粘贴重现问题吗?然后更好地解释一下“用简单的注射器装饰”是什么意思?你想让接口传递的Instance装饰在容器的实例上吗?还是什么?
-
我想你是从容器中请求
ICustomerService?您仅将容器配置为使用CustomerServicePermissionDecorator装饰ICustomerService- 您尚未将ICustomerService配置为由GetDataDecorator<,>装饰。 -
@qujck 我认为
ICustomerService会在实现IGetData<T,U>时被隐式修饰——事实并非如此,是吗? -
@MortenBork 该实例应该使用继承接口的两个装饰器来装饰(通过 SimpleInjector)。我会尝试创建一个更详细的示例。
-
@PhilReif SimpleInjector 不会做出这样的假设
标签: c# dependency-injection decorator simple-injector