Skip to content

A simple way to decorate a class with additional functionality using attributes.

License

Notifications You must be signed in to change notification settings

lawrence-laz/Decor.NET

Repository files navigation

Decor.NET

NuGet Version NuGet Downloads GitHub Action Status Codacy Badge Codacy Badge

🚧 Notes on version 3.0 🚧

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.

What does this mean for you?

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.

What does this package do?

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!

How to get started?

There are two ways to use this library:

  1. With Microsoft.DependencyInjection
  2. Without dependency injection

With Microsoft dependency injection

  1. Install the main package and Microsoft DependencyInjection integration:

    PS> Install-Package Decor.Extensions.Microsoft.DependencyInjection
  2. Create a decorator YourDecorator implementing IDecorator interface.

  3. Add [Decorate(typeof(YourDecorator))] attributes to SomeService class' methods to be decorated.

  4. Register to dependency container:

    services
        .AddScoped<SomeService>()
        .AddTransient<YourDecorator>() // Transient means decorator will inherit target's lifetime.
        .Decorate<SomeService>(); 

Without dependency injection

  1. Install the main package:

    PS> Install-Package Decor
  2. Create a decorator implementing IDecorator interface.

  3. Add [Decorate(typeof(YourDecorator))] attributes to methods to be decorated.

  4. Create decorated objects using Decorator class:

    var service = new SomeService();
    var decoratedService = new Decorator().For(service);

About

A simple way to decorate a class with additional functionality using attributes.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages