thoughts on coding

April 17, 2009

Fluently with NHibernate

Filed under: Uncategorized — Frantisek @ 8:06 pm

I’m big fan of the fluent interface pattern very much and I try to use it where it makes sense. The pattern is nicely explained on Fowler’s bliki.

In the DAL layer I use NHibernate as O/R mapper and I use ICriteria for quite much cases (especialy when formulating the search criteria for the lists – serach criteria depends on what was filled from the user).

So let’s start.

NHibernate tells the following how to create the criteria (copied from the documentation):

            IList cats = Session.CreateCriteria(typeof (Cat))
                .Add(Expression.Like("name", "Iz%"))
                .Add(Expression.Gt("weight", minWeight));

I wanted to make the creation more fluent like the following:

            IList cats = session.CreateCriteria(typeof(Cat))
                .Add( "name", Is.Like("Iz%") )
                .Add("weight", Is.Gt(minWeight));

I think this is more fluent and it’s possible to read it as 1 sentence which makes the sense, isn’t?

What is necessary to do?

NHibernate extensions:

 /// <summary>
    /// NHibernate extensions
    /// </summary>
    public static class NHibernateExtensions
    {
        /// <summary>
        /// Adds the specified criteria.
        /// </summary>
        /// <param name="criteria">The criteria.</param>
        /// <param name="propertyName">Name of the property.</param>
        /// <param name="expression">The expression.</param>
        /// <returns></returns>
        public static ICriteria Add(this ICriteria criteria, string propertyName, Func<string, ICriterion> expression)
        {
            if (expression.Is())
            {
                ICriterion criterion = expression(propertyName);
                if (criterion.Is())
                {
                    criteria.Add(criterion);
                }
            }
            return criteria;
        }
    }

and the Is class:


public class Is
    {
        /// <summary>
        /// Equals to.
        /// </summary>
        /// <param name="value">The value.</param>
        public static Func<string, ICriterion> EqualTo(object value)
        {
            return (name => new SimpleExpression(name, value, IsConstants.SignEqual, false));
        }
        /// <summary>
        /// Equals to.
        /// </summary>
        /// <param name="value">The value.</param>
        public static Func<string, ICriterion> EqualTo(int? value)
        {
            if (value.Is())
            {
                return (name => new SimpleExpression(name, value, IsConstants.SignEqual, false));
            }
            else
            {
                return null;
            }
        }
        /// <summary>
        /// Likes the specified name.
        /// </summary>
        /// <param name="value">The value.</param>
        /// <returns></returns>
        public static Func<string, ICriterion> Like(string value)
        {
            if (value.IsNullOrEmpty()) return null;
            return (name => new LikeExpression(name, value, MatchMode.Anywhere, null, true));
        }
    }

I was inspired by NBehave or NUnit libraries which are made very fluent.

What do you think about such added fluency with NHibernate?

Advertisements

1 Comment »

  1. I like it. Have u done further work on the subject ?
    I found something similar at
    http://code.google.com/p/nhlambdaextensions/

    Comment by jim tanner — August 17, 2009 @ 5:26 pm


RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Create a free website or blog at WordPress.com.

%d bloggers like this: