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(); }
Pingback: Reflective Perspective - Chris Alcock » The Morning Brew #142
Pingback: Dew Drop - July 23, 2008 | Alvin Ashcraft's Morning Dew
public 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.
OOPS 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
Pingback: So you want to learn NHibernate? (or, NHibernate Hyperlink Acupuncture) | The Freak Parade
Pingback: kjempekjekt.com » Blog Archive » Fem vekttall NHibernate
can u give me username and password for connecting to http://codecampserver.googlecode.com/svn/trunk/ ?
This took me 12 minutes :/
JK, Thanks for another great article Matt!
Your provider does not support extensibility
That is so sad =((