If you are curious about Linq to NHibernate and want to quickly check it out, here is an easy way to do it.
Check out a copy of the CodeCampServer source (svn). CodeCampServer is easy to work with because it has good tests on an already-working NHibernate implementation.
Check out a copy of NH Contrib (svn), open the NHibernate.Linq solution in Visual Studio and build. You may have to remove the compiler flag that indicates .NET 3.5 sp1 is available.
Open the CodeCampServer.sln and remove the NHibernate.dll reference from the DataAccess and IntegrationTests projects. Add a reference to NHibernate.dll from the NHibernate.Linq bin directory. Build and run tests. I had to play with the DatabaseTesterBase.recreateDatabase() method because I think are are still some bugs in Export():
public static void recreateDatabase() { var exporter = new SchemaExport(new HybridSessionBuilder().GetConfiguration()); exporter.Drop(false, true); exporter.Create(false, true); }
Now add a reference to NHibernate.Linq.dll in the DataAccess project. You can start writing queries and check your work with the integration tests that already exist. Here are a few simple queries for ConferenceRepository:
public Conference[] GetAllConferences() { ISession session = getSession(); var query = from conference in session.Linq<Conference>() select conference; return query.ToList().ToArray(); } public Conference GetConferenceByKey(string key) { ISession session = getSession(); var query = from conference in session.Linq<Conference>() where conference.Key == key select conference; return query.Distinct().Single(); } public Conference GetFirstConferenceAfterDate(DateTime date) { ISession session = getSession(); var query = from conference in session.Linq<Conference>() where conference.StartDate >= date orderby conference.StartDate select conference; return query.First(); } public Conference GetMostRecentConference(DateTime date) { ISession session = getSession(); var query = from conference in session.Linq<Conference>() where conference.EndDate <= date select conference; query.OrderByDescending(conference => conference.EndDate); return query.First(); }
5 Responses
Reflective Perspective - Chris Alcock » The Morning Brew #142
23|Jul|2008 1[...] Linq to NHibernate in 10 minutes - Matt Hinze examines Linq to NHibernate in the CodeCampServer project. [...]
Dew Drop - July 23, 2008 | Alvin Ashcraft's Morning Dew
23|Jul|2008 2[...] LINQ to NHibernate in 10 Minutes (Matt Hinze) [...]
Phil Webb
26|Jul|2008 3public Conference GetMostRecentConference(DateTime date)
{
ISession session = getSession();
var query = from conference in session.Linq()
where conference.EndDate conference.EndDate);
return query.First();
}
The above doen't work as expected. It returns 2004 - from conferencerepositorytester.
Actually needs the following:
public Conference GetMostRecentConference(DateTime date)
{
ISession session = getSession();
var query = from conference in session.Linq()
where conference.EndDate conference.EndDate).First();
}
This returns 2005. Subtle difference that has burnt me before.
Phil Webb
26|Jul|2008 4OOPS the pasted code has some stuff gone missing - probably the matching angle brackets.
The return from the second function needs to be:
return query.OrderByDescending(conference => conference.EndDate).First();replacing:
query.OrderByDescending(conference => conference.EndDate);return query.First();
Lets see how that goes
So you want to learn NHibernate? (or, NHibernate Hyperlink Acupuncture) | The Freak Parade
08|Aug|2008 5[...] & LINQ to NHibernate Linq to NHibernate in 10 Minutes Linq to NHibernate Kyle Baley - Trying out Linq to NHibernate My NHibernate 2.0 Supa' Layer [...]
Leave a reply
Search
Categories
Archives
Links
Recent Posts
Tags
.htaccess 443 Agile asp.net ASP.NET MVC aspnetmvc auditing business career certificate codecampserver continuous improvement culture ddd delicious dependency injection dry fluent interface headspring iis iis 6 linq log4net logging mvc nhibernate ninject refactoring resharper sed self selfssl seo sql ssl stats subcontrollers tabs tdd tempdata tortoisesvn treesurgeon visual studio whitespace wordpressA design creation of Design Disease
Copyright © 2008 - mhinze.com