MVC三层机构详解.docx_第1页
MVC三层机构详解.docx_第2页
MVC三层机构详解.docx_第3页
MVC三层机构详解.docx_第4页
MVC三层机构详解.docx_第5页
已阅读5页,还剩15页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

三层架构详解一、数据库/*=*/*DBMSname:MicrosoftSQLServer2000*/*=*/ifexists(select1fromsysobjectswhereid=object_id(newsContent)andtype=U)droptablenewsContentgo/*=*/*Table:newsContent*/*=*/createtablenewsContent(IDintidentity(1,1)primarykey,Titlenvarchar(50)notnull,Contentntextnotnull,AddDatedatetimenotnull,CategoryIDintnotnull)go二、项目文件架构实现步骤为:4-3-6-5-2-1ID项目描述用途项目引用关系实例所需文件相关方法1Web表现层Web页和控件引用BLLWebUI.aspxWebUI.aspx.csGetContent()2BLL业务逻辑层业务逻辑组件引用 IDAL,Model,使用DALFactory创建实例Content.csContentInfo GetContentInfo(int id)3IDAL数据访问层接口定义每个DAL实现都要实现的一组接口引用 ModelIContent.csContentInfo GetContentInfo(int id)4Model业务实体传递各种数据的容器无引用ContentInfo.cs5DALFactory数据层的抽象工厂创建反射,用来确定加载哪一个数据库访问程序集的类引用IDAL,通过读取web.config里设置的程序集,加载类的实例,返回给BLL使用。Content.csIDAL.Icontent create()6SQLServerDALSQLServer数据访问层Microsoft SQL Server特定的Pet Shop DAL实现,使用了IDAL接口引用 Model和IDAL,被DALFactory加载的程序集,实现接口里的方法。SqlHelper.csContent.csSqlDataReader ExecuteReader()PrepareCommand()ContentInfo GetContentInfo(int id)OracleDALOracle数据访问层7DBUtility数据库访问组件基础类GetSqlServerConnectionString得到数据库连接字符串,也可省去该项目,在SQLServerDAL.SqlHelper中用static readonly string SqlConnectionString代替。无引用实现步骤过程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.csusingSystem;usingSystem.Configuration;namespaceUtility/ConnectionInfo的摘要说明。/publicclassConnectionInfopublicstaticstringGetSqlServerConnectionString()returnConfigurationSettings.AppSettingsSQLConnString;2、SQLServerDAL项目(1)SqlHelper.cs抽象类usingSystem;usingSystem.Data;usingSystem.Data.SqlClient;usingDBUtility;namespaceSQLServerDAL/SqlHelper的摘要说明。/publicabstractclassSqlHelperpublicstaticreadonlystringCONN_STR=ConnectionInfo.GetSqlServerConnectionString();/用提供的函数,执行SQL命令,返回一个从指定连接的数据库记录集/例如:/SqlDataReaderr=ExecuteReader(connString,CommandType.StoredProcedure,PublishOrders,newSqlParameter(prodid,24);/SqlConnection有效的SQL连接字符串/CommandType:CommandType.Text、CommandType.StoredProcedure/SQL语句或存储过程/SqlParameter参数数组/SqlDataReader:执行结果的记录集publicstaticSqlDataReaderExecuteReader(stringconnString,CommandTypecmdType,stringcmdText,paramsSqlParametercmdParms)SqlCommandcmd=newSqlCommand();SqlConnectionconn=newSqlConnection(connString);/我们在这里用try/catch是因为如果这个方法抛出异常,我们目的是关闭数据库连接,再抛出异常,/因为这时不会有DataReader存在,此后commandBehaviour.CloseConnection将不会工作。tryPrepareCommand(cmd,conn,null,cmdType,cmdText,cmdParms);SqlDataReaderrdr=cmd.ExecuteReader(CommandBehavior.CloseConnection);cmd.Parameters.Clear();returnrdr;catchconn.Close();throw;/为执行命令做好准备:打开数据库连接,命令语句,设置命令类型(SQL语句或存储过程),函数语取。/SqlCommand组件/SqlConnection组件/SqlTransaction组件,可以为null/语句类型:CommandType.Text、CommandType.StoredProcedure/SQL语句,可以为存储过程/SQL参数数组privatestaticvoidPrepareCommand(SqlCommandcmd,SqlConnectionconn,SqlTransactiontrans,CommandTypecmdType,stringcmdText,SqlParametercmdParms)if(conn.State!=ConnectionState.Open)conn.Open();cmd.Connection=conn;cmd.CommandText=cmdText;if(trans!=null)cmd.Transaction=trans;cmd.CommandType=cmdType;if(cmdParms!=null)foreach(SqlParameterparmincmdParms)cmd.Parameters.Add(parm);(2)Content.cs类usingSystem;usingSystem.Data;usingSystem.Data.SqlClient;usingModel;usingIDAL;namespaceSQLServerDAL/Content的摘要说明。/publicclassContent:IContentprivateconststringPARM_ID=ID;privateconststringSQL_SELECT_CONTENT=SelectID,Title,Content,AddDate,CategoryIDFromnewsContentWhereID=ID;publicContentInfoGetContentInfo(intid)/创意文章内容类ContentInfoci=null;/创建一个参数SqlParameterparm=newSqlParameter(PARM_ID,SqlDbType.BigInt,8);/赋上ID值parm.Value=id;using(SqlDataReadersdr=SqlHelper.ExecuteReader(SqlHelper.CONN_STR,CommandType.Text,SQL_SELECT_CONTENT,parm)if(sdr.Read()ci=newContentInfo(sdr.GetInt32(0),sdr.GetString(1),sdr.GetString(2),sdr.GetDateTime(3),sdr.GetInt32(4),sdr.GetInt32(5),sdr.GetString(6);returnci;3、Model项目(1)contentInfo.csusingSystem;namespaceModel/Class1的摘要说明。/publicclassContentInfoprivateint_ID;privatestring_Content;privatestring_Title;privatestring_From;privateDateTime_AddDate;privateint_clsID;privateint_tmpID;/文章内容构造函数/文章流水号ID/文章内容/文章标题/文章来源/文章的分类属性ID/文章的模板属性IDpublicContentInfo(intid,stringtitle,stringcontent,stringfrom,DateTimeaddDate,intclsid,inttmpid)this._ID=id;this._Content=content;this._Title=title;this._From=from;this._AddDate=addDate;this._clsID=clsid;this._tmpID=tmpid;/属性publicintIDgetreturn_ID;publicstringContentgetreturn_Content;publicstringTitlegetreturn_Title;publicstringFromgetreturn_From;publicDateTimeAddDategetreturn_AddDate;publicintClsIDgetreturn_clsID;publicintTmpIDgetreturn_tmpID;4、IDAL项目(1)Icontent.csusingSystem;usingModel;namespaceIDAL/文章内容操作接口/publicinterfaceIContent/取得文章的内容。/文章的ID/ContentInfoGetContentInfo(intid);5、DALFactory项目(1)Content.csusingSystem;usingSystem.Reflection;usingSystem.Configuration;usingIDAL;namespaceDALFactory/工产模式实现文章接口。/publicclassContentpublicstaticIDAL.IContentCreate()/这里可以查看DAL接口类。stringpath=System.Configuration.ConfigurationSettings.AppSettingsWebDAL.ToString();stringclassName=path+.Content;/用配置文件指定的类组合return(IDAL.IContent)Assembly.Load(path).CreateInstance(className);6、BLL项目(1)Content.csusingSystem;usingModel;usingIDAL;namespaceBLL/Content的摘要说明。/publicclassContentpublicContentInfoGetContentInfo(intid)/取得从数据访问层取得一个文章内容实例IContentdal=DALFactory.Content.Create();/用DAL查找文章内容returndal.GetContentInfo(id);7、Web项目1、Web.config:2、WebUI.aspxWebUI宋体                  Label3、WebUI.aspx.cs后台调用显示:usingSystem;usingSystem.Collections;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Web;usingSystem.Web.SessionState;usingSystem.Web.UI;usingSystem.Web.UI.WebControls;usingSystem.Web.UI.HtmlControls;usingBLL;usingModel;namespacemyWeb/WebForm1的摘要说明。/publicclassWebUI:System.Web.UI.PageprotectedSystem.Web.UI.WebControls.LabellblTitle;protectedSystem.Web.UI.WebControls.LabellblDataTime;protectedSystem.Web.UI.WebControls.LabellblContent;protectedSystem.Web.UI.WebControls.LabellblMsg;privateContentInfoci;privatevoidPage_Load(objectsender,System.EventArgse)if(!Page.IsPostBack)GetContent(1);privatevoidGetContent(stringid)intID=WebComponents.CleanString.GetInt(id);Contentc=newContent();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;elsethis.lblMsg.Text=没有找到这篇文章;#regionWeb窗体设计器生成的代码overrideprotectedvoidOnInit(EventArgse)/CODEGEN:该调用是ASP.NETWeb窗体设计器所必需的。/InitializeComponent();base.OnInit(e);/设计器支持所需的方法-不要使用代码编辑器修改/此方法的内容。/privatevoidInitializeComponent()this.Load+=newSystem.EventHandler(thi

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论