Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Interceptor with Microsoft.Extensions.DependencyInjection and asp.net web api 2 for cross cutting concerns like logging

We are using Microsoft.Extensions.DependencyInjection in our asp.net web api2 for dependency injection.

For cross cutting concerns like logging, we are of thought that aspect oriented programming should be considered and unable to find any support in the above di.

Other DI like castle, Unity and ninject are opted out post looking at their benchmarks. Is there any way to use injectors of other di frameworks like castle dynamic proxy and use it with Microsoft.Extensions.DependencyInjection?

Any suggestions related to IL-weaving frameworks are also welcomed. Consideration of PostSharp is ruled out as it isn't free.

like image 987
azharuddin irfani Avatar asked Jul 07 '18 18:07

azharuddin irfani


2 Answers

Decor.NET is a wrapper around Castle.Core dynamic proxy, which aims to simplify method decoration.

It is DI container agnostic, but has an integration with Microsoft's DI, which is available in Decor.Extensions.Microsoft.DependencyInjection NuGet package.

  1. You create a decorator:

    public class YourDecorator : IDecorator
    {    
        public SomeDependency SomeDependency { get; }    
    
        public YourDecorator(SomeDependency someDependency) // Supports DI.
        {
            SomeDependency = someDependency;
        }
    
        public async Task OnInvoke(Call call)
        {
            ...
            await call.Next();
            ...
        }
    }
    
  2. Decorate your methods:

    [Decorate(typeof(YourDecorator))]
    virtual void SomeMethod()  // << has to be overridable (can come from an interface)
    {
        ...
    }
    
  3. Register everything in Microsofts' DI:

    services.AddDecor()
        .AddTransient<YourDecorator>()
        .AddScoped<YourService>().Decorated();
    

You can play around in this .Net Fiddle.


Disclosure: I'm the author of this package.

like image 199
Laurynas Lazauskas Avatar answered Oct 23 '22 02:10

Laurynas Lazauskas


I'd argue you don't need any AOP frameworks, .NET provides just enough tooling to be able to do the majority of the aspect oriented stuff yourself.

The short version is this:

  1. Inherit from MarshalByRefObject on your business class.

  2. Create an "Aspect" for your desired functionality. As in, create a class that inherits from RealProxy. e.g. public class LoggingAspect<T> : RealProxy.

  3. override the Invoke method, and implement your aspect code. Make sure to pass-though the method call to the original instance.

  4. Create a builder/factory class that will create instances of the desired classes, e.g. Entity e = EntityBuilder.WithLogging();

  5. Replace all instance of new Entity(...) with EntityBuilder.Get(...).

The long version is much better explained by Microsoft employees themselves:

Aspect-Oriented Programming : Aspect-Oriented Programming with the RealProxy Class

like image 20
Dan Rayson Avatar answered Oct 23 '22 01:10

Dan Rayson