Using ASP.NET 2.0âs new GridView control with the new ObjectDataSource allows you to properly layer your applications. You can create strongly-typed collections of your business objects, and quickly bind them to the new Grid. The hype sounds good. Real good, in fact:
The GridView control is the successor to the DataGrid and extends it in a number of ways. First, it fully supports data source components and can automatically handle data operations, such as paging, sorting, and editing, provided its bound data source object supports these capabilities.
The key phrase here is âprovided its bound data source object supports these capabilitiesâ When you bind your GridView to an ObjectDataSource wired to a DataSet, DataView or DataTable, you do indeed get automatic sorting, and paging, and you can also filter your data through the ObjectDataSourceâs FilterExpression property. All this adds up to a really useful control, one that you can base your entire web applications around.
Business Entities and SOA.
Okay, great. Well, say youâve been using Business Entities and Strongly-Typed collections in lieu of DataSets to build your application framework and business layers. If youâve been developing this way, youâre probably a convert. Having a nice, clean framework of entities can be a great foundation for application development. You also may be using a service oriented pattern to return these entity objects, instead of filling your objects with an instance method of the business entity itself.
Both of these patterns are good to follow, but neither work out very well using the ObjectDataSource. If youâve done this and you try wiring up these collections to an ObjectDataSource and GridView, youâll find that a Strongly-Typed Collection doesnât work with the filtering (setting the ObjectDataSourceâs FilterExpression property) and automatic sorting capabilities of the GridView. The GridViewâs current implementation requires that you use a DataTable, DataSet or DataView, unless you want to handle these events yourself and do your sorting on your own. It will automatically do paging, but other than that youâre on your own.
It also doesnât like the service oriented/entity pattern much. It actually works best when you have smart business objects that are DataTables, DataSets or DataViews and also implement methods which populate the entities. In Fact, the ObjectDataSourceâs control designer wonât complete until you specify which method on your business object is the âFillâ method. This is too bad, because often what we need is to be able to use our entity objects, our service tier methods, and a nice GridView that does the sorting/paging/filtering stuff easily.
Since this doesnât exist currently, the next best thing is to create a DataTable, fill it with your entity collectionâs data, and hand this off to the GridView for presentation. Itâs a little strange having to go back to a DataTable after having built a nice clean framework of entities. Whenever I find myself having to revert back to using DataSets or DataTables, just for the sake of using one control or another, I feel Michael Corleone in Godfather III - âJust when I thought that I was out they pull me back in.â
This is where I really can jibe with Jeffreyâs RAD Kills message. We have this great RAD grid, but to use it you have to revert to a sometimes poor design choice (DataSets).
The EntityDataTable â Best of Both Worlds?
To solve this problem of allowing my business entity collections to be quickly bind-able to a GridView I whipped together a quick abstract adapter class which I can sub-class that is actually a DataTable. This way, when I bind it to a GridView, it will support all this auto-sorting/filtering stuff. This class has two methods, GetDataTable which uses reflection and returns a DataTable containing a column for each public property in my entity class. It also contains an abstract method Fill which can be overridden to connect to your service tier, get your collection, and call the GetDataTable method with your collection and your collectionâs contained type.
All that's left for me to do is pop my parameter in the session, and call GridView1.DataBind().. Voila! My grid is bound to my entity collection, sorting and paging work like a charm, and I can filter my data buy calling by setting the Filter parameter of the ObjectDataSource.