Sometime ago I wrote this template project using Spring.Net and NHibernateprimarily. I use NHibernate for the data access and Spring.Netfor the IoC and Aop. With the Aop support, the transaction managmente it’s very easy to implement.
This it’s the CustomerService class:
public class CustomerService : BaseService, ICustomerService
{
/// <summary>
/// This field is inyected by IoC through the property.
/// </summary>
private ICustomerDao customerDao;
public ICustomerDao CustomerDao {
get { return customerDao; }
set { customerDao = value; }
}
#region ICustomerService Members
public int CreateCustomer(string Name, string LastName) {
Customer customer = new Customer();
customer.FirstName = "Dario";
customer.LastName = "Quintana";
CustomerDao.Save(customer);
return customer.Id;
}
public void DeleteCustomer(int Id) {
Customer customer = CustomerDao.GetById(Id);
CustomerDao.Delete(customer);
}
#endregion
}
As you can see, the CreateCustomer or DeleteCustomer is not envolved at code by anyone Transactional or UnitOfWork code, nor the method Save(customer) at CustomerDao class.
Then… where it’s the transactional support ? Spring + AOP is the answer. Spring envolves al Service methods between transaccions.
When you instantiate the class CustomerService, a proxy is instantiate insted. With the proxy Spring.Net can envolve the method configurated into transactions. Cool uh?
Another thing that you can appreciate at example it’s the using of uNhAddIns at Repository stuff. With this library you can use Hql/Sql queries detached with the class DetachedQuery. With DetachedQuery you can use NamedQuery and this a good practice to adopt.