Designing a type to return from a method

4 views
Skip to first unread message

Abraham Shilon

unread,
May 6, 2012, 4:59:05 AM5/6/12
to altnet...@googlegroups.com
Hi,

I need to design this type, to be returned from a method. 
Now this method (let's just call this method GetValue), should return either a single instance of this type, or a collection of instances from this type.

Here's some pseudo code to make things hopefully clearer:

The returned type's initial definition (later on i tried to make several modifications to it to make it suitable for my needs):
public class ResultField
{
    public string FieldName { get; set;}
    public string FieldValue { get; set;}
}

public class DataHandler
{
    public ResultField/IEnumerable<ResultField> GetValue(someKey) // Returns either ResultField or a collection of it
    {
        // Some decision making code
        if someKey is bla then return ResultField
        else return IEnumerable of ResultField
    }
}

And here's the hopefully somewhat improved version of ResultField (so no need to use a collection anymore):
public class ResultField
{
    public string FieldName { get; set;}
    public string FieldValue { get; set;}
    public IEnumerable<ResultField> Rows {get; set;}
}

But i still don't really like this solution. It contains fields that might not be relevant (if a single instance is needed, Rows is irrelevant, and vice versa).

Then I thought maybe use Composite pattern here somehow... but i'm not really on to something satisfactory (factory maybe? :)) here.

Any suggestions? 

P.S
I'm not versed into using the dynamic features of .net 4.0, so if dynamic might help here somehow, that would be great too.

Thanks,
Avi

Ariel Raunstien

unread,
May 6, 2012, 5:08:04 AM5/6/12
to altnet...@googlegroups.com
Why not always return a collection?


Ariel Raunstien
+972-522-747408



--
You received this message because you are subscribed to the Google Groups "altnetisrael" group.
To post to this group, send email to altnet...@googlegroups.com.
To unsubscribe from this group, send email to altnetisrael...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/altnetisrael?hl=en.

Abraham Shilon

unread,
May 6, 2012, 5:09:19 AM5/6/12
to altnet...@googlegroups.com
I was asked not to do so :)

Avishay Lavie

unread,
May 6, 2012, 5:12:06 AM5/6/12
to altnet...@googlegroups.com
The missing info here is what you plan to do with this data once it's returned. A single value is very different than a collection of values so you won't be able to do anything with it without type-checking ("if it's a singular I'll do X, otherwise I'll do Y"). Are clients expected to know which kind they get back? Or are they expected to handle both cases?

Also, what do you mean "I was told not to do so"? If the requirements of the code is that you return either one or multiple values, and one value is a particular case of many values, why not do that? Or do you explicitly want to differentiate between "this has one value" and "this is a collection of values with a single member"?

In short: more context, please.

Avish

Abraham Shilon

unread,
May 6, 2012, 5:23:43 AM5/6/12
to altnet...@googlegroups.com
Consumers of this code don't know in advance what they get, a single instance or a collection.
So you're right, they will have some type-checking logic.
And, I guess they want this behavior for a reason, not sure why though... oh crap... 

Avishay Lavie

unread,
May 6, 2012, 5:27:10 AM5/6/12
to altnet...@googlegroups.com
If the singular/multiple results have nothing in common, and clients will type-check anyway, then you can just return an empty interface and have two implementations, SingleValue { T Value { get; } } and MultipleValues { T[] Values { get; } }. 

But there seems to be a larger issue here about why this is needed in the first place. The requirement smells.

Avish

Ariel Raunstien

unread,
May 6, 2012, 5:27:49 AM5/6/12
to altnet...@googlegroups.com
I you can't convince them that a collection is always best - return an "IResult" which is implemented by "SingleResult" and "MultipleResults".
At which point they will understand that this sucks, change their minds, and request that you always return a collection.




Ariel Raunstien
+972-522-747408



Ariel Raunstien

unread,
May 6, 2012, 5:29:00 AM5/6/12
to altnet...@googlegroups.com
Goddamnit, refresh faster, GMail!


Ariel Raunstien
+972-522-747408



Avishay Lavie

unread,
May 6, 2012, 5:31:35 AM5/6/12
to altnet...@googlegroups.com
You know what they say: great minds have a tendency to group-think. We're such a cargo cult. Ugh.

Abraham Shilon

unread,
May 6, 2012, 5:34:48 AM5/6/12
to altnet...@googlegroups.com
Great! Unanimity is good :)
Thanks guys, and I hope that I convince these guys that this smells. 
Reply all
Reply to author
Forward
0 new messages