冷枫@CSDN

CSharp程序员大本营:积累平凡就是积累卓越!有了翅膀,你就有了天空!钝到极点的刀才最具杀伤力——因为它是锤子!

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

 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>

         /// Initial Function

         /// </summary>

         /// <param name="strConnection"></param>

         public DataAgent(string strConnection)

         {

              this.m_strConnectionString = strConnection;

         }

         #endregion DataAgend

 

         #region OpenDataBase

         /// <summary>

         /// function to open data base

         /// </summary>

         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>

         /// the function to cloase data base

         /// </summary>

         private void CloseDataBase()

         {

              if(this.m_objConnection != null)

              {

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

                   {

                        this.m_objConnection.Close();

                   }

              }

         }

         #endregion

 

         #region GetDataView

         /// <summary>

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

         /// </summary>

         /// <param name="strSelectString">Select String</param>

         /// <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>

         /// Create a new DataAgent

         /// </summary>

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

         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();

 

       至此,大功告成!

发表于 @ 2007年10月17日 20:35:00|评论(loading...)

新一篇: .NET Framework 3.0框架慨述  | 旧一篇: 知道自己获得MVP今天很高兴

用户操作
[即时聊天] [发私信] [加为好友]
╄ 冷枫
订阅我的博客
XML聚合  FeedSky
╄ 冷枫的公告
╄ 冷枫 Asp.NET微软MVP
本Blog技术支持QQ群
[NET技术联盟]:1908832
[冷枫开发小组]:6307410
CSharp开放源码促进会
CSharp程序员大本营
文章分类
收藏
    [╄ 冷枫]简介
    站长简介(RSS)
    经典网站收集
    .NET分页存储过程
    AJAX中国
    DOTNET控件网
    DotNet男孩社区
    Java共舞
    Tutorails[.NET]
    中国盟动力
    冷枫技术论坛
    开发者在线
    技术无极限(RSS)
    深圳赶集网
    源码网
    软件项目网
    网上邻居
    『 天道酬勤 』(RSS)
    『 孟子E章 』
    『 孟子E章 』(RSS)
    『 邹建专栏 』(RSS)
    『webdiyer 』
    微软官方
    ASP.NET 入门教程
    webcast视频教程
    存档
    Csdn Blog version 3.1a
    Copyright © ╄ 冷枫