Open-generic registrations can not be decorated
See original GitHub issueLet me start by saying that I am quite amazed to see what you’ve been able to build on top of the DI abstraction. The omission of being able to apply decorators (and batch-registration) makes the built-in container IMO a worthless tool for any reasonably sized code base.
That said, I stumbled upon a limitation of your current implementation. Although you’re able to apply open-generic decorators to closed-generic registrations, it seems impossible to apply open-generic decorators to open-generic registrations.
This might very well be a limitation of the API you are building upon, but I’ll let you be the judge of that.
Here’s a failing unit test:
[Fact]
public void CanDecorateOpenGenericTypeBasedOnOpenGenericRegistration()
{
var provider = ConfigureProvider(services =>
{
services.AddTransient(typeof(IQueryHandler<,>), typeof(QueryHandler<,>));
services.Decorate(typeof(IQueryHandler<,>), typeof(LoggingQueryHandler<,>));
services.Decorate(typeof(IQueryHandler<,>), typeof(TelemetryQueryHandler<,>));
});
var instance = provider.GetRequiredService<IQueryHandler<MyQuery, MyResult>>();
var telemetryDecorator = Assert.IsType<TelemetryQueryHandler<MyQuery, MyResult>>(instance);
var loggingDecorator = Assert.IsType<LoggingQueryHandler<MyQuery, MyResult>>(
telemetryDecorator.Inner);
Assert.IsType<MyQueryHandler>(loggingDecorator.Inner);
}
Issue Analytics
- State:
- Created 6 years ago
- Reactions:3
- Comments:8 (3 by maintainers)
Top Results From Across the Web
c# - AutoFac - Registering a decorator for some of an open ...
I only want to "Decorate" the implementations (of MyInterface ) that have a particular attribute. So all implementations of MyInterface that ...
Read more >Advanced Scenarios — Simple Injector 5 documentation
Simple Injector can handle open-generic types, closed-generic types and ... Because you register a collection, you can no longer call container.
Read more >Adapters and Decorators - Autofac's documentation!
Decoration can be conditional. A context object is provided to registrations that allows you to decide whether or not to apply the decorator:....
Read more >Upcoming decorator enhancements in Autofac 4.9
Implementation types are registered as normal and do not require any kind of key or other hint indicating that they will be decorated....
Read more >Registering a generic decorator of a Consumer using Autofac
Is it possible to register open generic types using Autofac for MT? ... decorate it, just create your own IConsumerFactory<>, which can then...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@akovanev, Microsoft didn’t add any feature in .NET 5 or .NET 6 to MS.DI that would allow @khellang to implement this feature in Scrutor. That status of this feature request is, therefore, unchanged.
I don’t know if my workaround will be helpful to anyone, as it has a few significant disadvantages, but let me put the link to the project here CqsDecorator.