Programming Microsoft ASP.NET 4 - Dino Esposito [241]
Table 10-17. Properties of the DataSourceView Class
Property
Description
CanDelete
Indicates whether deletions are allowed on the underlying data source. The deletion is performed by invoking the Delete method.
CanInsert
Indicates whether insertions are allowed on the underlying data source. The insertion is performed by invoking the Insert method.
CanPage
Indicates whether the data in the view can be paged.
CanRetrieveTotalRowCount
Indicates whether information about the total row count is available.
CanSort
Indicates whether the data in the view can be sorted.
CanUpdate
Indicates whether updates are allowed on the underlying data source. The update is performed by invoking the Update method.
Name
Returns the name of the current view.
The CanXXX properties indicate not only whether the data source control is capable of performing the specified operation but also whether that operation is appropriate given the current status of the data. Table 10-18 lists all the methods supported by the class.
Table 10-18. Methods of the DataSourceView Class
Method
Description
Delete
Performs a delete operation on the data associated with the view
Insert
Performs an insert operation on the data associated with the view
Select
Returns an enumerable object filled with the data contained in the underlying data storage
Update
Performs an update operation on the data associated with the view
All data source view objects support data retrieval through the Select method. The method returns an object that implements the IEnumerable interface. The real type of the object depends on the data source control and the attributes set on it.
Hierarchical Data Source Views
Unlike tabular data source controls, which typically have only one named view, hierarchical data source controls support a view for each level of data that the data source control represents. Hierarchical and tabular data source controls share the same conceptual specification of a consistent and common programming interface for data-bound controls. The only difference is the nature of the data they work with—hierarchical vs. flat and tabular.
The view class is different and is named HierarchicalDataSourceView. The class features only one method—Select—which returns an enumerable hierarchical object. Hierarchical data source controls are, therefore, read-only.
Important
Frankly speaking, I don’t like data source components much. It’s nothing personal; it’s only business—my business layer, to be precise! Data source components have been one of the several approaches of Microsoft to make programming easier. With data source components you write less code and write most of your code in classes instead of ASPX pages. The risk I see with data source components—and the reason why I don’t much like them—is that they end up being used everywhere and the entire back-end of the application is built around the needs of some data source components. I haven’t used the SqlDataSource control for years now; I never used the LinqDataSource and have no plans to use the EntityDataSource.
My advice, reflected in the book, can be summarized as follows. If you feel you have the need to model your business domain via an object model, use LINQ-to-SQL or Entity Framework and write a serious business layer around that. If the classes in the business layer don’t match up with the expectations of rich controls like a GridView, add another layer of components that can be easily plugged in via ObjectDataSource. In any case, if you are going to use data source components for data binding, the only control worth a look is, in my humble opinion, the ObjectDataSource control.
The ObjectDataSource Control
The ObjectDataSource class enables user-defined classes to associate the output of their methods to data-bound controls. Like other data source controls, ObjectDataSource supports declarative parameters to allow developers to pass page-level variables to the object’s methods. The ObjectDataSource