AOP in C#

In this post, I’m going to briefly discuss how to implement AOP (aspect-oriented programming) in C#. For this the class DispatchProxy will be used.
The idea is that cross-cutting concerns (like logging or caching) can be handled in a proxy class that wraps other types so that those concerns can be handled centrally without having to modify the original classes.

The most important part in the implementation of the DispatchProxy class is the Invoke method. Invoke method is called when a method or property on the proxied type is called.
It is important to highlight that the DispatchProxy class works only with interfaces. Target types need to implement an interface and that interface is what will be exposed by the proxy type created by DispatchProxy.

The link to the example GitHub project is the follows: https://github.com/MaverickFP/AOPTestApp

Analyzing the code, it is necessary to first create an interface and then a class that implements that interface.
Next we create a class (InvoiceServiceDispatch) that implements DispatchProxy.

public class InvoiceServiceDispatch<T> : DispatchProxy
        where T : class
	{
		public T Target { get; set; }

        public static T Create<T>(T target) where T : class
        {
            var proxy = Create<T, InvoiceServiceDispatch<T>>() as InvoiceServiceDispatch<T>;
            proxy.Target = target;
            return proxy as T;
        }

        protected override object? Invoke(MethodInfo? targetMethod, object?[]? args)
        {
            Console.WriteLine("Inside Proxy");
            //call original method
            return targetMethod.Invoke(Target, args);
        }
    }

The Invoke method is the heart of a DispatchProxy implementation. Here, we define what should happen when a method on the proxied object is used.
This Create method creates an instance of DispatchProxyLoggingDecorator, sets the target object, and calls DispatchProxy’s Create method to retrieve the proxy implementation of the target interface (which looks like an instance of T but calls its Invoke method whenever an API is used).

To run the flow, you can use the following code:

IInvoiceService invoiceService = new InvoiceService();
invoiceService = InvoiceServiceDispatch<IInvoiceService>.Create(invoiceService);
invoiceService.addInvoice();

Lascia un commento