ContainerRegisterDecoratorTService, TDecorator Method (Lifestyle)

Simple Injector
Ensures that the supplied TDecorator decorator is returned and cached with the given lifestyle, 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. Decorators can be applied to both open, closed, and non-generic service types.

Namespace:  SimpleInjector
Assembly:  SimpleInjector (in SimpleInjector.dll) Version: 5.3.0
Syntax

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

Parameters

lifestyle
Type: SimpleInjectorLifestyle
The lifestyle that specifies how the returned decorator will be cached.

Type Parameters

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

ExceptionCondition
ArgumentNullExceptionThrown when one of the arguments is a null reference.
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.
Remarks

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 registered. 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, the will not inject the decorated TService itself into the TDecorator instance, 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).

Examples

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

Reference