ASP.NET三层架构详解 如何实现三层架构
一、数据库
/*==============================================================*/ /* DBMS name: Microsoft SQL Server 2000 */ /*==============================================================*/ if exists (select 1 from sysobjects where id = object_id('newsContent') and type = 'U') drop table newsContent go /*==============================================================*/ /* Table: newsContent */ /*==============================================================*/ create table newsContent ( ID int identity(1,1) primary key, Title nvarchar(50) not null, Content ntext not null, AddDate datetime not null, CategoryID int not null ) go二、项目文件架构
实现步骤为:4-3-6-5-2-1
实现步骤过程
1、创建Model,实现业务实体。
2、创建IDAL,实现接口。
3、创建SQLServerDAL,实现接口里的方法。
4、增加web.config里的配置信息,为SQLServerDAL的程序集。
5、创建DALFactory,返回程序集的指定类的实例。
6、创建BLL,调用DALFactory,得到程序集指定类的实例,完成数据操作方法。
7、创建WEB,调用BLL里的数据操作方法。
注意:
1、web.config里的程序集名称必须与SQLServerDAL里的输出程序集名称一致。
2、DALFactory里只需要一个DataAccess类,可以完成创建所有的程序集实例。
3、项目创建后,注意修改各项目的默认命名空间和程序集名称。
4、注意修改解决方案里的项目依赖。
5、注意在解决方案里增加各项目引用。
三、各层间的访问过程
1、传入值,将值进行类型转换(为整型)。
2、创建BLL层的content.cs对象c,通过对象c访问BLL层的方法GetContentInfo(ID)调用BLL层。
3、BLL层方法GetContentInfo(ID)中取得数据访问层SQLServerDAL的实例,实例化IDAL层的接口对象dal,这个对象是由工厂层DALFactory创建的,然后返回IDAL层传入值所查找的内容的方法dal.GetContentInfo(id)。
4、数据工厂通过web.config配置文件中给定的webdal字串访问SQLServerDAL层,返回一个完整的调用SQLServerDAL层的路径给 BLL层。
5、到此要调用SQLServerDAL层,SQLServerDAL层完成赋值Model层的对象值为空,给定一个参数,调用SQLServerDAL层的SqlHelper的ExecuteReader方法,读出每个字段的数据赋值给以定义为空的Model层的对象。
6、SqlHelper执行sql命令,返回一个指定连接的数据库记录集,在这里需要引用参数类型,提供为打开连接命令执行做好准备PrepareCommand。
7、返回Model层把查询得到的一行记录值赋值给SQLServerDAL层的引入的Model层的对象ci,然后把这个对象返回给BLL。
8、回到Web层的BLL层的方法调用,把得到的对象值赋值给Lable标签,在前台显示给界面
四、项目中的文件清单
1、DBUtility项目
(1)connectionInfo.cs
using System; using System.Configuration; namespace Utility { /// <summary> /// ConnectionInfo 的摘要说明。 /// </summary> public class ConnectionInfo { public static string GetSqlServerConnectionString() { return ConfigurationSettings.AppSettings["SQLConnString"]; } } }2、SQLServerDAL项目
(1)SqlHelper.cs抽象类
(2)Content.cs类
using System; using System.Data; using System.Data.SqlClient; using Model; using IDAL; namespace SQLServerDAL { /// <summary> /// Content 的摘要说明。 /// </summary> public class Content:IContent { private const string PARM_ID = "@ID"; private const string SQL_SELECT_CONTENT = "Select ID, Title, Content, AddDate, CategoryID From newsContent Where ID = @ID"; public ContentInfo GetContentInfo(int id) { //创意文章内容类 ContentInfo ci = null; //创建一个参数 SqlParameter parm = new SqlParameter(PARM_ID, SqlDbType.BigInt, 8); //赋上ID值 parm.Value = id; using(SqlDataReader sdr = SqlHelper.ExecuteReader(SqlHelper.CONN_STR, CommandType.Text, SQL_SELECT_CONTENT, parm)) { if(sdr.Read()) { ci = new ContentInfo(sdr.GetInt32(0),sdr.GetString(1), sdr.GetString(2), sdr.GetDateTime(3), sdr.GetInt32(4), sdr.GetInt32(5), sdr.GetString(6)); } } return ci; } } }3、Model项目
(1)contentInfo.cs
4、IDAL项目
(1)Icontent.cs
5、DALFactory项目
(1)Content.cs
using System; using System.Reflection; using System.Configuration; using IDAL; namespace DALFactory { /// <summary> /// 工产模式实现文章接口。 /// </summary> public class Content { public static IDAL.IContent Create() { // 这里可以查看 DAL 接口类。 string path = System.Configuration.ConfigurationSettings.AppSettings["WebDAL"].ToString(); string className = path+".Content"; // 用配置文件指定的类组合 return (IDAL.IContent)Assembly.Load(path).CreateInstance(className); } } }6、BLL项目
(1)Content.cs
using System; using Model; using IDAL; namespace BLL { /// <summary> /// Content 的摘要说明。 /// </summary> public class Content { public ContentInfo GetContentInfo(int id) { // 取得从数据访问层取得一个文章内容实例 IContent dal = DALFactory.Content.Create(); // 用DAL查找文章内容 return dal.GetContentInfo(id); } } }7、Web项目
1)、Web.config:
<appSettings> <add key="SQLConnString" value="Data Source=localhost ;Persist Security info=True;Initial Catalog=newsDB; User ID=sa;Password= " /> <add key="WebDAL" value="SQLServerDAL" /> </appSettings>2)、WebUI.aspx
<%@ Page language="c#" Codebehind="WebUI.aspx.cs" AutoEventWireup="false" Inherits="Web.WebUI" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" > <HTML> <HEAD> <title>WebUI</title> <meta name="GENERATOR" Content="Microsoft Visual Studio 7.1"> <meta name="CODE_LANGUAGE" Content="C#"> <meta name="vs_defaultClientScript" content="JavaScript"> <meta name="vs_targetSchema" content="http://schemas.microsoft-com/intellisense/ie5"> </HEAD> <body MS_POSITIONING="GridLayout"> <form method="post" runat="server"> <FONT">宋体"></FONT> <table width="600" border="1"> <tr> <td style="WIDTH: 173px"> </td> <td> <asp:Label runat="server"></asp:Label></td> </tr> <tr> <td style="WIDTH: 173px; HEIGHT: 22px"> </td> <td style="HEIGHT: 22px"> <asp:Label runat="server"></asp:Label></td> </tr> <tr> <td style="WIDTH: 173px"> </td> <td> <asp:Label runat="server"></asp:Label></td> </tr> <tr> <td style="WIDTH: 173px"> </td> <td> </td> </tr> <tr> <td style="WIDTH: 173px; HEIGHT: 23px"> </td> <td style="HEIGHT: 23px"> </td> </tr> <tr> <td style="WIDTH: 173px"> </td> <td> </td> </tr> <tr> <td style="WIDTH: 173px"> </td> <td> </td> </tr> <tr> <td style="WIDTH: 173px"> </td> <td> </td> </tr> <tr> <td style="WIDTH: 173px"> </td> <td> <asp:Label runat="server">Label</asp:Label></td> </tr> </table> </form> </body> </HTML>3)、WebUI.aspx.cs后台调用显示:
using System; using System.Collections; using System-componentModel; using System.Data; using System.Drawing; using System.Web; using System.Web.SessionState; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.HtmlControls; using BLL; using Model; namespace myWeb { /// <summary> /// WebForm1 的摘要说明。 /// </summary> public class WebUI : System.Web.UI.Page { protected System.Web.UI.WebControls-l abel lblTitle; protected System.Web.UI.WebControls-l abel lblDataTime; protected System.Web.UI.WebControls-l abel lblContent; protected System.Web.UI.WebControls-l abel lblMsg; private ContentInfo ci ; private void Page_Load(object sender, System.EventArgs e) { if(!Page.IsPostBack) { GetContent("1"); } } private void GetContent(string id) { int ID = WebComponents.CleanString.GetInt(id); Content c = new Content(); ci = c.GetContentInfo(ID); if(ci!=null) { this.lblTitle.Text = ci.Title; this.lblDataTime.Text = ci.AddDate.ToString("yyyy-MM-dd"); this.lblContent.Text = ci.Content; } else { this.lblMsg.Text = "没有找到这篇文章"; } } #region Web 窗体设计器生成的代码 override protected void OnInit(EventArgs e) { // // CODEGEN: 该调用是 ASP Web 窗体设计器所必需的。 // InitializeComponent(); base.OnInit(e); } /// <summary> /// 设计器支持所需的方法 - 不要使用代码编辑器修改 /// 此方法的内容。 /// </summary> private void InitializeComponent() { this.Load += new System.EventHandler(this.Page_Load); } #endregion } }4)、WebComponents项目
(1)CleanString.cs
以上就是ASP三层架构的全部内容,希望对大家的学习有所帮助。
《ASP.NET三层架构详解 如何实现三层架构》阅读地址:http://www.haoshilao.net/12953/
已有0条评论,点击查看发表评论