20.12.08

Refactoring to DetachedNameQuery

The consequence of hiding the NHibernate ISession type from the client code is that I now have to rely on inline SQL which kind of makes me queasy. But this feeling is temporary because I can use a feature of NHibernate called DetachedNameQuery to achieve the following:


IRepository repository = new NHibernateRepository();
IDetachedQuery query = new DetachedNamedQuery("GetMaxTaskId");
ITask task = repository.Find<Task>(query);

GetMaxTaskId is defined in my NHibernate mapping file, Task.hbm.xml.

The entry takes the following form:

<query name ="GetMaxTaskId">
<
![CDATA[
select max(task.Id) from Task task
]]>
</query>

1 comment:

Taiwanigloo said...

Good work !!!
very interesting articles!

Happy New Year

W.