-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: Modify Expression.In(...) to let it acceptable IEnumerable
PostPosted: Sat Dec 02, 2006 6:12 pm 
Beginner
Beginner

Joined: Wed Dec 28, 2005 3:14 pm
Posts: 29
Currently there are two overloads of the Expression.In method:
Code:
public static AbstractCriterion In( string propertyName, object[ ] values )
public static AbstractCriterion In( string propertyName, ICollection values )


However, since now generic collections are widely used, these two overload are not very convenient for creating InExpression using generic collections.
I think a overload to accept IEnumerable could be helpful. Thus I wrote two:
Code:
  /// <summary>
        /// Apply an "in" constraint to the named property
        /// </summary>
        /// <param name="propertyName">The name of the Property in the class.</param>
        /// <param name="values">An IEnumerable of values.</param>
        /// <returns>An <see cref="InExpression" />.</returns>
        public static AbstractCriterion In(string propertyName, IEnumerable values)
        {
            IList l = new ArrayList();
            foreach (object o in values) {
                l.Add(o);
            }
            return In(propertyName, l);
        }

This one is the most simple implementation. However it has two problems:
first, its signature could be confusing with the In( string propertyName, ICollection values ). since ICollection also inherits IEnumerable;
second, it relies on the performance of the ArrayList.Add method. For big IEumerable it could introduce performance issue.

The other implementation I wrote is as the following:
Code:

        /// <summary>
        /// Apply an "in" constraint to the named property
        /// </summary>
        /// <param name="propertyName">The name of the Property in the class.</param>
        /// <param name="values">An IEnumerable of values.</param>
        /// <param name="length">the number of the items in the IEnumerable that should be included in the expression.
        /// It should always be larger than or equal to the number of the items of the IEnumerable
        /// </param>
        /// <returns>An <see cref="InExpression" />.</returns>
        /// <exception cref="ArgumentOutOfRangeException">thrown when length is less than zero or is larger than the number of items in the IEnumerable </exception>
        public static AbstractCriterion In(string propertyName, IEnumerable values, int length)
        {
            object[] ary = new object[length];
            if (length > 0)
            {
                int i = 0;
                foreach (object o in values)
                {
                    ary[i] = o;
                    i++;
                    if (i >= length) break;
                }
                if (i <= length)
                    throw new ArgumentOutOfRangeException(
                        "length should be smaller than or equal to the number of items in the IEnumerable");
            }
            else
                throw new ArgumentOutOfRangeException(
                    "length should be no less than 0");
            return In(propertyName, ary);
        }

This implementation has a different signature that won't messed with the other two existing one. It introduced a new parameter : length, which indicates the number of the items in the IEnumerable that need to be included in the in expression. This implementation also eliminate the dependency on ArrayList. However, it also has two problems:
first, the signature is two different from the existing ones, user needs to learn the usage of the new parameter.
second, the implementation is more complex and thus more subject to errors.
I listed both of the two implementations here because neither of them are perfect. But I do think that if the Expression.In can accept a IEnumerable rather than ICollection, it can be beneficial.
Thanks,


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.