Tag Archive for '.net'

Extension Method of the Day: OrderBy with a Descending Flag

The title should say it all. Not sure why this didn’t make it into the System.Linq.Enumerable class, but this seems like a no-brainer. You need to sort a collection, but whether you sort ascending or descending is determined by a boolean argument instead of having to call a different method for each scenario.  Add this to your collection utilities class immediately:

        /// <summary>
        /// Sorts the elements of a sequence according to a key.
        /// </summary>
        /// <exception cref="System.ArgumentNullException">source or keySelector is null.</exception>
        /// <typeparam name="TSource">The type of the elements of source.</typeparam>
        /// <typeparam name="TKey">The type of the key returned by keySelector.</typeparam>
        /// <param name="source">A sequence of values to order.</param>
        /// <param name="keySelector">A function to extract a key from an element.</param>
        /// <param name="descending">if set to <c>true</c>, sorts the elements in descending order; otherwise in ascending order.</param>
        /// <returns>An System.Linq.IOrderedEnumerable<TElement> whose elements are sorted according to a key.</returns>
        public static IOrderedEnumerable<TSource> OrderBy<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, bool descending)
        {
            if (descending)
                return source.OrderByDescending(keySelector);
 
            return source.OrderBy(keySelector);
        }

nHibernate in the Enterprise

I just recently received this response regarding the use of nHibernate within the division of the company I am working with:

… [nHibernate] has alot of promise I think but [coorporate] doesn’t consider it “stable” yet …

Are you kidding me? Where do these guys get their information? I guess the Microsoft Entity Framework is stable enough though.

Lets see, nHibernate is a port of the Java Hibernate Framework which was first released on November 30, 2001 (making it almost 8 years old). The major 1.2.1 version of nHibernate was released in November 2007, improving on the already stable port of the the Hibernate framework, adapting .Net 2.0 language features.

I began using the Hibernate framework in the Fall of 2005 being introduced to it by my colleague Winston Fassett. I switched over to .Net development in the Fall of 2006, and have been using nHibernate from the start.

Having used nHibernate since before the 1.2.1 release, I can safely vouch for its stability and performance … visit http://racenation.com for an example of my most recent nHibernate project.

I can’t say that some of the surrounding projects like Linq to nHibernate, although very cool, can be considered stable, but how long does a framework need to be around and used to be considered stable?