Note that there are some explanatory texts on larger screens.

plurals
  1. POC# Property Access vs Interface Implementation
    primarykey
    data
    text
    <p>I'm writing a class to represent a <a href="http://www.getpivot.com" rel="nofollow noreferrer">Pivot</a> Collection, the root object recognized by Pivot. A Collection has several attributes, a list of facet categories (each represented by a <code>FacetCategory</code> object) and a list of items (each represented by a <code>PivotItem</code> object). Therefore, an extremely simplified Collection reads:</p> <pre><code>public class PivotCollection { private List&lt;FacetCategory&gt; categories; private List&lt;PivotItem&gt; items; // other attributes } </code></pre> <p>What I'm unsure of is how to properly grant access to those two lists. Because declaration order of both facet categories and items is visible to the user, I can't use sets, but the class also shouldn't allow duplicate categories or items. Furthermore, I'd like to make the Collection object as easy to use as possible. So my choices are:</p> <ol> <li><p>Have <code>PivotCollection</code> implement <code>IList&lt;PivotItem&gt;</code> and have accessor methods for <code>FacetCategory</code>: In this case, one would add an item to Collection <code>foo</code> by writing <code>foo.Add(bar)</code>. This works, but since a Collection is equally both kinds of list making it only pass as a list for one type (category or item) seems like a subpar solution.</p></li> <li><p>Create nested wrapper classes for <code>List</code> (<code>CategoryList</code> and <code>ItemList</code>). This has the advantage of making a consistent interface but the downside is that these properties would no longer be able to serve as lists (because I need to override the non-virtual <code>Add</code> method I have to implement <code>IList</code> rather than subclass <code>List</code>. Implicit casting wouldn't work because that would return the <code>Add</code> method to its normal behavior.</p></li> </ol> <p>Also, for reasons I can't figure out, <code>IList</code> is missing an <code>AddRange</code> method...</p> <pre><code>public class PivotCollection { private class CategoryList: IList&lt;FacetCategory&gt; { // ... } private readonly CategoryList categories = new CategoryList(); private readonly ItemList items = new ItemList(); public CategoryList FacetCategories { get { return categories; } set { categories.Clear(); categories.AddRange(value); } } public ItemList Items { get { return items; } set { items.Clear(); items.AddRange(value); } } } </code></pre> <p>Finally, the third option is to combine options one and two, so that <code>PivotCollection</code> implements <code>IList&lt;PivotItem&gt;</code> and has a property <code>FacetCategories</code>. </p> <p><strong>Question:</strong> Which of these three is most appropriate, and why?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload