w0051977
w0051977

Reputation: 15817

.NET Design Patterns (tiers)

I support and develop a large ASP.NET application (I am a sole developer). I am trying to adopt a structured approach to coding by using design patterns, but I have not yet managed to grasp the subject fully. I was thinking about using a MVP pattern for the user interface and a data tier to separate the business logic and the data logic (two patterns in total). For example, have a look at the code below:

Imports System.Data.SqlClient
Imports System.Web.Configuration
Partial Class _Default
    Inherits System.Web.UI.Page

    Private _ConString As String

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Try
            If Session("OrderID") > " " Then 'Line 10
            Dim objDR As SqlDataReader
                _ConString = WebConfigurationManager.ConnectionStrings("TestConnection").ConnectionString
                Dim objCon As New SqlConnection(_ConString)
                Dim objCommand As New SqlCommand
                objCommand.CommandText = "SELECT * FROM Person WHERE ID = " & session("id") 'I know this could cause SQL injection attacks.  I wrote it quickly to get my point accross
                objCon.Open()
                objCommand.Connection = objCon
                objDR = objCommand.ExecuteReader
                Do While objDR.Read
                    MsgBox(objDR("name"))
                Loop
                objDR.Close()
    End If
        Catch ex As Exception
            Throw
        End Try
    End Sub
End Class

This breaks a lot of SOLID rules. There is data logic and business logic in the presentation layer (line 10 is the business logic).

I was thinking about creating two new classes i.e. PersonDAL (for the data access layer) and PersonBLL for the business logic layer. The business logic layer and the data access layer will have the same function names i.e. getPerson() i.e. the presentation layer will call getPerson in the BLL layer which will call getPerson in the DAL. My question is: is this a good apprach or is there a better way to break up this function into tiers?

I have looked at the following link, which talks about this approach when using datasets, but I am not using datasets i.e. I am using SQLCommands and SQLDataReaders: http://msdn.microsoft.com/en-us/library/aa581779.aspx.

Upvotes: 0

Views: 258

Answers (1)

competent_tech
competent_tech

Reputation: 44971

We have applications that have been in production for 8+ years using this exact tier structure, designed initially based on Microsoft reference applications.

This tiered system has allowed us to easily add web client, service interfaces (for processing incoming email), win forms clients, windows tablet clients, and most recently support an almost direct port to mono for creation of iPad clients.

All of the clients share the BLL and communicate with the DAL through .Net remoting or WCF. This approach also allows us to distribute the web client and DAL across multiple physical servers in order to handle very large numbers of users.

Upvotes: 1

Related Questions