Generic Lists are All You Will Ever Need
Generic lists have reduced the amount of code we have to write for our n-tier applications dramatically. Where they have been most uselful is in the definition of our Strongly Typed Object Collections.
Previously we would have created a class that inherited from CollectionBase and then implemented all of the usual functionality.
An example is shown below :-
[Serializable]
public class AddressCollection:CollectionBase
{
public Address this[int index]
{
get {return ((Address)List[index]);}
set {List[index] = value;}
}
public int Add(Address value)
{
return (List.Add(value));
}
public int IndexOf(Address value)
{
return (List.IndexOf(value));
}
public void Insert (int index, Address value)
{
List.Insert(index, value);
}
public void Remove(Address value)
{
List.Remove(value);
}
public bool Contains (Address value)
{
return (List.Contains(value));
}
protected override void OnInsert(int index, object value)
{
if (value.GetType() != Type.GetType("Entities.Address"))
throw new ArgumentException("value must be of type Address", "value");
}
protected override void OnRemove(int index, object value)
{
if(value.GetType() != Type.GetType("Entities.Address"))
throw new ArgumentException("value must be of type Address.", "value");
}
protected override void OnSet(int index, object oldValue, object newValue)
{
if(newValue.GetType() != Type.GetType("Entities.Address"))
throw new ArgumentException("newValue must be of type Address.", "newValue");
}
protected override void OnValidate(object value)
{
if(value.GetType() != Type.GetType("Entities.Address"))
throw new ArgumentException("value must be of type Address.");
}
But now this has all been replaced with
List<Entities.Address> AddressCollection = new List<Entities.AddressCollection>();
As you can see a massive code reduction with no loss of functionality, making it simplier to use and easier to maintain.