当前位置:编程学习 > C#/ASP.NET >>

分层体系结构(MVC)一

答案: 

要开始开发项目了,在我参加之前,他们都比较关注技术细节,比如曲线,报表之类的,对于整个软件的体系结构基本没有考虑过,这令我比较奇怪。由于我有些java开发大型项目的经验,知道一个好的体系结构对项目开发的重要性。所以开始着手架构问题。

采用分层的体系结构,将用户界面、业务逻辑处理、数据访问、以及一些操作数据库必须的操作分离开来。

Business

Data

EntityClass

WinForm

层次如上所示。具体每一层里面的内容。

一、Data层

主要完成一些对数据库的基本的通用的 操作,包括两个文件,ConnectionFactory.vb和DataFactory.vb

ConnectionFactory.vb完成连接数据库的操作,代码如下:

Imports System
Imports System.Data
Imports System.Data.SqlClient
Namespace Data
    Public Class ConnectionFactory
        Private Const ConnectionKey As String = "ConnectionString"
        Public Sub New()
            ' TODO: 在此处添加构造函数逻辑
        End Sub
        Public Shared Function myConnection() As System.Data.SqlClient.SqlConnection
            Dim cmConn As New SqlConnection
            Dim strConnection As String = System.Configuration.ConfigurationSettings.GetConfig(ConnectionKey)
            strConnection = "server=200.200.200.101;database=winlog;user id=sa;password=sa"
            If strConnection <> " " Then

                cmConn.ConnectionString = strConnection
                Return cmConn

            Else
                '应该返回null
                cmConn.Close()
                Return cmConn
            End If
        End Function
        Public Shared Function myConnection(ByVal strConnection As String) As System.Data.SqlClient.SqlConnection
            Dim ConnectionKey1 As String
            If strConnection <> " " Then
                ConnectionKey1 = strConnection
            End If
            Dim cmConn As SqlConnection = New SqlConnection
            strConnection = System.Configuration.ConfigurationSettings.GetConfig(ConnectionKey1)
            If (strConnection <> "") Then

                cmConn.ConnectionString = strConnection
                Return cmConn

            Else
                cmConn.Close()
                Return cmConn
            End If
        End Function

    End Class
End Namespace

DataFactory.vb一些通用的数据库操作的封装,便于上层掉用,具体的看代码中的注释,代码如下:


Imports System
Imports System.Data
Imports System.Data.SqlClient
Namespace Data

    Public Class DataFactory
        Protected myCommand As SqlCommand
        Protected myTrans As SqlTransaction
        Protected strErrMessage As String
        Protected myConnection As SqlConnection
        Protected myDataAdapter As SqlDataAdapter
        Protected thisTransactionOn As Boolean

        Public Sub New()

            ' TODO: 在此处添加构造函数逻辑

            Me.myConnection = ConnectionFactory.myConnection()
            '   this.myCommand = this.myConnection.CreateCommand();  
            '   this.myDataAdapter = new SqlDataAdapter(this.myCommand);
            'MsgBox("hehe" + Me.myConnection.Database.ToString)
            Me.strErrMessage = ""
            Me.thisTransactionOn = False
            '新增了一个初始化()

            Me.myCommand = New SqlClient.SqlCommand

        End Sub
        '设置SQL语句
        Protected Function SetCommandText(ByVal commandText As String, ByVal commandType As CommandType)
            'MsgBox(Me.myCommand.CommandText)
            'MsgBox(Me.thisTransactionOn)
            If (Me.thisTransactionOn) Then
                'MsgBox(5)
                If (Me.myCommand.ToString.Equals("")) Then

                    Me.myCommand = Me.myConnection.CreateCommand()
                    Me.myCommand.Transaction = Me.myTrans

                ElseIf (Me.myCommand.Connection.State = ConnectionState.Closed) Then

                    Me.myCommand.Connection = Me.myConnection
                    Me.myCommand.Transaction = Me.myTrans

                End If
            ElseIf ((Not Me.thisTransactionOn) And Me.myCommand.CommandText.Equals("")) Then
     &nbs

上一个:分层体系结构(MVC)二
下一个:自动改变CheckBoxList选择项目的背景颜色

CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,