question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Open-generic registrations can not be decorated

See original GitHub issue

Let 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:closed
  • Created 6 years ago
  • Reactions:3
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
dotnetjunkiecommented, Jun 22, 2021

Any updates here?

@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.

1reaction
akovanevcommented, Jun 22, 2021

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found