Monthly Archives: May 2012

Client Consuming From The Data Access Layer

When I first launched the World Of Art web app, I wrote a post entitled  Improving the Design, where I  expressed concerns over the tight coupling between the user interface and the SQL Server database. In today’s post, I will demonstrate how the user interface can consume data without any regard as to what or where the data source is. This decoupling of the user interface from the data source, is made possible, by separating the entity data model classes, data access classes, and the domain entities classes, into distinct architectural layers of the application.

In the first iteration of the Artist Web Form, I used three ASP.Net user interface server controls;

  • DetailsView
  • GridView
  • SqlDataSource

In the latest iteration of the user interface, I added a new web form called ArtistEntityWebForm, which eliminates the SqlDataSource control for querying the World of Art database. With the first iteration of the artist web form, it wasn’t necessary to write any C# code to retrieve data, or bind data to the user interface controls. The ASP.Net engine on the web server, performed all of these operations automatically, based on the XHTML markup in the web form. By eliminating the SqlDataSource control,  it is now necessary to write some C# code, to retrieve data through the data access layer, and bind it to the views controls. Here is the XHTML markup of the artist entity web form;


Here is the C# code in the code behind file;

At the top of the C# file, you can see that it is referencing the ArtWorldOjects class library. The classes in this library represent the objects found in the world of art domain. The C# file also references the WorldOfArtWebApp.DataAccess name space in the data access layer. There are two methods called in the page load event;

  • LoadArtistView()
  • LoadWorkOfArtView()

Both methods create a new instance of the appropriate class in the Data Access Layer. The constructor of each class calls methods to retrieve data through the data entity model and initialize the appropriate collection of world of art objects. I added a get list method to each class in the data access layer to return a simple array of objects. These get list methods are now the data source for the detail views control and the grid view control. All this client object needs to know, is that it can consume an enumerable list of world of art objects, by instantiating the appropriate class in the data access layer and calling the get list method of that class.

Click Here to render the new artist entity page and see the results of all the code on today’s post.