You don’t need to use a 3th party IoC container to use AOP(Aspect Oriented Programming) and the proxy pattern in ASP.NET Core. We’ll combine the power of Castle.DynamicProxy and the standard DI to make this possible.
Castle's dynamic proxies allows you to create proxies of abstract classes, interfaces and classes (only for virtual methods/properties).
Let’s create an example that caches the output of a repository call.
Here is the example repository that we want to proxy:
Now we first need to create an interceptor that intercepts the method calls and caches the response:
Let’s move on to the DI registration. We first need to create an extension method that triggers the proxy creation:
Almost there, as a last step we need to register everything: