Introduction
SmartLists
library is a collection of classes that extends the standard C# List<T>
object. Better: they implement the IList<T>
interface, wrapping the List<T>
object and adding more functionalities to it.
These lists implements solutions to common issues that usually happen when dealing with List
s. As an example, a list with a "Parent
" object that is consistently updated between the elements of the list and the list itself.
These lists are not designed for performance but with easy of use and consistency in mind. If you look for performance, you better look into other kind of lists that are designed for that.
EventList
This is a list that generates events when elements are added, inserted, removed, swapped and when it is cleared. Events are generated before and after these actions, so that we can cancel the action or take further actions after the action.
Some examples of use of the events could be:
- To check an object before it's added to the list so that objects with inconsistent properties are not added to the list
- To avoid that some kind of elements are removed from the list
- To update GUI elements after some actions are taken on a list
NamedList
This is an EventList
that requires its elements to have a Name
(string
) property (INamed
interface). In this kind of list, you can't add elements with the same name. The name comparison can be set to be case sensitive or not.
public interface INamed
{
event CancelRenameEventHandler BeforeNameChanged;
event EventHandler NameChanged;
string Name { get; }
}
ParentedList
This is an EventList
where its elements and the list itself have a Parent
object (IParented<T>
interface). And we want this Parent
property to be updated automatically when the object is added or removed from the list or the list's parent
changes. This is really quite a common situation that happens really often and is usually coded in the most wrong and inconsistent ways.
public interface IParented<T>
{
event EventHandler ParentChanged;
T Parent
{
get;
set;
}
}
ParentedNamedList
This is an EventList
that puts together the features of the NamedList
and the ParentedList
. So in this kind of list, we will have items with a Name
and a Parent
property.
An example of use of this kind of list is a list of a database table fields. The fields have of course a Name
and a Parent
(the table). When a field is removed or created and added to the list, its parent is updated automatically by the list. Of course, you can't add two fields with the same name: and of course you can't rename a field with the name of another field (this generates a DuplicatedNameException
). This is all handled automatically by the ParentedNamedList
. The only thing the developer needs to do is create a Field
object that implements the INamed
and the IParent<T>
interfaces.