Kriya and Meditation for Software / IT Professionals. Conducted by Bipin Joshi in Thane. Read more...
Learn ASP.NET MVC, ASP.NET Core and ASP.NET Design Patterns from the comfort of your home. Online courses conducted by Bipin Joshi on weekends. Click here for more details.

N-Tier Applications and .NET: Achieving Isolation between UI and BOL

Introduction

In the previous three article (N-Tier Applications and .NET, N-Tier Applications and .NET: A Simple Example and N-Tier Applications and .NET: Achieving Isolation between DAL and BOL ) we have seen how a typical 3-tier application looks like and how to achieve isolation between Data Access Layer and Business Object Layer. I mentioned in the last article that we still have tight coupling between UI and database. This is due to the fact that our Select methods are returning DataSets which contain actual database column names. This means any change to database column names is going to affect UI layer. This article takes the concept one step ahead and shows how this coupling can be reduced.

Reducing coupling between UI and BOL

When you return a DataSet from your DAL or BOL directly it carries the schema information about the tables such as column names. You use this information to bind your controls such as DropDownList and DataGrid with the DataSet. However, in the process you end up having a tight coupling between UI and database schema. What if some column name changes? Naturally you will need to change (and possibly recompile) your UI also. Some people use SELECT queries in such a way that real column names are not exposed to UI. However, this approach is not only cumbersome but more error prone. 

Better way is to use some kind of collection classes such as ArrayList or arrays to ship your data from one layer to another. I have modified the application to use ArrayList now. This means SelectAll() and SelectSingle() methods from BOL will no nonger return DataSets. Instead they will return ArrayList of CustomerState objects and a single CustomerState object respectively. Following is the code for these two methods.

   Public Shared Function SelectAll() As ArrayList
        Dim ds As DataSet = CustomerDALActions.SelectAll()
        Dim arr As New ArrayList
        For Each row As DataRow In ds.Tables(0).Rows
            Dim c As New CustomerState
            c.CustomerID = row("customerid")
            c.CompanyName = row("companyname")
            c.ContactName = row("contactname")
            c.Country = row("country")
            arr.Add(c)
        Next
        Return arr
    End Function

    Public Shared Function SelectSingle(ByVal custid As String) 
        As CustomerState
        Dim ds As DataSet = CustomerDALActions.SelectSingle(custid)
        Dim row As DataRow = ds.Tables(0).Rows(0)
        Dim c As New CustomerState
        c.CustomerID = row("customerid")
        c.CompanyName = row("companyname")
        c.ContactName = row("contactname")
        c.Country = row("country")
        Return c
    End Function

The UI elements such as DropDownList or DataGrid will be bound with this ArrayList.

        DropDownList1.DataSource = CustomerBOLActions.SelectAll
        DropDownList1.DataTextField = "CustomerID"
        DropDownList1.DataValueField = "CustomerID"
        DropDownList1.DataBind()

Note that in the above code DataSource is ArrayList. Also, DataTextField and DataValueField properties are set to the property names of CustoemrState class not to the database column names. This way real column names get encapsulated in properties.

You can also use collection classes instead of using ArrayList. Refer my article on how to create your custom collection classes.

Downloading and running the application

You can download the complete source code above (see top of this article).

Before you run the application at your end make sure that the web application project is marked as IIS application. Also, ensure that the database connection string is changed as per your requirements.


Bipin Joshi is a software consultant, an author and a yoga mentor having 22+ years of experience in software development. He also conducts online courses in ASP.NET MVC / Core and Design Patterns. He is a published author and has authored or co-authored books for Apress and Wrox press. Having embraced the Yoga way of life he also teaches Meditation and Mindfulness to interested individuals. To know more about him click here.

Get connected : Twitter  Facebook  Google+  LinkedIn

Posted On : 24 April 2005


Tags : ASP.NET Architecture Components