Archive for IList tag
Multiple-inheritance, composition and single responsibility principle in .NET
.NET is often chided by C++ developers for failing to support multiple-inheritance. The reply is often Favor object composition over class inheritance – a mantra chanted from everywhere including the opening chapters of the Gang of Four’s Design Patterns book.
If the accepted mantra is that your object should expose interfaces and delegate the implementation of those interfaces elsewhere then it could really do with some better support than .NET currently offers especially where the interface comprises more than a member or two.
Consider the following fragment of a class for customer price-lists (properties and methods omitted). We decide to support IList<ProductPrice> so that consumers of our class can add, remove and iterate over the prices in a familiar manner (principle of least surprise).
public class CustomerPriceList : IList<ProductPrice> {
private List<ProductPrice> productPrices = new List<ProductPrice>();
public Customer Customer;
}
Implement interface
Visual Studio offers some assistance where you can choose Implement interface IList<ProductPrice> which gives you all the method definitions with the very unhelpful body of throwing an exception of “This method or operation is not implemented”. It requires some work to fill in all these definitions to something that works:
public class CustomerPriceList : IList<ProductPrice> {
private List<ProductPrice> productPrices = new List<ProductPrice>();
public Customer Customer;
public int IndexOf(ProductPrice item) {
return productPrices.IndexOf(item);
}
public void Insert(int index, ProductPrice item) {
productPrices.Insert(index, item);
}
public void RemoveAt(int index) {
productPrices.RemoveAt(index);
}
public ProductPrice this[int index] {
get { return productPrices[index]; }
set { productPrices[index] = value; }
}
public void Add(ProductPrice item) {
productPrices.Add(item);
}
public void Clear() {
productPrices.Clear();
}
public bool Contains(ProductPrice item) {
return productPrices.Contains(item);
}
public void CopyTo(ProductPrice[] array, int arrayIndex) {
productPrices.CopyTo(array, arrayIndex);
}
public int Count { get { return productPrices.Count; } }
public bool IsReadOnly { get { return ((IList)productPrices).IsReadOnly; } }
public bool Remove(ProductPrice item) {
return productPrices.Remove(item);
}
public IEnumerator<ProductPrice> GetEnumerator() {
return productPrices.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator() {
return ((IEnumerable) productPrices).GetEnumerator();
}
}
This is a lot of effort for a cluttered solution.
Use inheritance to subclass List<T>
public class CustomerPriceList : List<ProductPrice> {
public Customer Customer;
}
Small amount of code but not an option if you have a class hierarchy in place or need to implement multiple interfaces.
Expose IList<ProductPrice> property directly
public class CustomerPriceList : IListable<ProductPrice> {
private List<ProductPrice> productPrices = new List<ProductPrice>();
public Customer Customer;
public IList<ProductPrice> ProductPrices { get { return productPrices; } }
}
This works but means CustomerPriceList can not control any of the IList implementation such as validation.
Methods may also start accepting IList<ProductPrice> instead of CustomerPriceList because developers imagine the parts to be more decoupled than they actually are and are encouraged to code to interfaces not concrete classes.
Refactoring away from this at a later date would require a IList<ProductPrice> wrapper than delegated calls back to the containing class to prevent an interface-breaking change.
Introduce interface to declare IList<ProductPrice> available
Add an interface that signifies a IList<ProductPrice> can be obtained by calling the named method, e.g.
public interface IListable<T> {
IList<T> GetList();
}
This is a similar pattern to that of IEnumerable<T> and IEnumerator<T> whereby one interface signifies the availability of the other. In this example our class would look like:
public class CustomerPriceList : IListable<ProductPrice> {
private List<ProductPrice> productPrices = new List<ProductPrice>();
public Customer Customer;
public IList<ProductPrice> GetList() {
return productPrices;
}
}
Which is less code, a closer adherence to single responsibility principle (SRP) and the ability to change without breaking the interface although it still does nothing to prevent passing IList or IListable interfaces where CustomerPriceList would be more suitable. An IPriceList class could be introduced although it starts to feel like abstract infinity.
Improved support from .NET
I’d really like to see .NET improve on the support for interfaces and composition, like perhaps the following:
public class CustomerPriceList : IList<ProductPrice> goto productPrice {
private IList<ProductPrice> productPrice = new IList<ProductPrice>();
public Customer Customer;
}
This would signify to the compiler that all IList<T> interfaces should be wired up to the productPrice variable unless explicitly defined and gives goto a whole new lease of life ;-)
[)amien
Observing changes to a List<T> by adding events
In an attempt to get more C# and .NET content up I’m putting up some snippets I’ve put together in response to enquiries on some C# user support groups. Many of them are not particularly advanced but they are quite useful.
This sample shows how to observe events on an generic IList<T>. It does this by way of implementing the IList<T> interface over the top of something that already supports IList<T> to do the actual work and highlights how useful publishing the interface, IList<T>, separate from the actual concrete class List<T> can be for reuse.
using System;
using System.Collections;
using System.Collections.Generic;
public class ObservableList<T> : IList<T> {
private IList<T> internalList;
public class ListChangedEventArgs : EventArgs {
public int index;
public T item;
public ListChangedEventArgs(int index, T item) {
this.index = index;
this.item = item;
}
}
public delegate void ListChangedEventHandler(object source, ListChangedEventArgs e);
public delegate void ListClearedEventHandler(object source, EventArgs e);
public event ListChangedEventHandler ListChanged;
public event ListClearedEventHandler ListCleared;
public ObservableList() {
internalList = new List<T>();
}
public ObservableList(IList<T> list) {
internalList = list;
}
public ObservableList(IEnumerable<T> collection) {
internalList = new List<T>(collection);
}
protected virtual void OnListChanged(ListChangedEventArgs e) {
if (ListChanged != null)
ListChanged(this, e);
}
protected virtual void OnListCleared(EventArgs e) {
if (ListCleared != null)
ListCleared(this, e);
}
public int IndexOf(T item) {
return internalList.IndexOf(item);
}
public void Insert(int index, T item) {
internalList.Insert(index, item);
OnListChanged(new ListChangedEventArgs(index, item));
}
public void RemoveAt(int index) {
T item = internalList[index];
internalList.Remove(item);
OnListChanged(new ListChangedEventArgs(index, item));
}
T this[int index] {
get { return internalList[index]; }
set {
internalList[index] = value;
OnListChanged(new ListChangedEventArgs(index, value));
}
}
public void Add(T item) {
internalList.Add(item);
OnListChanged(new ListChangedEventArgs(internalList.IndexOf(item), item));
}
public void Clear() {
internalList.Clear();
OnListCleared(new EventArgs());
}
public bool Contains(T item) {
return internalList.Contains(item);
}
public void CopyTo(T[] array, int arrayIndex) {
internalList.CopyTo(array, arrayIndex);
}
public int Count {
get { return internalList.Count; }
}
public bool IsReadOnly {
get { return IsReadOnly; }
}
public bool Remove(T item) {
lock(this) {
int index = internalList.IndexOf(item);
if (internalList.Remove(item)) {
OnListChanged(new ListChangedEventArgs(index, item));
return true;
}
else
return false;
}
}
public IEnumerator<T> GetEnumerator() {
return internalList.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator() {
return ((IEnumerable) internalList).GetEnumerator();
}
}
[)amien