Note that there are some explanatory texts on larger screens.

plurals
  1. POC# Class library collections ObservableCollection<T> vs Collection<T>
    primarykey
    data
    text
    <p>I'm creating a class library that could be used in a number of situations ASP.NET, Console Apps, other class libraries and XAML targets like Silverlight or WPF.</p> <p>Initially I decided to expose collections as IList. But then when writing samples using XAML I found that if I want to make it easy to bind to these collections I need to use ObservableCollection.</p> <p>What are my options?</p> <p>I could make the library expose ObservableCollection and force that upon users who have nothing to do with XAML. Is that a bad thing?</p> <p>I could make my class generic allowing the caller to specify the collection type they want as long as it implements ICollection perhaps with default to Collection</p> <p>I could make a set of classes one that uses ObservableCollection and one that does not say Foo and ObservableFoo.</p> <p>I could implement INotifyCollectionChanged in my class but that seems silly when ObservableCollection does it for me.</p> <p>Obviously I'm trying to keep the code clean and simple, but supporting data binding seems important.</p> <p>Any suggestions?</p> <p>Edit: Tried creating a Portable Class Library project using both alternatives.</p> <p>In class Foo I have</p> <pre><code> private readonly Collection&lt;string&gt; strings = new Collection&lt;string&gt;(); public ReadOnlyCollection&lt;string&gt; Strings { get { return new ReadOnlyCollection&lt;string&gt;(this.strings); } } </code></pre> <p>In class ObservableFoo I have</p> <pre><code> private readonly ObservableCollection&lt;string&gt; strings = new ObservableCollection&lt;string&gt;(); public ReadOnlyObservableCollection&lt;string&gt; Strings { get { return new ReadOnlyObservableCollection&lt;string&gt;(this.strings); } } </code></pre> <p>The very simple unit test code is</p> <pre><code> [TestMethod] public void TestMethod1() { var foo = new ObservableFoo(); // or new Foo() Assert.AreNotEqual(0, foo.Id); Assert.AreNotEqual(0, foo.Strings.Count); } </code></pre> <p>The only downside is that when I used ReadOnlyObservableCollection the test project got this compile error</p> <blockquote> <p>The type 'System.Collections.ObjectModel.ReadOnlyObservableCollection`1' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Windows, Version=2.0.5.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e, Retargetable=Yes'</p> </blockquote> <p>So in this case, using ReadOnlyObservableCollection would force users to add a reference to System.Windows which is a downside.</p> <p><strong>Edit: I came up with a solution that I posted on my blog - see <a href="http://blogs.msdn.com/b/rjacobs/archive/2012/07/22/how-to-make-a-library-portable-and-data-binding-friendly-at-the-same-time.aspx" rel="nofollow">How to make a library portable and data binding friendly at the same time?</a></strong></p>
    singulars
    1. This table or related slice is empty.
    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.
 

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