You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Currently, Autofac's decorator feature requires either a fromKey or a toKey to be given. I'd like to suggest changing this so that neither key needs to be specified. In such a case, Autofac should just decorate all the registrations for the given service type.
If multiple decorators are registered, they should probably be chained in the order of registration to be consistent with how multiple registrations behave with Resolve<IEnumerable<...>>(). (More elaborate configuration of decorator dependencies might be desirable but is probably out of scope for this feature request.)
Motivation
In our projects, we often have the requirement of decorating instances registered by other modules in library assemblies. For example, module A in a library component would register an IService, module B would like to decorate that IService. These services are not registered with keys because they should be resolvable without giving a key.
public class ModuleA : Module
{
public override void Load(ContainerBuilder builder)
{
builder.Register(c => new DefaultService(c.Resolve<SomeParameterService>(), "some parameter", 42))
.As<IService>();
}
}
public class ModuleB : Module
{
public override void Load(ContainerBuilder builder)
{
builder.RegisterDecorator<IService>((ctx, decorated) => new ServiceDecorator(decorated), fromKey: null);
}
}
At the moment, this does not work, it gives an ArgumentException:
The service IService cannot be both the adapter's from and to parameters - these must differ.
This means that we cannot use the RegisterDecorator feature in our scenario. Instead, we need to copy the registration from ModuleA to ModuleB. (Since ModuleA is in a library, we can't just refactor the registration code into a common abstraction.)
public class ModuleB : Module
{
public override void Load(ContainerBuilder builder)
{
builder.Register((ctx, decorated) => new ServiceDecorator(new DefaultService(c.Resolve<SomeParameterService>(), "some parameter", 42)))
.As<IService>();
}
}
This is cumbersome and reduces maintainability, and it doesn't work for collections (IService will now have two registrations rather than just the decorated one).
The text was updated successfully, but these errors were encountered:
We're pushing to enhance decorator syntax and features, which will hopefully resolve this issue. I've created a larger meta-issue for tracking that at #880. In the meantime, I'll close this specific issue and hopefully we can handle everything at once.
In our team we introduced an extension method RegisterDecorator() which uses the .Activating handler to decorate the instance. Maybe that's a solution for you @fschmied ?
Currently, Autofac's decorator feature requires either a
fromKey
or atoKey
to be given. I'd like to suggest changing this so that neither key needs to be specified. In such a case, Autofac should just decorate all the registrations for the given service type.If multiple decorators are registered, they should probably be chained in the order of registration to be consistent with how multiple registrations behave with
Resolve<IEnumerable<...>>()
. (More elaborate configuration of decorator dependencies might be desirable but is probably out of scope for this feature request.)Motivation
In our projects, we often have the requirement of decorating instances registered by other modules in library assemblies. For example, module A in a library component would register an
IService
, module B would like to decorate thatIService
. These services are not registered with keys because they should be resolvable without giving a key.At the moment, this does not work, it gives an
ArgumentException
:This means that we cannot use the
RegisterDecorator
feature in our scenario. Instead, we need to copy the registration fromModuleA
toModuleB
. (SinceModuleA
is in a library, we can't just refactor the registration code into a common abstraction.)This is cumbersome and reduces maintainability, and it doesn't work for collections (
IService
will now have two registrations rather than just the decorated one).The text was updated successfully, but these errors were encountered: