Namespace: SimpleInjector
Assembly: SimpleInjector (in SimpleInjector.dll) Version: 5.3.0
public void RegisterDecorator( Type serviceType, Type decoratorType, 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. - predicate
- Type: SystemPredicateDecoratorPredicateContext
The predicate that determines whether the decoratorType must be applied to a service type.
Exception | Condition |
---|---|
ArgumentNullException | Thrown when one of the arguments is a null reference. |
ArgumentException | Thrown 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. |
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 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).