Skip to content

maximtcapenko/querypack-dispatchproxy

Repository files navigation

QueryPack.DispatchProxy

Simple interception implementation based on System.Reflection.DispatchProxy. Allows you to create type-safe interceptors for individual methods. Supports standard dependency injection. Has direct access to intercepted instance.

Getting Started

  1. Install the package into your project
dotnet add package QueryPack.DispatchProxy
  1. Add interception configuration
class EntityInterceptorProxyFactoryBuilder : InterceptorProxyFactoryBuilder<Context, IEntityService>
{
    public void AddInterceptor(IInterceptorBuilder<Context, IEntityService> interceptorBuilder)
    {
        interceptorBuilder
        .OnMethodExecuting<string, EntityArg, CancellationToken, Task<EntityResult>>(e => e.CreateAsync,
                async (ctx, service, id, arg, token, invoker) =>
            {
                // code before method call
                var result = await invoker.Invoke();
                // code after method call
                return result;
            })
    }
}
  1. Register interception configuration in Startup
 services.AddInterceptorFor(new EntityInterceptorProxyFactoryBuilder());
  1. Service method call
IEntityService entitySerice;
var result = await entitySerice.CreateAsync("some_id", new EntityArg(), CancellationToken.None);
  1. Use cases
  • Access Control: Verify if the current user has the necessary permissions to invoke a specific method.
  • Input Validation: Validate the input parameters of the invoked method to ensure they meet the required format or constraints.
  • Output Validation: Validate the output of the invoked method to ensure it meets the expected format or constraints.
  • Data Replication: Use the result of the method execution to replicate data to other data sources.
  • Event Sourcing: Generate an event before or after the method is executed to capture and store the changes made to the data.

About

Simple interception implementation based on System.Reflection.DispatchProxy

Resources

License

Stars

Watchers

Forks

Languages