How to apply decorators with ASP.NET Core Dependency Injection

scrutor
microsoft dependency injection decorator
asp net core dependency injection interception
decorator pattern with dependency injection
autofac decorator
net core dependency injection scan assemblies
decorator pattern vs dependency injection
asp net decorators

On an ASP.NET MVC 5 application I have the following StructureMap configuration:

cfg.For(typeof(IRequestHandler<,>)).DecorateAllWith(typeof(MediatorPipeline<,>));

Does anyone know how to do this configuration with ASP.NET Core IOC?

The out of the box IoC container doesn't support decorate pattern or auto discovery, which is "by design" as far as I know.

The idea is to provide a basic IoC structure that works out of the box or where other IoC containers can be plugged in to extend the default functionality.

So if you need any advanced features (support for a specific constructor, auto-registering of all types which implement an interface or inject decorators and interceptors) you have to either write it yourself or use an IoC container which offers this functionality.

How to apply decorators with ASP.NET Core Dependency Injection , The out of the box IoC container doesn't support decorate pattern or auto discovery, which is "by design" as far as I know. The idea is to provide� Use the dependency injection framework to instantiate the decorator from the services registered so far. //Decorator class as above public static void ConfigureServices ( IServiceCollection services ) { var wrappedServiceDescriptor = services .

This workaround doesn't apply the decorator to all instances of a type but uses extension methods to abstract the decorator logic into another file.

Defining the decorator structure like:

public static class QueryHandlerRegistration
{
    public static IServiceCollection RegisterQueryHandler<TQueryHandler, TQuery, TResult>(
        this IServiceCollection services) 
        where TQuery : IQuery<TResult>
        where TQueryHandler : class, IQueryHandler<TQuery, TResult>
    {
        services.AddTransient<TQueryHandler>();
        services.AddTransient<IQueryHandler<TQuery, TResult>>(x =>
            new LoggingDecorator<TQuery, TResult>(x.GetService<ILogger<TQuery>>(), x.GetService<TQueryHandler>()));
        return services;
    }
}

And calling it like:

services.AddMvc();
// Add application services.
services.AddTransient<IEmailSender, AuthMessageSender>();
services.AddTransient<ISmsSender, AuthMessageSender>();

services.RegisterQueryHandler<FindThingByIdQueryHandler, FindThingByIdQuery, Thing>();

There's also the Scrutor package being worked on.

Beyond Basics - ASP.Net Core Using the Decorator , Registering The Decorators To register the decorators we need to make sure we have the base underlying implementation registered with the IServiceCollection first and then we can apply further decorators. services. AddScoped<IRestaurantData, SqlRestaurantData>(); // Add the decorator implementations after the services. Now we've got to register this new implementation with the dependency injection framework. Scrutor. Out of the box the Microsoft Dependency Injection framework does not support the Decorator Pattern. Due to this small limitation a nuget package has been made by Kristian Hellang called Scrutor which provides the required functionality.

In my blogpost I described how a relatively simple extension method can solve this problem easily. Here is an example from that post which shows how decorator configuration may look like:

services.AddDecorator<IEmailMessageSender, EmailMessageSenderWithRetryDecorator>(decorateeServices =>
    {
        decorateeServices.AddScoped<IEmailMessageSender, SmtpEmailMessageSender>();
    });

Decorators in .NET Core with Dependency Injection , Ideally we would like to be able to use this pattern with dependency injection without having to put too many constraints on either the decorator or� So now I'm not sure of the "proper" way of actually creating a decorator with dependency injection. I based my decorator on Mark Seemann's answer here. In his example, he is newing up several objects that get passed into the class. This is how my it "works"* snippet works. However, I think I have missed a fundamental step.

Use Scrutor. Just install the nuget package and then do the followng.

services.AddSingleton<IGreeter, Greeter>();
services.Decorate<IGreeter, GreeterLogger>();
services.Decorate<IGreeter, GreeterExceptionHandler>();

The order is important. In the above, GreeterLogger decorates Greeter. And GreeterExceptionHandler decorates GreeterLogger.

If you need more info, take a look at this and this.

.NET Core Dependency Injection —Decorator Workaround, I've been learning about Domain Driven Design and applying Command Query Separation (CQS) at an architectural level. The Decorator� Manually creating decorators with the ASP.NET Core DI container. As I've discussed previously, the ASP.NET Core DI container is designed to provide only those features required by the core ASP.NET Core framework. Consequently, it does not provide advanced capabilities like assembly scanning, convention-based registration, diagnostics, or

Command decorators with ASP.NET Core Dependency Injection, There is no pretty way to register your decorators if you use built-in dependency injection in .NET Core. So let's create decorator builder which� Dependency Injection in a Console Application. In the previous section, you implemented Dependency Injection for an ASP.NET Web application. You followed a few steps to register and get your dependencies automatically resolved by the built-in IoC Container.

Decorator pattern with C# and DotNet Core, What is decorator pattern and how to use it in your core applications If you are using the out of box dependency injection for ASP.NET Core� Support for dependency injection is built into ASP.Net Core, Microsoft’s open source, cross platform, lean, and modular framework for building high performance, scalable web applications.

Exploring ASP.NET Core Fundamentals, Various popular DI (Dependency Injection) containers such as Ninject and Autofac have different code syntaxes to implement decorator pattern. Implementing a� ASP.NET Core framework contains simple out-of-the-box IoC container which does not have as many features as other third party IoC containers. If you want more features such as auto-registration, scanning, interceptors, or decorators then you may replace built-in IoC container with a third party container.

Comments
  • The Scrutor mention was very useful. Nice library there. It's impressive that they managed to create the entire mechanism using standard registration for descriptor classes.
  • Note that Scrutor doesn't curently support calling Dispose on decorated objects when a DI scope ends. I filed an issue on GitHub for this, hope they will fix it eventually: github.com/khellang/Scrutor/issues/91