3 layer Example of NHibernate + Spring.Net + AOP
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.
Download the Example
-
Feroz
-
RedVesper
-
neusha
tweets
Recent Posts
- How To: Configure Access Control Service on Windows Azure
- Installing Windows 8 Consumer Preview with VMWare Workstation 8
- Authorizable content with Razor
- NHibernate Validator on GitHub
- NHibernate Validator 1.3.1 GA
- NHibernate Validator 1.2 Beta3 released
- NHibernate Validator 1.2 Beta2 released
- jqGrid + Linq + Asp.Net MVC example
- Asp.net MVC: Testing a custom Authorize filters
- NHibernate Validator Quickstart
Recent Comments
- Dario on Installing Windows 8 Consumer Preview with VMWare Workstation 8
- Steve C on Installing Windows 8 Consumer Preview with VMWare Workstation 8
- Quang on StatelessSession: NHibernate without first-level cache
- Luis Fernando on NHibernate Validator 1.3.1 GA
- Daniel on NHibernate – Bulk Manipulation with SQL Native




