Spring.NET DAO

I’ll be blogging an example on Spring.NET DAO. I received a number of requests from the participants of Move Up to the Paradigm: From PHP & JSP to ASP.NET 2.0. They have actually requested that I demonstrate more .NET/Java related examples on frameworks such Spring Vs. Spring.NET, Struts Vs. NStruts, Velocity Vs. NVelocity and so forth. I’ll be showing an example of using Spring.NET (C#) to develop a standard data access component. In this example, I’ll be using the Northwind database as well as some design patterns (Data Access Object, and Value Object).
 
Northwind.xml (my configuration file)

<?xml version=”1.0″ encoding=”utf-8″ ?>

<objects xmlns=”http://www.springframework.net&#8221;

xmlns:db=”http://www.springframework.net/database”&gt;

<db:provider id=”dbProvider”

provider=”SqlServer-1.1″

connectionString=”SERVER=localhost;DATABASE=northwind;Integrated Security=SSPI;”/>

<object id=”adoTemplate” type=”Spring.Data.Core.AdoTemplate”>

<property name=”DbProvider” ref=”dbProvider”/>

<property name=”DataReaderWrapperType” value=”Spring.Data.Support.NullMappingDataReader”/>

</object>

<object id=”customerDao” type=”Spring.Dao.CustomerDao”>

<property name=”AdoTemplate” ref=”adoTemplate”/>

</object>

</objects>

My Entity Class

using System.Collections;

namespace Spring.Dao
{
public class Customer
{
#region Fields

        protected string id;
protected string companyName;
protected string contactName;
protected string contactTitle;
protected string address;
protected string city;
protected string region;
protected string postalCode;
protected string country;
protected string phone;
protected string fax;
protected IList orders;

        #endregion

        #region Properties

        public string Id
{
get { return id; }
set { id = value; }
}

        public string CompanyName
{
get { return companyName; }
set { companyName = value; }
}

        public string ContactName
{
get { return contactName; }
set { contactName = value; }
}

        public string ContactTitle
{
get { return contactTitle; }
set { contactTitle = value; }
}

        public string Address
{
get { return address; }
set { address = value; }
}

        public string City
{
get { return city; }
set { city = value; }
}

        public string Region
{
get { return region; }
set { region = value; }
}

        public string PostalCode
{
get { return postalCode; }
set { postalCode = value; }
}

        public string Country
{
get { return country; }
set { country = value; }
}

        public string Phone
{
get { return phone; }
set { phone = value; }
}

        public string Fax
{
get { return fax; }
set { fax = value; }
}

        #endregion

        #region Constructor (s)

        public Customer()
{
}

        #endregion
}
}

My DAO Class

using System.Data;
using Spring.Data.Core;
using Spring.Dao;

namespace Spring.Dao
{
public class CustomerDao : AdoDaoSupport
{
private string SQL_SELECT = @”select Address, City, CompanyName, ContactName, ContactTitle, Country, Fax, CustomerID, Phone, PostalCode, Region from Customers where ContactName = @ContactName”;

public Customer GetCustomer(string contactName)
{
return (Customer)AdoTemplate.QueryForObject(CommandType.Text, SQL_SELECT, new CustomerRowMapper(), “ContactName”, DbType.String, 30, contactName);
}
}
}

My Mapper Class

 
using System.Data;
using Spring.Data;
using Spring.Dao;
namespace Spring.Dao
{
public class CustomerRowMapper : IRowMapper
{
public object MapRow(IDataReader iDataReader, int rowNumber)
{
Customer customer = new Customer();
customer.Address = iDataReader.GetString(0);
customer.City = iDataReader.GetString(1);
customer.CompanyName = iDataReader.GetString(2);
customer.ContactName = iDataReader.GetString(3);
customer.ContactTitle = iDataReader.GetString(4);
customer.Country = iDataReader.GetString(5);
customer.Fax = iDataReader.GetString(6);
customer.Id = iDataReader.GetString(7);
customer.Phone = iDataReader.GetString(8);
customer.PostalCode = iDataReader.GetString(9);
customer.Region = iDataReader.GetString(10);
return customer;
}
}
}

My Unit Test Program

using System;
using System.Collections;
using Spring.Context;
using Spring.Context.Support;
using Spring.Data.Config;
using Spring.Objects.Factory.Xml;
using Spring.Data;
using Spring.Dao;

namespace Spring.NETCSConsoleApplication
{
class TestCustomer
{
static void Main(string[] args)
{
IApplicationContext iApplicationContext = null;

            try
{
NamespaceParserRegistry.RegisterParser(typeof(DatabaseNamespaceParser));
iApplicationContext = new XmlApplicationContext(“
file://C:/Spring.NETCSConsoleApplication/Spring.NETCSConsoleApplication/Northwind.xml“);

                CustomerDao customerDao = iApplicationContext[“customerDao”] as CustomerDao;
Customer customer = customerDao.GetCustomer(“Hanna Moos”);
Console.WriteLine(customer.Id);
Console.WriteLine(customer.CompanyName);
}
catch (Exception exception)
{
Console.WriteLine(exception.StackTrace);
}
finally
{
Console.WriteLine(“Press <ENTER> to exit.”);
Console.ReadLine();
}
}
}
}

Generally, Spring.NET is an enterprise application framework. The framework heavily uses design patterns and thus is very well designed. I personally like the data access library of the framework as the library provides a very well designed class called AdoTemplate that provides a number of comprehensive transparent data access functions to a relational database.