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!


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))


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




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

   fault = faults[0];

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)



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:


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.

About Derick Bailey

Derick Bailey is an entrepreneur, problem solver (and creator? :P ), software developer, screecaster, writer, blogger, speaker and technology leader in central Texas (north of Austin). He runs SignalLeaf.com - the amazingly awesome podcast audio hosting service that everyone should be using, and WatchMeCode.net where he throws down the JavaScript gauntlets to get you up to speed. He has been a professional software developer since the late 90's, and has been writing code since the late 80's. Find me on twitter: @derickbailey, @mutedsolutions, @backbonejsclass Find me on the web: SignalLeaf, WatchMeCode, Kendo UI blog, MarionetteJS, My Github profile, On Google+.
This entry was posted in .NET, Community, Fluent NHibernate, FNH.Contrib, Git, NHibernate. Bookmark the permalink. Follow any comments here with the RSS feed for this post.
  • sirrocco

    And you’re not using Linq To Nh … why ?

  • This looks like a great improvement, although a contrib to a contrib is a bit scary.

  • harrisonmeister
  • Wouldn’t this fit better in the NHibernate Contrib since it deals with the criteria api and not fluent nhibernate directly?

    Or am I missing something?

  • Without looking at the code, this would appear to be an extension to an ISession impl. rather than an ICriteria impl. As such it looks like its tied directly to a specific Session lifetime whereas basing in on ICriteria would support using it within a DetachedCriteria instance and permit it to be independent of whatever session lifecycle usage pattern the consuming app might be using.


  • @sean chambers,

    ya – it may be a better fit in NHContrib, right now. Part of the discussion with the other FNH authors was around how we would eventually like FNH to be the one-stop-shop for all things fluenty, around NHIberate. It is the “Fluent NHibernate” project, after-all, not just the fluent mapping project. The intention was to eventually re-evaluate FNH.Contrib’s functionality and see whether or not it would be moved into the FNH core.

    @steve bohlen,

    correct – at the moment, it is a set of extension methods based on the ISession. I like the thought of making an extension set to ICriteria, to facilitate detached criteria, though. will look into that.

  • Looks a lot like the NH lambda extensions stuff. Might be worthwhile looking up http://code.google.com/p/nhlambdaextensions/ and checkign out http://broloco.blogspot.com/2008/12/using-lambda-expressions-with.html to see if these guys have some useful things to contribute.

  • Awesome, something that makes Critera more accessible us a welcome step from my persepctive :)

    NHibernate is unfortunately a quagmire of confusion when it comes to satellite project names. “FNH.Contrib” is a step too far in my eyes, what about NHibernate.FluentCriteria as an alternative name?

  • Marcin Rybacki

    To be honest, more strongly type convention is hardly readable… Although there’s a benefint of strong typying we’re there’s some kind of loss, here…

  • Looks like a great start!!!

    Can you add this?

    public T IsLike(TV value)
    Criteria.Add(Restrictions.Like(PropertyInfo.Name, value));

    return GetConjunction();

    public T IsLikeInsensitive(TV value)
    Criteria.Add(Restrictions.InsensitiveLike(PropertyInfo.Name, value));

    return GetConjunction();

  • ryzamm

    Any plan or raodmap that stated detach criteria will be enable?

    I make small changes local to add
    public TConj EndChild()
    return Conjunction;

    to NHibernateChileQuery class becasue when i use

    var q = Session.GetAll()
    .ThatHasChild(c => c.Subject)
    I cannot call endchild here.

  • Rob Gibbens

    Is there any documentation, or even forums, around this project? I’m trying to figure out how to use WithFetchModeOn()

  • @Brett Veenstra and @ryzamm,

    I’ve added the methods that you both requested. sorry it took so long. I’ll do better about keeping up, in the future.

    @Rob Gibbens,

    I added a wiki page at github to document how to use WithFetchModeOn()


  • ryzamm

    I thought better if the method function follow standar naming what we have in FluentNHibernate

    for example..if we have manytoone relation i think its better to use the same name Reference instead of ThatHasChild which is for me have different meaning as for me