-
Notifications
You must be signed in to change notification settings - Fork 24
Decorators
A Decorator is different to other registrations, in that it does not provide an instance of a type, but rather wraps/modifies an underlying instance of a type, which is redolved in the normal manner.
If multiple decorators are registered for a type, all of them will be applied, by wrapping one decorator in another, onion style. The order in which decorators wrap each other is deterministic but an implmentation detail - you should not rely on this order.
Decorators are not applied to delegate parameters, or to [Instance]
fields and properties with Options.DoNotDecorate applied. They are applied to everything else.
There are two ways to register decorators.
You can register a type as a decorator for an interface it implements, if its constructor has exactly one parameter whose type is the interface.
You can register a method returning T
as a decorator of T
if it has exactly one parameter of type T
.
Decorators are not disposed by default, for a number of reasons:
- In many cases a decorator implements
IDisposable
as the interface requires it, but does not actually require disposal. - In many cases a decorator will delegate to the underlying's
Dispose
method:- Since the underlying is disposed separately, this can lead to double disposal.
- The underlying may be an
Instance
field or property, which should never be disposed.
- In many cases a
DecoratorFactory
may return the same instance as was passed in, also leading to issues 2.i and 2.ii.
If your decorator needs to be disposed, make sure it does not dispose the underlying instance, only resources it owns directly. Use DecoratorOptions.Dispose
to mark it as requiring disposal.
Decorators are disposed from outermost to innermost, followed by the underlying instance.