Click here to Skip to main content
15,893,487 members
Articles / Web Development / ASP.NET

Simple NHibernate Architecture

Rate me:
Please Sign up or sign in to vote.
4.91/5 (38 votes)
11 Sep 2012CPOL3 min read 133.7K   3.2K   145  
An article showing a nice architecture I've come up for using NHibernate
using System;
using System.Collections.Generic;
using System.Text;
using Shared.NHibernateDAL;
using NHibernate;

namespace Business
{
	public class Product : BusinessObject<Product>
	{
		public Product()
		{
			this._name = null;
			this._weight = 0;
		}

		private string _name;
		private decimal _weight;

		public virtual decimal Weight
		{
			get { return _weight; }
			set { _weight = value; }
		}

		public virtual string Name
		{
			get { return _name; }
			set { _name = value; }
		}

		public static List<Product> Select()
		{
			ISession session = NHibernateSessionManager.Instance.GetSession();
			IQuery query = session.CreateQuery("from Product");
			return (List<Product>)query.List<Product>();
		}

		public void OutterTransaction()
		{
			using (TransactionBlock tran = new TransactionBlock())
			{
				Product p1 = new Product();
				p1.Name = "Product 1";
				p1.Weight = 10;

				p1.Save();

                //Call another method with a transaction block
				InnerTransaction();

				tran.IsValid = true;
                // On the dispose call the transaction is commited if all transaction blocks are valid
			}
		}
        		
		private void InnerTransaction()
		{
			using (TransactionBlock tran = new TransactionBlock())
			{
				Product p2 = new Product();
				p2.Name = "Product 2";
				p2.Weight = 10;

				p2.Save();				

				tran.IsValid = true;
			}
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) Intelligent Coder
Canada Canada
I've been developing .NET enterprise applications since 2000.

I am originally from Rio de Janeiro and I am currently working at http://www.intelligentcoder.com in Ontario.

I also have my own startup where we offer client intake forms.

Comments and Discussions