温莎城堡 - 指定所有拦截器
container
.Register
(
AllTypes
.FromAssemblyContaining<TestInterceptor>()
.BasedOn<IInterceptor>()
.Where(t => t.Name.EndsWith("Interceptor"))
.WithService.AllInterfaces()
.Configure(c => c.LifeStyle.Transient),
Component
.For<IRepository>()
.ImplementedBy<LogRepository>()
.Interceptors
(
InterceptorReference.ForType<TestInterceptor>(),
InterceptorReference.ForType<TestTwoInterceptor>(),
InterceptorReference.ForType<TestThreeInterceptor>()
).Anywhere
);
有没有一种简单的方法来指定组件将使用所有拦截器,而不必指定每个拦截器?
container
.Register
(
AllTypes
.FromAssemblyContaining<TestInterceptor>()
.BasedOn<IInterceptor>()
.Where(t => t.Name.EndsWith("Interceptor"))
.WithService.AllInterfaces()
.Configure(c => c.LifeStyle.Transient),
Component
.For<IRepository>()
.ImplementedBy<LogRepository>()
.Interceptors
(
InterceptorReference.ForType<TestInterceptor>(),
InterceptorReference.ForType<TestTwoInterceptor>(),
InterceptorReference.ForType<TestThreeInterceptor>()
).Anywhere
);
Is there an easy way to specify that a component will use all interceptors instead of having to specify each one?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是一个有趣的问题。在 Fluent API 中没有开箱即用的方法来解决这个问题,主要是因为大多数时候您确实希望对要应用的拦截器以及应用的顺序进行精细控制。这是一种手术工具。
无论如何,如果您确定需要首先注册拦截器,然后获取它们的名称(通过container.Kernel.GetAssignableHandlers(typeof(IInterceptor)))并在循环中应用所有他们到组件。
That's an interesting problem. There's no out of the box way to solve it in the fluent API, mostly because most of the time you do want to have a fine grained control over which interceptors to apply, and in what order. This is a surgical tool.
Anyway, if you're sure you need to go this way register the interceptors first, then grab their names (via
container.Kernel.GetAssignableHandlers(typeof(IInterceptor))
) and in a loop apply all of them to the component.