-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
Author Message
 Post subject: Writing a base DAO
PostPosted: Wed Jan 04, 2006 4:52 am 
Regular
Regular

Joined: Fri Nov 04, 2005 12:37 pm
Posts: 81
I am writing a BaseDAO for a c#.net application that uses nHibernate. I am having trouble making use of session.get in a generic manner, so that it can be utilized by my other DAOs.

I currently have something like this:


protected Task get(int id)
{
ISessionFactory factory = cfg.BuildSessionFactory();

ISession session = factory.OpenSession();

return session.Get(typeof(Task), 1) as Task;

}


I want to change this so that it will operate on any of my Persistant objects, i.e not just Task, i want to do something like pass in the Persistant class into the method and then this gets used in the 'get' method.

But im a little unsure how to do this, any suggestions?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 04, 2006 5:16 am 
Regular
Regular

Joined: Fri Nov 04, 2005 12:37 pm
Posts: 81
I have managed to get it working like this:

Base Class:

protected Object get(Object ob,int id)
{
ISessionFactory factory = cfg.BuildSessionFactory();
ISession session = factory.OpenSession();
return session.Get(ob.GetType(),id);
}


The extending class:

public Tasks getTask(int id)
{
Tasks t = new Tasks();
return (Tasks)base.get(t,id);
}


But this seem like abit of a hack...can it be cleaned up in anyway?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 04, 2006 8:44 am 
Contributor
Contributor

Joined: Wed May 11, 2005 4:59 pm
Posts: 1766
Location: Prague, Czech Republic
It can't be cleaned up unless you use generics.
And by the way, don't create a new session factory for each call of Get, it's incorrect and very expensive. Read the documentation.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 04, 2006 8:47 am 
Regular
Regular

Joined: Fri Nov 04, 2005 12:37 pm
Posts: 81
Oh thanks for the tip


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.