`

怎样用VS2005进行三层结构应用程序的开发

 
阅读更多
<iframe align="center" marginwidth="0" marginheight="0" src="http://www.zealware.com/csdnblog336280.html" frameborder="0" width="336" scrolling="no" height="280"></iframe>
VS2005.NET进行三层结构应用程序的开发

1.三层之间的关系:

三层是指:界面显示层(UI),业务逻辑层(Business),数据操作层(Data Access)

文字描述:

ClientsUI进行操作,UI调用Business进行相应的运算和处理,Business通过Data AccessData Base进行操作。

优点:

1、增加了代码的重用。Data Access可在多个项目中公用;Business可在同一项目的不同地方使用(如某个软件B/SC/S部分可以共用一系列的Business组件)。

2、使得软件的分层更加明晰,便于开发和维护。美工人员可以很方便地设计UI设计,并在其中调用Business给出的接口,而程序开发人员则可以专注的进行代码的编写和功能的实现。


2.Data Access的具体实现:

DataAgent类型中变量和方法的说明

private string m_strConnectionString; //连接字符串

private OleDbConnection m_objConnection; //数据库连接

public DataAgent(string strConnection) //构造方法,传入的参数为连接字符串

private void OpenDataBase() //打开数据库连接

private void #region CloseDataBase() //关闭数据库连接

public DataView GetDataView(string strSqlStat) //根据传入的连接字符串返回DataView

具体实现代码如下:

public class DataAgent

{

#region Variables

private string m_strConnectionString;

private OleDbConnection m_objConnection;

#endregion Variables

#region Functions

#region DataAgend

/// <summary><p></p></summary>

/// Initial Function

///

///

public DataAgent(string strConnection)

{

this.m_strConnectionString = strConnection;

}

#endregion DataAgend

#region OpenDataBase

/// <summary><p></p></summary>

/// function to open data base

///

private void OpenDataBase()

{

try

{

this.m_objConnection = new OleDbConnection();

this.m_objConnection.ConnectionString = this.m_strConnectionString;

if(this.m_objConnection.State != ConnectionState.Open)

{

this.m_objConnection.Open();

}

}

catch(Exception e)

{

throw e;

}

}

#endregion OpenDataBase

#region CloseDataBase

/// <summary><p></p></summary>

/// the function to cloase data base

///

private void CloseDataBase()

{

if(this.m_objConnection != null)

{

if(this.m_objConnection.State == ConnectionState.Open)

{

this.m_objConnection.Close();

}

}

}

#endregion

#region GetDataView

/// <summary><p></p></summary>

/// Execute the sql and return the default table view

///

/// Select String

/// <returns>DataView of the DataTable</returns>

public DataView GetDataView(string strSqlStat)

{

try

{

this.OpenDataBase();

OleDbDataAdapter objDataAdapter = new OleDbDataAdapter(strSqlStat.Trim(),this.m_objConnection);

DataSet objDataSet = new DataSet();

objDataAdapter.Fill(objDataSet);

return objDataSet.Tables[0].DefaultView;

}

catch(Exception e)

{

throw e;

}

finally

{

this.CloseDataBase();

}

}

#endregion GetDataTable

#endregion Functions

}


3.Business的具体实现:

建立名为Base的类,此类作为其他事务类的基类,其中定义了一个DataAgent的实例。其他所有的Business类都从该改类派生。

在该类中添加对DataAgent的引用,使所有的事务类都能使用DataAgent中的方法。

Base.cs源代码:

public abstract class Base

{

#region DataAgent

private DataAgent m_objDBAgent;

protected DataAgent OleDBAgent

{

get

{

if(this.m_objDBAgent == null)

{

this.m_objDBAgent = this.CreateAgent();

}

return this.m_objDBAgent;

}

set

{

this.m_objDBAgent = value;

}

}

#endregion DataAgent

public Base()

{

}

#region CreateAgent

/// <summary><p></p></summary>

/// Create a new DataAgent

///

/// <returns></returns>the DataAgent

private DataAgent CreateAgent()

{

string strConnection = ConfigurationSettings.AppSettings["ConnectionString"];

return new DataAgent(strConnection);

}

#endregion CreateAgent

}

准备好了数据操作层和事务层的基类,底下就可以正式地开始业务逻辑类的开发了,如有一个显示新闻的类News,其中包含了一个GetNewsLsit()的方法,该方法用来获取所有的新闻标题列表,代码如下:

public class News: Base

{

public News Contact()

{

}

public DataView GetNewsList()

{

string strSql;

strSql = "";

strSql += " SELECT Top 10 NewsId,NewsTitle ";

strSql += " FROM Tb_News";

strSql += " WHERE NewsEnable = 1";

strSql += " ORDER BY NewsId ";

return base.OleDBAgent.GetDataView(strSql);

}

}

由于数据库结构比较简单,在此就不再给出详细的表结构。

4.UI层对Business中接口的调用

首先,在页面中添加对News类的引用。

然后,在页面中添加一个(DataGrid)dgNews用来显示新闻列表。

Page BehindPage_Load方法中添加如下代码:

News objNews = new News();

this.dgNews.DataSource = objNews.GetNewsList();

this.dgNews.DataBind();

至此,大功告成!




分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics