One of the nice features of Autofac is the support for decorators.
A decorator class implements the same interface as the class it “wraps”. With a decorator you can add functionality to a class without changing it’s signature.
Let’s walk through an example step by step:
Here is the class and it’s corresponding interface I want to decorate:
Nothing special at the Autofac level yet:
Let’s now extend the behavior of this class through a decorator. What if we want to benchmark the execution time of the LogMessage() method? The decorator class should implement the same interface AND inject the interface at the same time:
Now we need to register the decorator in Autofac:
Remark: When using generics, it is also possible to register an open generic decorator. Therefore you need to use the RegisterGenericDecorator() overload
More information: https://autofaccn.readthedocs.io/en/latest/advanced/adapters-decorators.html