Online Book Reader

Home Category

Programming Microsoft ASP.NET 4 - Dino Esposito [217]

By Root 5674 0

{

IEnumerator GetEnumerator();

}

Many bindable objects, though, actually implement more advanced versions of IEnumerable, such as ICollection and IList. In particular, you can bind a Web control to the following classes:

Collections (including dictionaries, hashtables, and arrays)

ADO.NET container classes such as DataSet, DataTable, and DataView

ADO.NET data readers

Any IQueryable object that results from the execution of a LINQ query

To be honest, I should note that the DataSet and DataTable classes don’t actually implement IEnumerable or any other interfaces that inherit from it. However, both classes do store collections of data internally. These collections are accessed using the methods of an intermediate interface—IListSource—which performs the trick of making DataSet and DataTable classes look like they implement a collection.

Collection Classes


At the highest level of abstraction, a collection serves as a container for instances of other classes. A collection is like an array, but with a richer programming interface. All collection classes implement the ICollection interface, which in turn implements the IEnumerable interface. As a result, all collection classes provide a basic set of functionalities.

All collection classes have a Count property to return the number of cached items; they have a CopyTo method to copy their items, in their entirety or in part, to an external array; and they have a GetEnumerator method that instantiates an enumerator object to loop through the child items. GetEnumerator is the method behind the curtain whenever you call the foreach statement in C# and the For…Each statement in Microsoft Visual Basic.

IList and IDictionary are two interfaces that extend ICollection, giving a more precise characterization to the resultant collection class. ICollection provides only basic and minimal functionality for a collection. For example, ICollection does not have any methods to add or remove items. Add and remove functions are exactly the capabilities that the IList interface provides. In the IList interface, the Add and Insert methods place new items at the bottom of the collection or at the specified index. The Remove and RemoveAt methods remove items, while Clear empties the collection. Finally, Contains verifies whether an item with a given value belongs to the collection, and IndexOf returns the index of the specified item. Commonly used container classes that implement both ICollection and IList are Array, ArrayList, and StringCollection.

The IDictionary interface defines the API that represents a collection of key/value pairs. The interface exposes methods similar to IList, but with different signatures. Dictionary classes also feature two extra properties, Keys and Values. They return collections of keys and values, respectively, found in the dictionary. Typical dictionary classes are ListDictionary, Hashtable, and SortedList.

Most of the time, however, you’ll be using generic lists of custom objects, as shown here:

boundServerControl1.DataSource = new List();

The net effect is that the data-bound control is linked to an object that contains a list of Customer objects.

It is important that the element class—Customer, in the preceding code—implements data members as properties, instead of fields.

public class Customer

{

public Int32 CustomerId {get; set};

public String Name {get; set;}

...

}

A property is a data member exposed through the filter represented by a get and/or a set method. A field, instead, is a member that is exposed directly as a read/write location. Data members coded as fields won’t be discovered at run time and therefore are useless for data binding. This is by design. However, any .NET class can modify the conventional algorithm through which its properties are discovered at run time by implementing the ICustomTypeDescriptor interface.

Implementing the ICustomTypeDescriptor interface gives the object itself a chance to enumerate exactly the properties it wants to expose regardless of the actual schema of the

Return Main Page Previous Page Next Page

®Online Book Reader