Introduction

Developing software applications is hard enough even with good tools and technologies. It is said by Spring.NET developers that Spring provides a lightweight solution for building enterprise-ready applications. Spring provides a consistent and transparent means to configure your application and integrate Aspect-Oriented Programming (AOP) into your software. Highlights of Spring's functionality are providing declarative transaction management for your middle tier as well as a full-featured ASP.NET Framework.

According to them, Spring.NET is an application Framework that provides comprehensive infrastructural support for developing enterprise .NET applications. It allows you to remove incidental complexity when using the base class libraries which makes best practices, such as test driven development, easy practices. Spring.NET is created, supported and sustained by SpringSource.

We have now added a new HTTP handler for *.aspx pages that is a Spring.NET provided handler. Now Spring.NET manages all our ASP.NET pages. We'll be able to use Spring.NET features for the ASP.NET pages after this configuration.

Now we have to add a reference to the site. We need a reference to Spring.Core and Spring.Web assembly located in the Spring.NET installation folder. We can now run the application to see our page. We'll go to the next step if everything is OK.

Dependency Injection

DI is a very interesting thing. You can make your design totally decoupled with concrete implementations. To see this in effect, let us create a new string type property in our page's code named Message with a private variable message. And in the Page_Load method, add the following line:

Response.Write(message);

Here we do not set the value of the message variable. We'll set it through the Web.Config file. We now change the object definition for Default.aspx:

<objecttype="Default.aspx"><propertyname="Message"value="Hello from Web.Config"/></object>

Now we have supplied a value outside of the page using the configuration file. We have set a string type value here. We can also set an object type value. To do that, let us define a class Math with one method:

publicclass Math
{
publicintadd(int x, int y)
{
return x+y;
}
}

Now let us add a new property Math in the Default.aspx code file like we did for message. In the Page_Load method, we add code:

Response.Write(math.add(30, 50));

In Web.Config we now add new object definition just above object for Default.aspx. So our spring section becomes:

Working with Data

Now we want to deal with data. To deal with data, Spring.NET natively provides NHibernate and ADO.NET support. Here we use NHibernate - as it handles all dirty things about database and lets us work in the object world only. In the Spring.NET installation folder in /bin and in /lib directory, we have the required NHibernate binaries. Here I use NHibernate 1.2 assemblies. For hibernate we define a table in the database named person, create a new class named Person and a NHibernate mapping file (Person.hbm.xml):

Declarative Transaction Management

It keeps transaction management out of business logic, and is not difficult to configure in Spring. We want our certain operation be under transaction. We do not need to manage that in code. We simply configure that from Web.Config file. Let us assume we want Save, SaveOrUpdate, Delete, Query etc. method in PersonDAO to be in atomic database operation and rollback entire operation on any exception. We create PersonDAOTx object in our config file and set it to the Deafult.aspx pages' PersonDAO property.

Please note that we can use a new transaction or an inherited transaction from the caller. While using inherited transaction, a new transaction is automatically created if none exists from the caller.

Web Service

Spring introduces very flexible Web service support. It can export any class that implements a public interface. WebService attribute is not required at all. Let us define a plain interface and implement it in a plain class:

That's it. We can now export this class by just adding a few lines in the Web.Config file. We can also inject its dependency (the Message property) from web.config. Please note the new httpHandlers entry to handle.asmx page.

By now we can create ASPX pages, Web services without [WebService] attribute from virtually any class, inject dependency from configuration file - making the application highly configurable, work with database and totally separate the transaction management from business logic. I expect it will help to quickstart using Spring.NET and develop a scalable application. For beginners, when you use Spring.NET for the first time you'll be surprised how much it helps by reducing most of the redundant work. Please excuse for language - my first language is Bangla. I'll be happy to hear from you.

I am getting errors for the Math example. My errors are: Cannot declare a variable of static type 'System.Math', etc... I have placed my Math class in the App_Code folder. I'm not sure why it isn't working but I'm new to Spring.NET. Here is the main error I get when I run the page.

Error creating object with name '/default.aspx' defined in 'config [spring/objects]' : Initialization of object failed : Cannot set the value of a type.

I recreated the project as a file based web application from my C:\ drive. Because the app was on a file share I had some permissions issue and the App_Code folder wasn't an available to option to add either. Now I have the Math and Hello from web.config options working as expected.

when I try to use your sample, I have a problem with IBaseDAO <entityt, idt="">. The errors are:C:\Inetpub\wwwroot\WebApplication1\Dal\BaseDAO.cs(10): Invalid token '<' in class, struct, or interface member declarationC:\Inetpub\wwwroot\WebApplication1\Dal\BaseDAO.cs(10): Invalid token ',' in class, struct, or interface member declaration

Not only a very nice & clean article but also very easy to follow. I know it might not be the most advanced tutorial on the topic, nonetheless, it has more substance than volume. Good job mate as there aren't enough on the topic for starters on the web. Keep it up!