This week marked the end of my stint on one of the biggest projects we have had in the last year. Things went very well, as the remaining members of the team begin to move into the final beta phase, I am being reassigned as a billable consultant to a company in Holland, MI. The upside of this assignment is I get to work with PHP which is one of the first languages I did serious programming in and was my forte in college. Getting the opportunity to work with it for the first time since I became strictly .NET is going to be fun, challenging, and exciting; its an opportunity that I am very much looking forward to. The only downside is my current 10m commute to work now becomes an hour long commute as I must be onsite in Holland, but I think its well worth it.
One of the other happenings is I have finally started to take a serious looking into NHibernate as something that RCM may go to from our current open source custom framework (The Kinetic Framework). Its part of the normal aging process for a framework. NHibernate is highly touted and looks to be a good alternative to using the still not ready for prime time Entity Framework. However, one of the long standing problems with NHibernate, is configuration and that really is true for most ORMs that exist today. Microsoft has generally released good tools built into Visual Studio to aid programmers in configuring the ORM products it releases, this is not the case for NHibernate. To counter this, Fluent NHibernate was created that allows you to “fluently” define mappings for entity objects.
The main advantage behind this notion of fluent programming is that we take procedures that normally are relegated to external programming and bring them under the control of the compiler, to allow for easier compile time checking. An example of this is Microsoft LINQ – where developers are able to querying databases (for example) using standard type safe C# code as opposed to passing a string off to a black box (the database) and magically getting a rectangular result set.
This is the same idea with Fluent NHibernate which removes the XML configuratioin files and allows you to define this configuration using C#, below is an example:
1: public class SeriesMap : ClassMap
2: {
3: public SeriesMap()
4: {
5: Id(x => x.SeriesId);
6: Map(x => x.Name).WithLengthOf(100).Not.Nullable();
7: References(x => x.Studio).TheColumnNameIs("StudioId");
8: HasMany(x => x.Seasons).WithKeyColumn("SeasonId");
9: HasManyToMany(x => x.Genres)
10: .WithTableName("SeriesGenres")
11: .WithParentKeyColumn("SeriesId")
12: .WithChildKeyColumn("GenreId");
13: }
14: }
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, “Courier New”, courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
This being the mapping file, it would help to show you what the entity itself looks like:
1: public class Series
2: {
3: public virtual int SeriesId { get; set; }
4: public virtual string Name { get; set; }
5: public virtual IList Seasons { get; set; }
6: public virtual Studio Studio { get; set; }
7: public virtual IList Genres { get; set; }
8: }
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, “Courier New”, courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
Note that we define properties we want NHibernate to “fill” as virtual. But take note of the correlation between the property names and the calls to properties in the mapping class. The link below will provide more of a reference to the various calls available from the mapping class:
http://wiki.fluentnhibernate.org/show/HomePage & http://fluentnhibernate.org/
Once you have these classes in place you can use the standard NHibernate calls, either via the Criteria API or HQL (Hibernate Query Language) to extract your data. Also have a look at the Linq to NHibernate project. While it is still in its infancy stages it has great potential. I think Linq and NHibernate are actually made for each other as the DataContext pattern that Microsoft employs to track changes matches very well with the repository pattern employed by NHibernate, I look forward to some great stuff in the future from the meshing of these two technologies.