Using Advices and Poincuts in SPRING.NET and ASP.NET MVC

In this tutorial we learn how to do dependency Injection using SPRING.NET,  Dependency injection (DI) is a process whereby objects define their dependencies, that is, the other objects they work with, only through constructor arguments and properties that are set on the object instance after it is constructed. (Factory methods may be considered a special case of providing constructor arguments for the purposes of this description). The container injects these dependencies when it creates the object. This process is fundamentally the inverse to the case when the object itself is controlling the instantiation or location of its dependencies by using direct construction of classes, or the Service Locator pattern. The inverting of this responsibility is why the name Inversion of Control (IoC) is used to describe the container’s actions.

This tutorial is a continuation of Aspect Oriented Programming using spring.NET, ASP.NET MVC

The principles covered in this tutorial will apply to creating ASP.NET MVC Web Site and inject services object in controller using SPRING.NET. We also use advices and pointcuts to intercept object behaviours

We are going to implement a before advice , after advice, around advice and throw advice

  • So open our project and reference Spring.AOP

advice1

  • The next step is to create our advices

advice2 advice3 advice4 advice5

  • Now lets create xml file aop.xml and define it Build Action as Embedded Ressource

advice6

  • Open spring-config-services.xml file and update it as follow :

advice7

Open our web.config file and add aop.xml file as follow :

advice8

  • Ok, we are at the end of this tutorial. So lets begin tests.

Make breakPoint at our advices and press F5 , the excetion workflow look like this : The method GetProducts of IProductService is intercepted Before, Around and after execution.

advice9

advice10

advice11

  • Lets now test our ThrowAdvice by a division by Zero and press F5 :

advice12

advice13

We can see that the exception is intercepted. So we log here all exceptions occured during execution of our service.

Thank you for feedbacks

Gora LEYE

I'm a microsoft most valuable professional (MVP) .NET Architect and Technical Expert skills located in Paris (FRANCE). The purpose of this blog is mainly to post general .NET tips and tricks, www.masterconduite.com Gora LEYE

Support us

BMC logoBuy me a coffee