PrimeDigit – A Design Blog by Will Shaver

August 26, 2008

ISession Extension for Saving Multiple Objects

Filed under: NHibernate,c# — Will @ 3:36 pm

Incredibly simple, but quite useful:

        public static void Save(this ISession session, params object [] entities)
        {
            foreach (var o in entities)
            {
                session.Save(o);
            }
        }

You can use it like:

session.Save(a,b,c,d,….);

with a list of objects of any type. Useful for unit testing where you want to save multiple items at once.

[Update]

Probably should call it something other than Save, such as SaveAll or SaveList because if you only pass two items to the overload it calls the standard Save(object, int id) version. :(

No Comments »

No comments yet.

RSS feed for comments on this post. TrackBack URL

Leave a comment

Powered by WordPress