FluentNHibernate Contrib (FNH.Contrib) Is Alive!


A few months ago, a coworker created a set of extension methods to turn NHibernate’s Criteria API into a more fluenty, strongly typed API. We’ve been using it in a production app for a few months, and I wanted to share it with the world. After talking about it with the other Fluent NHibernate contributors, we decided that it was not a good time to introduce new APIs and features into FNH right now (especially considering that we just removed Repository and Linq from FNH).

Thus, FNH.Contrib was born!

FluentNHibernate.Query

Right now the only project in FNH.Contrib is the fluent query API. The basic idea was to turn a standard NHibernate Criteria query, like this:

IList<Fault> faults = Criteria

             .Add(Expression.Eq("FaultNumber", faultNumber))

             .Add(Expression.Eq("AdminNumber", adminNumber))

             .CreateCriteria("UIC")

             .Add(Expression.Eq("UIC", uic.UIC))

             .SetMaxResults(1)

             .List<Fault>();

 

 if (faults != null && faults.Count > 0)

   fault = faults[0];

</div> </div>

Into a more strongly typed, no-magic-strings API, like this:

fault = Session.GetOne<Fault>()

             .Where(f => f.FaultNumber).IsEqualTo(faultNumber)

             .And(f => f.AdminNumber).IsEqualTo(adminNumber)

             .AndHasChild(f => f.UIC)

                    .Where(u => u.UIC).IsEqualTo(uic.UIC)

                    .EndChild()

             .Execute();

</div> </div>

The syntax is not perfect, by any means. However, it’s a great start and it’s been in a production app for several months now! I’m hoping to continue expanding this, cleaning up the syntax, etc, as I start using it in more projects.

Moving Forward and Other Contributions

At the moment, there are no other contributions in FNH.Contrib. However, I would love to get input and other projects up and running in it. There was some brief discussion of moving the FNH Repository and Linq APIs into. Perhaps that’s a good place to start?

I also plan to put in a complete Rake based automated build, and hopefully get a full suite of unit tests wrapped around the code, moving forward.

How To Contribute

FNH.Contrib is being hosted over at Github:

https://github.com/derickbailey/FNH.Contrib/tree/master

If you would like to contribute, just fork the master repository and start plugging your contributions in! When you have something ready to go, send me a pull request and we can start putting together a more complete contrib library.

Decoupling Workflow And Forms With An Application Controller