With the introduction of source generators in C#, Decor will undergo major changes with significant improvements. Big part of dynamic code will be transformed into compile time source generators.
If you are currently using Decor V2, you can continue using it with no problems. Once V3 is available you will have to make a decision to either stay with V2, or update your code to V3. No new features will be added to V2, but in case of significant bugs they will be addressed. All further development of Decor will be performed on V3.
This package provides a nice and simple way to execute any code before and after any other method. This is particularly useful for things like: logging, profiling, retry logic, caching, etc.
[Decorate(typeof(LoggingDecorator))]
void DoWork()
{
Console.WriteLine("Working...");
}
public class LoggingDecorator : IDecorator
{
public async Task OnInvoke(Call call)
{
Console.WriteLine("Will do some work!");
await call.Next();
Console.WriteLine("Work is finished!");
}
}
Output is:
Will do some work!
Working...
Work is finished!
There are two ways to use this library:
-
Install the main package and Microsoft DependencyInjection integration:
PS> Install-Package Decor.Extensions.Microsoft.DependencyInjection
-
Create a decorator
YourDecorator
implementingIDecorator
interface. -
Add
[Decorate(typeof(YourDecorator))]
attributes toSomeService
class' methods to be decorated. -
Register to dependency container:
services .AddScoped<SomeService>() .AddTransient<YourDecorator>() // Transient means decorator will inherit target's lifetime. .Decorate<SomeService>();
-
Install the main package:
PS> Install-Package Decor
-
Create a decorator implementing
IDecorator
interface. -
Add
[Decorate(typeof(YourDecorator))]
attributes to methods to be decorated. -
Create decorated objects using
Decorator
class:var service = new SomeService(); var decoratedService = new Decorator().For(service);