After I got really stuck with a NHibernate HQL query string that didn't want to come out right, I wished for something like Linq and I remembered the Linq to NHibernate project. And exactly 24 hours ago Steve Strong posted a progress report which sounds very promising to me. Especially, after I read how easy it is to use the new Linq to NH Provider, I was convinced.
However, I went first to the "old" provider in the NH Contrib source repository, although the "new" one is in the NHibernate trunk. For the new provider, we have to compile NHibernate from the trunk ourselves.
Well, and so I implemented the usage in an Empinia branch and for the specific case, where I wanted to use Linq instead of HQL. And it works great!
The query looks very simple and neat now and I wonder, why it took me so long with the HQL.
var wastes = from w in wasteContext.Wastes.Query()
where
w.DisposalDate != default(DateTime)
&& w.RecordDate >= thisYear
select w;
Under the hood, the Query() extension method simply tries to cast the domain repository (Wastes) to ILinqSupport. If that succeeds, the method ILinqSupport.Query<T>() is called, which means essentially that the extension method provided by the NHibernate.Linq namespace is called on the underlying NHibernate session.
And so simple it actually is to use Linq queries with the Empinia DomainModel.
The ILinqSupport interface is implemented in the Persistence internal NHibernateStore. However, we can consider making this even better accessible on the API, i.e. in the prominent IDomainModelStore by implementing a Query<T> method that returns an a IQueryable<T>. Currently, the Query extension method throws a NotSupportedException, if the IDomainModelStore instance does not implement ILinqSupport.
After all, this opportunity definitely speaks for usage of the NET 3.5 Framework. Linq - and especially NHibernate.Linq - rock and can really increase productivity and simplicity of our source code.
Comments
Neue Inhalte bitte
Wed, 07/21/2010 - 19:45 — karen61Halten Sie die großartige Arbeit