.NET设计模式(层次)层次、模式、NET

2023-09-04 06:05:40 作者:圈子不同别硬融

我支持和发展的大ASP.NET应用程序(我是唯一的开发者)。我想通过一个结构化的方法来使用设计模式的编码,但我还没有能够完全掌握的主题。我想使用一个MVP模式的用户界面和数据层分离业务逻辑和数据逻辑(共2个模式)。例如,看一下下面的code:

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

这打破了很多扎实的规则。有在presentation层数据逻辑和业务逻辑(10号线是业务逻辑)。

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

我在想创建两个新类即PersonDAL(用于数据访问层)和PersonBLL业务逻辑层。业务逻辑层和数据访问层都会有相同的函数名,即getPerson()即presentation层将调用getPerson在BLL层将调用getPerson在DAL。我的问题是:这是一个很好的apprach或是否有更好的方法,这个功能分解成层

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?

我已经看过下面的链接,使用的数据集时,其中谈到了这种方法,但我不使用的数据集,即我使用SQLCommands和SQLDataReaders: http://msdn.microsoft.com/en-us/library/aa581779.aspx 。

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.

推荐答案

我们有一个已经在生产中使用这个确切的三层结构具有8年以上的应用,最初设计基于微软的参考应用程序。

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

此分层系统使我们能够轻松地添加Web客户端,服务接口(用于处理传入的电子邮件),赢得形式的客户端,Windows平板客户端,以及最近的支持几乎是直接端口为单声道创造的iPad客户端。

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.

所有的客户机共享BLL,并通过.Net远程或WCF的DAL沟通。这种方法还使我们能够分配到多个物理服务器的Web客户端和DAL为了处理大量的用户。

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.