views:

266

answers:

2

I have a function that identify coordinates on a page, and I am returning them as a

Dictionary<int, Collection<Rectangle>> GetDocumentCoordinates(int DocumentId)

However, later I need information about each page - if it was validated, what is the page resolution, color/bw, etc. I could create another function and run through pretty much the same result set as the previous function and get that information.

Dictionary<int, PageInfo> GetDocumentAttributes(int DocumentId)

Another alternative would be to add a ref parameter so I can get these values back.

Dictionary<int, Collection<Rectangle>> GetCoordinates(int DocumentId, ref Dictionary<int, PageInfo> PageAttributes)

Yet another alternative is to create an encompassing class that contains the Dictionary and the page information:

class DocumentInfo
{
    Dictionary<int, Collection<Rectangle>> Coordinates { get; set;}
    Dictionary<int, PageInfo> PageAttributes { get; set; }
}

and then define:

DocumentInfo GetDocumentInfo(int DocumentId);

I'm leaning towards the last option, but your insights are very much appreciated.

+1  A: 

It seems like you need a lot of data out. The last option should be fine, and is extensible; it you wanted (to simplify the Dictionary<,> usage), you could encapsulate things a bit more, but the fact that C# doesn't directly support named indexed properties means you'd need a few classes, unless you just wrap with methods like:

class DocumentInfo {
    Dictionary<int, Collection<Rectangle>> rectangles = ...
    public Collection<Rectangle> GetRectangles(int index) {
        return rectangles[index]; // might want to clone to
                                  // protect against mutation
    }
    Dictionary<int, PageInfo> pages = ...
    public PageInfo GetPageInfo(int index) {
        return pages[index];
    }

 }

I'm not quite clear what the int is, so I can't say whether this is sensible (so I've just left it alone).

Also - with the first option, you probably wouldn't need ref - it would be sufficient to use out.

Marc Gravell
Named, indexed properties would be pretty simple to implement, whether you use an event-based model or simply threw in a couple lambdas to handle enumeration/indexing...
Will
Simple yes - just extra classes ;-p
Marc Gravell
@Mark - thank you for the input; even though I set the other answer as "correct" it was just because I had to choose one, I would mark yours as well if I could.
Otávio Décio
I love you guys! GROUP HUG!
Will
+5  A: 

The last option is definitely the best. I've found that, when taking or returning complex data with multiple meanings, creating a complex type to encapsulate this data is the best practice for a number of reasons.

First, your return data probably will change as your design changes. Encapsulating this data in an object allows you to alter what it carries and how your methods operate on this data without altering the interfaces of your objects. Obviously, your data object shouldn't implement an interface; at most, have a base class with the minimum interface and then pass references to the base around.

Second, you may find your data gets complex to the point where you will need to perform validation on it. Rather than have this validation in all the methods of your classes where you act upon this data, you can easily wrap this up in the data class. Single responsibility, etc.

Will