Cysharp / ConsoleAppFramework

Zero Dependency, Zero Overhead, Zero Reflection, Zero Allocation, AOT Safe CLI Framework powered by C# Source Generator.
MIT License
1.54k stars 88 forks source link

Global filters with dependency injection #88

Closed wozzo closed 10 months ago

wozzo commented 1 year ago

The documentation specifies⬇️

// Filter is instantiated by DI so you can get parameter by constructor injection.

But only shows how to add global filters where the instance is instantiated upfront

var app = ConsoleApp.Create(args, options =>
{
    options.GlobalFilters = new ConsoleAppFilter[]
    {
        new MutextFilter() { Order = -9999 } ,
        new LogRunningTimeFilter() { Oder = -9998 }, 
    }
});

Is there a way to use Dependency Injection with global filters, that I've just missed from the documentation?

e.g.


// filter class
public class MyFilter : ConsoleAppFilter
{
  private readonly ILogger<MyFilter> logger;
  public MyFilter(ILogger<MyFilter> logger)
  {
    this.logger = logger;
  }

  public override async ValueTask Invoke(ConsoleAppContext context, Func<ConsoleAppContext, ValueTask> next)
  {
    logger.LogInformation(context.MethodInfo.Name);
    await next(context);
  }
}

// and adding the filter
var builder = ConsoleApp.CreateBuilder(args, options =>
  {
    options.AddFilter<MyFilter>;
  });
github-actions[bot] commented 1 year ago

This issue is stale because it has been open 90 days with no activity. Remove stale label or comment or this will be closed in 7 days.

wozzo commented 1 year ago

Still a thing

neuecc commented 1 year ago

Sorry for delayed response, I'll check it.

LadislavBohm commented 1 year ago

I don't think you can create them out of the box with DI but do you need to? ConsoleAppContext has access to ServiceProvider so you can resolve dependencies there.

github-actions[bot] commented 11 months ago

This issue is stale because it has been open 180 days with no activity. Remove stale label or comment or this will be closed in 30 days.