Geeks With Blogs
Billy McCafferty whatever (but really just .NET)

4/28/06 Update: The NHibernate codeproject.com article wins ASP.NET article of the month. Woohoo!

3/14/06 Update: See how this is used in a sample application at http://www.codeproject.com/useritems/NHibernateBestPractices.asp.

 


 

Hibernate.org has a great article on creating a generic DAO for Hibernate in Java. Below is what I use for the C# port.

 

The interface for common CRUD functionality...

 

public interface GenericDAO {

T GetById(IdDataType id, bool shouldLock);

List GetAll();

List GetByExample(T exampleInstance, string[] propertiesToExclude);

T SaveOrUpdate(T entity);

void Delete(T entity);

}

 

The generic DAO implementation...

 

public abstract class GenericNHibernateDAO : GenericDAO

{

/// Could be set using contruction injection IoC

public GenericNHibernateDAO(ISessionManager sessionManager) {

this.sessionManager = sessionManager;

}

 

public T GetById(IdDataType id, bool shouldLock) {

ISession session = GetSession();

T entity;

 

if (shouldLock) {

entity = (T) session.Load(persitentType, id, LockMode.Upgrade);

}

else {

entity = (T) session.Load(persitentType, id);

}

 

return entity;

}

 

public List GetAll() {

return GetByCriteria();

}

 

protected List GetByCriteria(params ICriterion[] criterion) {

ISession session = GetSession();

ICriteria criteria = session.CreateCriteria(persitentType);

foreach (ICriterion criterium in criterion) {

criteria.Add(criterium);

}

GenericUtils genericUtils = new GenericUtils();

return genericUtils.ConvertToGenericList(criteria.List());

}

 

public List GetByExample(T exampleInstance, string[] propertiesToExclude) {

ISession session = GetSession();

ICriteria criteria = session.CreateCriteria(persitentType);

Example example = Example.Create(exampleInstance);

 

foreach (string propertyToExclude in propertiesToExclude) {

example.ExcludeProperty(propertyToExclude);

}

 

criteria.Add(example);

 

GenericUtils genericUtils = new GenericUtils();

return genericUtils.ConvertToGenericList(criteria.List());

}

 

public T SaveOrUpdate(T entity) {

ISession session = GetSession();

session.SaveOrUpdate(entity);

 

return entity;

}

 

public void Delete(T entity) {

ISession session = GetSession();

session.Delete(entity);

}

 

private ISession GetSession() {

Check.Require(sessionManager != null, "sessionManager was not set");

return sessionManager.OpenSession();

}

 

private Type persitentType = typeof(T);

private ISessionManager sessionManager;

}

 

And for using this within your code...

 

public class ProjectDAONHibernate : GenericNHibernateDAO<Project, int> {

public ProjectDAONHibernate(ISessionManager sessionManager) : base(sessionManager) {}

}

 



Notes:

NHibernate Generics: To use generics with NHibernate, use the “NHibernate Generics” assembly available at http://www.ayende.com/projects/nhibernate-query-analyzer/generics.aspx.

SessionManager: To help with NHibernate session management, a good tool of choice is Castle Project's NHibernate facility:  http://www.castleproject.org/index.php/Facility:NHibernate.  Alternatively, a good design is explained in Chapter 8 of Hibernate In Action by Christian Bauer and Gavin King.  (They should be having a second edition coming out soon.)  The beneift of using Castle Project comes to light when used in conjunction with their Transaction factility; the Transaction facility allows you to mark which actions should be transactional via inline attributes.

Design by Contract:  You may have noticed Check.Require reference.  This is a completely optional, design-by-contract constraint using the VERY light weight framework described at http://www.codeproject.com/csharp/designbycontract.asp.

I've had wonderful success with NHibernate in the past and can continue to embrace it with C# 2.0 using generics.  The .NET community is finally starting to take notice of ORM tools like NHibernate which are helping to shave weeks, if not months, off of development.  I hope you all experience similar successes.

Posted on Wednesday, February 15, 2006 7:32 AM C# , NHibernate | Back to top


