简体   繁体   English

Autofac 6 - 将装饰器注册为也实现另一个接口

[英]Autofac 6 - register a decorator as also implementing another interface

before Autofac 6, I could do something like:在 Autofac 6 之前,我可以执行以下操作:

builder.RegisterType<ResourceStorageParallelOperationsLimitDecorator>()
       .As(new DecoratorService(typeof(IResourceStorage)))
       .As<INetworkQueueMetrics>();

In short, I could tell Autofac that all IResourceStorage instances should be decorated by ResourceStorageParallelOperationsLimitDecorator and that this decorator also offered the service INetworkQueueMetrics .简而言之,我可以告诉 Autofac 所有IResourceStorage实例都应该由ResourceStorageParallelOperationsLimitDecorator装饰,并且这个装饰器还提供服务INetworkQueueMetrics

Now, in v6, if I do:现在,在 v6 中,如果我这样做:

builder.RegisterDecorator<ResourceStorageParallelOperationsLimitDecorator,
                          IResourceStorage>()

I cannot continue the call with .As<>(...) .我无法使用.As<>(...)继续通话。

And if I try to amend the registration of ResourceStorageParallelOperationsLimitDecorator like:如果我尝试修改ResourceStorageParallelOperationsLimitDecorator的注册,例如:

builder.RegisterDecorator<ResourceStorageParallelOperationsLimitDecorator,
                          IResourceStorage>();
builder.RegisterType<ResourceStorageParallelOperationsLimitDecorator>()
       .As<INetworkQueueMetrics>()
       .SingleInstance();

then this leads to INetworkQueueMetrics and IResourceStorage being resolved as two different instances of ResourceStorageParallelOperationsLimitDecorator , see the following test:那么这导致INetworkQueueMetricsIResourceStorage被解析为ResourceStorageParallelOperationsLimitDecorator的两个不同实例,请参见以下测试:

[Test]
public void INetworkQueueMetrics_resolves_to_the_same_instance_as_ResourceStorage()
{
  var container = BuildContainer();
  var resourceStorage = container.Resolve<IResourceStorage>();
  var networkMetrics = container.Resolve<INetworkQueueMetrics>();

  resourceStorage.Should().BeSameAs(networkMetrics); // <<<< FAILS
}

There are plenty of overrides for ContainerBuilder.RegisterDecorator() , maybe one of them would let me do what I need, but frankly their usage is not all that clear to me. ContainerBuilder.RegisterDecorator()有很多覆盖,也许其中一个可以让我做我需要的,但坦率地说,它们的用法对我来说并不是那么清楚。

Any idea how I can get the semantics I want with Autofac v6?知道如何使用 Autofac v6 获得我想要的语义吗?

What you're seeing is an intentional behavior.你看到的是一种有意的行为。 The point of a decorator is to decorate - it's basically a "value-add proxy."装饰器的重点是装饰——它基本上是一个“增值代理”。 A decorator, in and of itself, doesn't expose a whole separate service - its sole purpose is to wrap something else.装饰器本身并没有公开一个完整的单独服务——它的唯一目的是包装其他东西。 It only worked before somewhat accidentally - via a leaky abstraction in the way the fluent registration syntax functioned.它只是在偶然的情况下才起作用-通过流畅的注册语法运行方式中的泄漏抽象。 That leak got "plugged" with the latest version.该泄漏被最新版本“堵塞”。

If you need something to wrap a component and change the services exposed , that's an adapter and Autofac does have adapter support, too .如果您需要一些东西来包装组件并更改公开的服务那就是适配器Autofac 也确实支持适配器

And, of course, if you need something more general purpose (like "log every call made to this thing"), you can use type interceptors .当然,如果你需要更通用的东西(比如“记录对这个东西的每次调用”), 你可以使用类型拦截器

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM