Skip to content

Simple implementation of audit based on QueryPack.DispatchProxy

License

Notifications You must be signed in to change notification settings

maximtcapenko/querypack-audit

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

26 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

QueryPack.Audit

Simple implementation of audit based on QueryPack.DispatchProxy

Getting Started

  1. Install the package into your project
dotnet add package QueryPack.Audit
  1. Add audit configuration
class EntityAuditConfiguration : IAuditConfiguration<AuditContext, IEntityService>
{
    public void Configure(IAuditConfigurator<AuditContext, IEntityService> configurator)
    {
      configurator.Add(builder
        => builder.OnMethodExecuting<string, EntityArg, CancellationToken, 
            Task<EntityResult>>(e => e.CreateAsync,
          async (ctx, service, id, arg, token, invoker) =>
          {
              var result = await invoker.Invoke();
              ctx.Sender.Send(result);

              return result;
          }));
    }
 }
  1. Register audit configuration in Program
static IHostBuilder CreateHostBuilder(string[] args) => Host.CreateDefaultBuilder(args)
      .ConfigureServices((hostContext, services) =>
      {
          services.AddTransient<IEntityService, EntityService>();
          services.AddAudit(registry =>
          registry.ConfigureOptions(options =>
          {
              options.QueueReadBatchSize = 50;
              options.QueueReadIntervalInSeconds = 5;
              options.ReceiveTimeoutInSeconds = 15;
          })
          .AddContext<AuditContext>()
          .AddReceiver<EntityResult, EntityResultAuditableReceiver>()
          .AuditFor(new EntityAuditConfiguration()));
      });
  1. Implement auditable receiver
class EntityResultAuditableReceiver : IAuditableReceiver<EntityResult>
{
    public Task<IEnumerable<EntityResult>> ReceiveAsync(IEnumerable<EntityResult> auditables, CancellationToken cancellationToken)
    {
        // code is here
        // return empty collection in case if there are no errors
        return Task.FromResult(Enumerable.Empty<EntityResult>());
    }
}
  1. Service method call
IEntityService entitySerice;
var result = await entitySerice.CreateAsync("some_id", new EntityArg(), CancellationToken.None);

About

Simple implementation of audit based on QueryPack.DispatchProxy

Resources

License

Stars

Watchers

Forks

Languages