Los Techies : Blogs about software and anything tech!

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];

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

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:

http://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.

Kick It on DotNetKicks.com
Posted May 13 2009, 10:14 PM by derick.bailey

Comments

sirrocco wrote re: FluentNHibernate Contrib (FNH.Contrib) Is Alive!
on 05-14-2009 12:11 AM

And you're not using Linq To Nh ... why ?

Liam McLennan wrote re: FluentNHibernate Contrib (FNH.Contrib) Is Alive!
on 05-14-2009 1:44 AM

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

harrisonmeister wrote re: FluentNHibernate Contrib (FNH.Contrib) Is Alive!
on 05-14-2009 1:50 AM

Check out this nhlambdaextensions.googlecode.com/.../NhLambdaExtensions.html

Does something quite similar

Reflective Perspective - Chris Alcock » The Morning Brew #347 wrote Reflective Perspective - Chris Alcock &raquo; The Morning Brew #347
on 05-14-2009 3:28 AM

Pingback from  Reflective Perspective - Chris Alcock  » The Morning Brew #347

DotNetShoutout wrote FluentNHibernate Contrib (FNH.Contrib) Is Alive! - Derick Bailey - Los Techies
on 05-14-2009 4:30 AM

Thank you for submitting this cool story - Trackback from DotNetShoutout

schambers wrote re: FluentNHibernate Contrib (FNH.Contrib) Is Alive!
on 05-14-2009 2:49 PM

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?

Steve Bohlen wrote re: FluentNHibernate Contrib (FNH.Contrib) Is Alive!
on 05-14-2009 3:15 PM

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.

Thoughts--?

derick.bailey wrote re: FluentNHibernate Contrib (FNH.Contrib) Is Alive!
on 05-14-2009 3:58 PM

@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.

Neal Blomfield wrote re: FluentNHibernate Contrib (FNH.Contrib) Is Alive!
on 05-14-2009 4:27 PM

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

Tobin Harris wrote re: FluentNHibernate Contrib (FNH.Contrib) Is Alive!
on 05-14-2009 7:41 PM

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 wrote re: FluentNHibernate Contrib (FNH.Contrib) Is Alive!
on 05-18-2009 5:52 AM

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

Brett Veenstra wrote re: FluentNHibernate Contrib (FNH.Contrib) Is Alive!
on 05-18-2009 1:27 PM

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

       }

#.think.in wrote #.think.in infoDose #29 (11th May - 15th May)
on 05-22-2009 7:47 AM

#.think.in infoDose #29 (11th May - 15th May)

ryzamm wrote re: FluentNHibernate Contrib (FNH.Contrib) Is Alive!
on 05-29-2009 3:54 AM

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

               .ThatHasChild<Subject>(c => c.Subject)

I cannot call endchild here.

Thanks

Rob Gibbens wrote re: FluentNHibernate Contrib (FNH.Contrib) Is Alive!
on 05-29-2009 9:03 AM

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

derick.bailey wrote re: FluentNHibernate Contrib (FNH.Contrib) Is Alive!
on 05-29-2009 10:06 AM

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

wiki.github.com/.../withfetchmodeon

#.think.in wrote #.think.in infoDose #29 (11th May - 15th May)
on 05-30-2009 6:16 PM

#.think.in infoDose #29 (11th May - 15th May)

ryzamm wrote re: FluentNHibernate Contrib (FNH.Contrib) Is Alive!
on 06-01-2009 6:45 AM

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<T> instead of ThatHasChild<T> which is for me have different meaning as for me

Copyright Los Techies 2008, 2009. All rights reserved.
Powered by Community Server (Commercial Edition), by Telligent Systems