Comments on this post: Generic DAO with NHibernate

# re: Generic DAO with NHibernate
Requesting Gravatar...
I cant get this to work. Did you leave some magic out? Pleace post some code and not just a teaser :)

Left by Charlie on Mar 08, 2006 9:17 AM

# re: Generic DAO with NHibernate
Requesting Gravatar...
I'm currently preparing a few posts to describe the whole shebang. The first should be posted on Friday. The following two should come next week. Please keep your eye out for these as I would very much like feedback.
Left by Billy on Mar 08, 2006 10:29 AM

# re: Generic DAO with NHibernate
Requesting Gravatar...
Left by Billy on Mar 11, 2006 10:26 PM

# re: Generic DAO with NHibernate
Requesting Gravatar...
Dear Billy,

You article is brilliant.
However I am not use to EntitySet, EntityRef, EntityList etc. Can you add Many-to-Many mapping code in your Sample?

Many thanks,
kids
Left by kids on Apr 12, 2006 6:13 PM

# re: Generic DAO with NHibernate
Requesting Gravatar...
Ironically, I did my first many-to-many relationship with NHibernate.Generics' EntityList just today. Go to http://www.ayende.com/projects/nhibernate-query-analyzer/generics.aspx and search for the phrase "many to many" under the questions section.

The nice thing is that you don't need to implement the many-to-many declarations from both sides with NHibernate.Generics. For instance, suppose you have an addresses table. A user could have multiple addresses and an address could belong to multiple people - e.g. business address. But you'd usually only manage this many-to-many relationship from the user side. In this case, you can declare the many-to-many relationship within the user HBM and class files without having to introduce it to the address class. So far, I've been very impressed with NHibernate.Generics at www.ayende.com.

Thanks for the feedback!

Billy
Left by Billy McCafferty on Apr 12, 2006 6:32 PM

# re: Generic DAO with NHibernate
Requesting Gravatar...
Hi Billy,
Thanks for the article. It seems that the link you provided in the comments (the third comment down) is broken. Not to correct you on your own link (:p) but I believe this is the article you are referring to?

http://www.codeproject.com/KB/architecture/NHibernateBestPractices.aspx

~ Lacuna
Left by Lacuna on Jun 09, 2008 5:55 AM

# Mr
Requesting Gravatar...
Thanks for the code! I would like to borrow to use it if you dont mind sir. I wouldbe very greatly because I dont know what I am doing half the time and need a lil help
Left by brakes on Oct 27, 2010 5:09 PM

# re: Generic DAO with NHibernate
Requesting Gravatar...
good job mate. thank for sharing it :)
Left by forum paris sportifs on Nov 09, 2010 5:40 AM

# re: Generic DAO with NHibernate
Requesting Gravatar...

I really loved reading your blog. It was very well authored and easy to understand. Unlike additional blogs I have read which are really not good. I also found your posts very interesting. In fact after reading, I had to go show it to my friend and he enjoyed it as well!
Left by coco chanel on Dec 05, 2010 9:56 PM

# re: Generic DAO with NHibernate
Requesting Gravatar...
Great article and I agree with you that young generation spend a lot of time on social networking sites as now people are quiet interested in sharing their views with other people and there you can get your own group who are interested on the same topic that you are interested in...
Left by coco chanel on Dec 06, 2010 12:27 AM

# car moving company
Requesting Gravatar...
You made some good quality points there. I did a search on the topic and found many people will agree with your blog.
Left by auto transport on Dec 15, 2010 2:52 AM

# re: Generic DAO with NHibernate
Requesting Gravatar...
Is ok, but a example for dowload
Left by CArlos on Aug 24, 2011 11:35 AM

# re: Generic DAO with NHibernate
Requesting Gravatar...
i CAN NOT FIND NHibernate Generics
link is broken
http://www.codeproject.com/KB/architecture/NHibernateBestPractices.aspx

;)))
Left by AHYE on Jul 04, 2012 8:10 AM

Your comment:
 (will show your gravatar)


Copyright © Billy McCafferty | Powered by: GeeksWithBlogs.net