Castle.Core.AsyncInterceptor icon indicating copy to clipboard operation
Castle.Core.AsyncInterceptor copied to clipboard

Suggestion: utility methods to create interceptors or even intercepted objects

Open ndrwrbgs opened this issue 8 years ago • 4 comments

To provide a simple interceptor like logging start/finish of all methods, users could use a utility method (lightweight) instead of being required to create a class (medium weight for a simple concern).

Example (typed from phone, so pseudo code):

IEquatable<string> sample = EqualityComparer<string>.Default;
// sample name stolen from Rx, if/when implementing spend more time naming
var wrapped = DelegateAsyncInterceptor
      .Intercept<IEquatable<string>>(
            target: sample,
            filter: invocation => true, // optional parameter
            beforeInvoke: async () => {}, // Log
            afterInvoke: async () => {}); // Log

Or at least

IEquatable<string> sample = EqualityComparer<string>.Default;
// sample name stolen from Rx, if/when implementing spend more time naming
var wrapped = DelegateAsyncInterceptor
      .Intercept<IEquatable<string>>(
            target: sample,
            interceptor:
                async (invocation, proceed) =>
                {
                     // TODO: logging
                     await proceed(invocation);
                });

ndrwrbgs avatar Nov 11 '17 18:11 ndrwrbgs

I guess the second should’ve been an example of creating a dynamic Interceptor rather than the target :) oh well the idea should be explained well enough.

ndrwrbgs avatar Nov 11 '17 18:11 ndrwrbgs

It's a great idea. Maybe as an extension method to IProxyGenerator so something like this:

IEquatable<string> sample = EqualityComparer<string>.Default;
// sample name stolen from Rx, if/when implementing spend more time naming
var generator = new ProxyGenerator();
generator..Intercept<IEquatable<string>>(
    target: sample,
    filter: invocation => true, // optional parameter
    beforeInvoke: async () => {}, // Log
    afterInvoke: async () => {}); // Log

JSkimming avatar Nov 11 '17 23:11 JSkimming

@JSkimming looking for executive decision - as a case could be made for either option...

  1. Add to this code base. PRO: discoverability. CON: feature-creep and more to maintain
  2. Leave for an external code base. (pro/con opposite of 1. CON: might not ever get done if I forget about it again like last year :) )

ndrwrbgs avatar Dec 12 '18 20:12 ndrwrbgs

@ndrwrbgs I'm happy for it to be added here.

JSkimming avatar Dec 13 '18 22:12 JSkimming