温莎城堡 - 指定所有拦截器

发布于 2024-10-02 22:50:44 字数 771 浏览 0 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

幸福不弃 2024-10-09 22:50:44

这是一个有趣的问题。在 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文