I\'d like to expose a property on a view model that contains a list of objects (from database).
I need this collection to be read-only. That is, I want to prevent A
I don't like using ReadOnlyObservableCollection
as it seems like a mistake / broken class; I prefer a contract based approach instead.
Here is what I use that allows for covarience:
public interface INotifyCollection
: ICollection,
INotifyCollectionChanged
{}
public interface IReadOnlyNotifyCollection
: IReadOnlyCollection,
INotifyCollectionChanged
{}
public class NotifyCollection
: ObservableCollection,
INotifyCollection,
IReadOnlyNotifyCollection
{}
public class Program
{
private static void Main(string[] args)
{
var full = new NotifyCollection();
var readOnlyAccess = (IReadOnlyCollection) full;
var readOnlyNotifyOfChange = (IReadOnlyNotifyCollection) full;
//Covarience
var readOnlyListWithChanges =
new List>()
{
new NotifyCollection