ContainerRegisterDecorator Method (Type, Type, Lifestyle, PredicateDecoratorPredicateContext)

Simple Injector
Ensures that the supplied decoratorType decorator is returned when the supplied predicate returns true and cached with the given lifestyle, wrapping the original registered serviceType, by injecting that service type into the constructor of the supplied decoratorType. Multiple decorators may be applied to the same serviceType. 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(
	Type serviceType,
	Type decoratorType,
	Lifestyle lifestyle,
	Predicate<DecoratorPredicateContext> predicate
)

Parameters

serviceType
Type: SystemType
The definition of the (possibly open generic) service type that will be wrapped by the given decoratorType.
decoratorType
Type: SystemType
The definition of the (possibly open generic) decorator type that will be used to wrap the original service type.
lifestyle
Type: SimpleInjectorLifestyle
The lifestyle that specifies how the returned decorator will be cached.
predicate
Type: SystemPredicateDecoratorPredicateContext
The predicate that determines whether the decoratorType must be applied to a service type.
Exceptions

ExceptionCondition
ArgumentNullExceptionThrown when one of the arguments is a null reference.
ArgumentExceptionThrown when serviceType is not an open generic type, when decoratorType does not inherit from or implement serviceType, when decoratorType does not have a single public constructor, or when decoratorType does not contain a constructor that has exactly one argument of type serviceType or FuncTResult where T is serviceType.
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 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 serviceType, or an argument of type FuncTResult where TResult is serviceType. An exception will be thrown when this is not the case.

The registered decoratorType may have a constructor with an argument of type FuncTResult where T is serviceType. In this case, the will not inject the decorated serviceType itself into the decoratorType 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 the RegisterDecorator method for more information.
See Also

Reference