ContainerRegisterDecoratorTService, TDecorator Method

Simple Injector
Ensures that the supplied TDecorator decorator is returned, wrapping the original registered TService, by injecting that service type into the constructor of the supplied TDecorator. Multiple decorators may be applied to the same TService. By default, a new TDecorator instance will be returned on each request (according the Transient lifestyle), independently of the lifestyle of the wrapped service.

Namespace:  SimpleInjector
Assembly:  SimpleInjector (in SimpleInjector.dll) Version: 4.0.0

public void RegisterDecorator<TService, TDecorator>()
where TService : class
where TDecorator : class, TService

Type Parameters

The service type that will be wrapped by the given TDecorator.
The decorator type that will be used to wrap the original service type.

ArgumentExceptionThrown when TDecorator does not have a single public constructor, or when TDecorator does not contain a constructor that has exactly one argument of type TService or FuncTResult where T is TService.

This method uses the container's LifestyleSelectionBehavior to select the exact lifestyle for the specified type. By default this will be Transient.

The RegisterDecorator method works by hooking onto the container's ExpressionBuilt event. This event fires after the ResolveUnregisteredType event, which allows decoration of types that are resolved using unregistered type resolution.

Multiple decorators can be applied to the same service type. The order in which they are registered is the order they get applied in. This means that the decorator that gets registered first, gets applied first, which means that the next registered decorator, will wrap the first decorator, which wraps the original service type.

Constructor injection will be used on that type, and although it may have many constructor arguments, it must have exactly one argument of the type of TService, or an argument of type FuncTResult where TResult is TService. An exception will be thrown when this is not the case.

The registered TDecorator may have a constructor with an argument of type FuncTResult where T is TService. In this case, an decorated instance will not injected into the TService, but it will inject a FuncTResult that allows creating instances of the decorated type, according to the lifestyle of that type. This enables more advanced scenarios, such as executing the decorated types on a different thread, or executing decorated instance within a certain scope (such as a lifetime scope).


Please see RegisterDecorator(Type, Type) for an example.
See Also