Aspect Oriented Programming using spring.NET, 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.

The principles covered in this tutorial will apply to creating ASP.NET MVC Web Site and inject services object in controller using SPRING.NET.

A. So create a service class Library named LogCorner.Services.DataService   and Add class    class ProductService that implement  IProductService  as follow

1

2

3

Add a .xml or .config file name it  and make it as embedded Ressource on Build Action property

4 5

B . Create an ASP.NET Web Client to use ProductService

  • So add a new Asp.NET MVC4 Internet Web application
  • Expand Controller Folder
  • Open HomeController file and add a property  IProductService (don t forget to reference our service dll)

7

61. Spring Configuration of the client

  • Add an xml or config file and name it spring-config-controllers and it as Embedded Ressource

8

Now add a Spring Application Context class (dont forget to reference Spring.Core dll). If you know how to install nuget Packages please  follow this link . Using nuget package gallery with visual studio 2012 

9Here is Complete class code

using Spring.Context;
using Spring.Context.Support;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace LogCorner.Client.Web
{
/// <summary>
/// Spring Application Context.
/// </summary>
public static class SpringApplicationContext
{
/// <summary>
///
/// </summary>
private static IApplicationContext Context { get; set; }

/// <summary>
/// Returns a boolean value if the current application context contains an named object.
/// </summary>
/// <param name=”objectName”>Accepts the name of the object to check.</param>
public static bool Contains(string objectName)
{
SpringApplicationContext.EnsureContext();
return SpringApplicationContext.Context.ContainsObject(objectName);
}

/// <summary>
/// Return a instance of an object in the context by the specified name.
/// </summary>
/// <param name=”objectName”>Accepts a string object name.</param>
public static object Resolve(string objectName)
{
SpringApplicationContext.EnsureContext();
return SpringApplicationContext.Context.GetObject(objectName);
}

/// <summary>
/// Return a instance of an object in the context by the specified name and type.
/// </summary>
/// <typeparam name=”T”>Accepts the type of the object to resolve.</typeparam>
/// <param name=”objectName”>Accepts a string object name.</param>
public static T Resolve<T>(string objectName)
{
return (T)SpringApplicationContext.Resolve(objectName);
}
/// <summary>
///
/// </summary>
private static void EnsureContext()
{
if (SpringApplicationContext.Context == null)
{
SpringApplicationContext.Context = ContextRegistry.GetContext();
}
}
}
}

  • Add a SpringControllerFactory  Class

10

. Open then Global.asax file and  set our SpringControllerFactory  class  at Application_Start method

11

  • The final step is to setup our web.config file so as to configure spring objects

12To test our sample , lets update our HomeController Class as follow

13Thanks , feedbacks and comments are welcome

If you ask for catching Exception using advices , please see our tutorial  http://logcorner.wordpress.com/2013/08/23/using-advices-and-poincuts-in-spring-net/

Dowload source code

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