Friday, January 14, 2011 10:56 PM Sean Feldman

Creating collection with no code (almost)

When doing testing, I tend to create an object mother for the items generated multiple times for specifications. Quite often these objects need to be a part of a collection. A neat way to do so is to leverage .NET params mechanism:

    public static IEnumerable<T> CreateCollection<T>(params T[] items)
    {
      return items;
    }

And usage is the following:

private static IEnumerable<IPAddress> addresses = CreateCollection(new IPAddress(123456789), new IPAddress(987654321));
Filed under:

Comments

# re: Creating collection with no code (almost)

Saturday, January 15, 2011 8:55 PM by eppyjerk

why wouldn't you do:

private static IEnumerable<IPAddress> addresses = new IPAddress[]{ new IPAddress(""), new IPAddress("")};

# re: Creating collection with no code (almost)

Saturday, January 15, 2011 9:06 PM by James

This seems pointless...... can't you just use object initialization syntax?

private static IEnumerable<IPAddress> addresses = new IPAddress[] { new IPAddress(123456789), new IPAddress(123456789) };

It makes maintenance a nightmare when you fill your code with unnecessary library methods. It also makes it very hard for a new programmer to come in and work on your code when you use non-standard conventions and library functions.

Every c# programmer knows object initialization, and will instantly understand what your doing with it, conversely no-one understands the purpose of "CreateCollection" until they look at the source code.

# re: Creating collection with no code (almost)

Saturday, January 15, 2011 9:54 PM by marianor

I wonder why you don't use:

private static IEnumerable<IPAddress> addresses = new [] { new IPAddress.... };

# re: Creating collection with no code (almost)

Monday, January 17, 2011 8:02 PM by Sean Feldman

@eppyjerk, @james, @marianor:

As a matter of fact I do. This is just something funny my pair and I ran into. And James, this is not an "unnecessary library" - this is an extension method we are using as a part of specific object Object Mother. I would suggest not to jump into conclusions :)

The original code looks like this:

IPAddress_ObjectMother.CreateCollection(IPAddress_ObjectMother.CreateOne(),  IPAddress_ObjectMother.CreateOne())

I really don't care about the values (for the most part), but references to the objects. Probably should be a bit more clear on that :)

Thank you for the comments.

# re: Creating collection with no code (almost)

Saturday, January 29, 2011 9:24 PM by Will

Sean, I might reconsider the name since you aren't really returning a Collection.

I'm also leaning toward the other guys comments.  

For your specific scenario another, perhaps not as elegant for the implementation of the helper method, but easier for the consumer...

IPAddress_ObjectMother.CreateMany(2)

static IEnumerable<IPAddress> CreateMany( int count )

{

 return Enumerable.Range(1,count).Select(i => IPAddress_Mother.CreateOne());

}

Regardless, what you have is quite slick.

# MS Visual Studio 2010 &raquo; Blog Archive &raquo; Thematic links. 14.01.2011

Pingback from  MS Visual Studio 2010  &raquo; Blog Archive   &raquo; Thematic links. 14.01.2011