怎么实现ASP。NET三层架构。。
要有实倒。。不要只解释名词。
不胜感激。。
要有实倒。。不要只解释名词。
不胜感激。。
解决方案 »
- ASP.NET MVC分页问题
- 登录控件Login控件下Button事件代码,当密码不正确时,单步调试运行执行异常!
- DataSourceid='<%ItemList %>' 服务器标记的格式不正确。
- 可以用Response.Write语句在树型菜单的内容旁边加CheckBox吗,急.正确马上给分
- 请教sql语句的case的使用
- GridView的内容只显示10个字?
- 请问遺漏組件是什么意思(初学)
- 求给定时间查询SQL数据库的例子
- 使用CrystalReportViewer是不是也要先注册啊?
- 服务器数据库访问报错!详细问题如下
- asp调用wcf
- 想在上传文件的时候把该路径下的所有图片文件以缩略图形式显示并能进行选择和编辑 不是该怎么做呢
刚开始接触看petshop3就够用了~~
你可以下载微软苏鹏讲的petshop 与 n层架构~~可以在csdn上下载:最新三层构架网站 OA设计4大示例
using System;
using System.Collections.Generic;
using System.Text;namespace Model
{
public class guestbook //留言本实体类
{
private string _id, _name, _ip, _content, _email, _pic, _recontent;
DateTime _time; #region 设置属性
//id
public string Id
{
get { return _id; }
set { _id = value; }
}
//网名
public string Name
{
get { return _name; }
set { _name = value; }
} //时间
public DateTime Time
{
get { return _time; }
set { _time = value; }
} //IP地址
public string Ip
{
get { return _ip; }
set { _ip = value; }
} //留言内容
public string Content
{
get { return _content; }
set { _content = value; }
} //电子信箱
public string Email
{
get { return _email; }
set { _email = value; }
} //头像
public string Pic
{
get { return _pic; }
set { _pic = value; }
} //回复内容
public string Recontent
{
get { return _recontent; }
set { _recontent = value; }
}
#endregion }
}
----------------------接口IDAL----------------------
----------------------------
using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration ;
using System.Collections;
using System.Xml;
using System.Xml.XPath;
using IDAL;
using Model;namespace DAL
{
class xml:Iguestbook
{
string xmlGuestBookPath = System.Web.HttpContext.Current.Server.MapPath("../App_Data/") + ConfigurationSettings.AppSettings["xmlConnectionString"];
//得到所有留言
public guestbook[] getListGuestBook()
{
XmlDataDocument xmlDoc = new XmlDataDocument();
xmlDoc.Load(xmlGuestBookPath);//打开留言本xml文档
XmlNodeList xnl = xmlDoc.SelectSingleNode("guestBooks").ChildNodes;//读取所有结点
XmlElement xe ;//声明一个元素
ArrayList guest = new ArrayList();
foreach (XmlNode xn in xnl)
{
guestbook gb = new guestbook();
xe = (XmlElement)xn;//转换这个节点为一个元素
gb.Time = DateTime.Parse(xe.GetAttribute("time"));//得到留言时间
XmlNodeList xnlChild = xn.ChildNodes ;
gb.Id = gb.Time.ToString();//以时间为主键
gb.Name = xn.ChildNodes[0].InnerText;
gb.Ip = xn.ChildNodes[1].InnerText;
gb.Content = xn.ChildNodes[2].InnerText;
gb.Recontent = xn.ChildNodes[3].InnerText;
gb.Pic = xn.ChildNodes[4].InnerText;
gb.Email = xn.ChildNodes[5].InnerText;
guest.Add(gb);
}
guest.Reverse();//数组反转,最后一条留言在第一位显示
return (guestbook[])guest.ToArray(typeof(guestbook));
}
//得到一条留言
public guestbook[] getOnly(string id)
{
guestbook gb = new guestbook();
ArrayList guest = new ArrayList();
XmlDataDocument xmlDoc = new XmlDataDocument();
xmlDoc.Load(xmlGuestBookPath);
XmlNodeList xnl = xmlDoc.SelectSingleNode("guestBooks").ChildNodes;
XmlElement xe;
foreach (XmlNode xn in xnl)
{
xe = (XmlElement)xn;
gb.Time = DateTime.Parse (xe.GetAttribute("time"));
gb.Id = gb.Time.ToString();//以时间为主键
if (xe.GetAttribute("time") == id)
{
XmlNodeList xnlChild = xn.ChildNodes;
gb.Name = xn.ChildNodes[0].InnerText;
gb.Ip = xn.ChildNodes[1].InnerText;
gb.Content = xn.ChildNodes[2].InnerText;
gb.Recontent = xn.ChildNodes[3].InnerText;
gb.Pic = xn.ChildNodes[4].InnerText;
gb.Email = xn.ChildNodes[5].InnerText;
guest.Add(gb);
}
}
return (guestbook[])guest.ToArray(typeof(guestbook));
} //添加留言内容
public void setGuestBook(guestbook gb)
{
XmlDataDocument xmlDoc = new XmlDataDocument();
xmlDoc.Load(this.xmlGuestBookPath);
//选择根结点
XmlNode root = xmlDoc.SelectSingleNode("guestBooks");
XmlElement xe = xmlDoc.CreateElement("guestBook");//添加一个新结点元素
xe.SetAttribute("time", gb.Time.ToString());//添加元素属性
gb.Id = gb.Time.ToString();//以时间为关键字段 XmlElement xesub1 = xmlDoc.CreateElement("name");//创建子无素name
xesub1.InnerText = gb.Name;
xe.AppendChild(xesub1);//添加到xe结点中 XmlElement xesub2 = xmlDoc.CreateElement("ip");
xesub2.InnerText = gb.Ip;
xe.AppendChild(xesub2); XmlElement xesub3 = xmlDoc.CreateElement("content");
xesub3.InnerText = gb.Content;
xe.AppendChild(xesub3); XmlElement xesub4 = xmlDoc.CreateElement("recontent");
xesub4.InnerText = gb.Recontent;
xe.AppendChild(xesub4); XmlElement xesub5 = xmlDoc.CreateElement("pic");
xesub5.InnerText = gb.Pic;
xe.AppendChild(xesub5); XmlElement xesub6 = xmlDoc.CreateElement("email");
xesub6.InnerText = gb.Email;
xe.AppendChild(xesub6); root.AppendChild(xe);//新建节点添加到根结点中 xmlDoc.Save(this.xmlGuestBookPath);//保存 } //回复留言
public void setReGuestBook(guestbook Regb)
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(this.xmlGuestBookPath);
XmlNodeList xnl = xmlDoc.SelectSingleNode("guestBooks").ChildNodes;
foreach (XmlNode xn in xnl)
{
XmlElement xe = (XmlElement)xn;
if (xe.GetAttribute("time") == Regb.Id)
{
xe.ChildNodes[3].InnerText = Regb.Recontent;//添加回复内容
}
}
xmlDoc.Save(this.xmlGuestBookPath);
} //删除留言
public bool delGuestBook(string time)
{
XmlDataDocument xmlDoc = new XmlDataDocument();
xmlDoc.Load(this.xmlGuestBookPath);
XmlNodeList xnl = xmlDoc.SelectSingleNode("guestBooks").ChildNodes ;
XmlNode xnroot = xmlDoc.SelectSingleNode("guestBooks");//查找根结点,为删除子结点做准备
foreach (XmlNode xn in xnl)//循环所有子结点
{
XmlElement xe = (XmlElement)xn;
if (xe.GetAttribute("time") == time) //对比是否是要删除的结点
{
xnroot.RemoveChild(xe);//从根结点删除
xmlDoc.Save(this.xmlGuestBookPath);
return true;
}
}
return false;
}
}
}DAL ----- 2----FILE
--------------------------------------------
using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
using IDAL;
using Model;namespace DAL
{
class Sql : Iguestbook
{
private string connectionString = System.Configuration.ConfigurationSettings.AppSettings["sqlConnectionString"] + System.Web.HttpContext.Current.Server.MapPath("../App_Data/myPage.mdf");
/// <summary>
/// 读取数据库中所有留言
/// </summary>
/// <returns>返回guestbool数组</returns>
public guestbook[] getListGuestBook()
{
using (SqlConnection Conn = new SqlConnection(connectionString))
{
SqlCommand Cmd = new SqlCommand("select * from guestbook order by id desc",Conn);
Conn.Open(); ArrayList guest = new ArrayList();
SqlDataReader Ddr = Cmd.ExecuteReader();
while (Ddr.Read())
{
guestbook gb = new guestbook();
gb.Id = Ddr["id"].ToString();
gb.Name =Ddr["name"].ToString();
gb.Time = DateTime.Parse(Ddr["times"].ToString());
gb.Ip = Ddr["ip"].ToString();
gb.Content = Ddr["content"].ToString();
gb.Recontent =Ddr["recontent"].ToString();
gb.Pic = Ddr["pic"].ToString();
gb.Email = Ddr["email"].ToString();
guest.Add(gb);
}
Ddr.Close(); return (guestbook[])guest.ToArray(typeof(guestbook));
} }
----------------------------
using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration ;
using System.Collections;
using System.Xml;
using System.Xml.XPath;
using IDAL;
using Model;namespace DAL
{
class xml:Iguestbook
{
string xmlGuestBookPath = System.Web.HttpContext.Current.Server.MapPath("../App_Data/") + ConfigurationSettings.AppSettings["xmlConnectionString"];
//得到所有留言
public guestbook[] getListGuestBook()
{
XmlDataDocument xmlDoc = new XmlDataDocument();
xmlDoc.Load(xmlGuestBookPath);//打开留言本xml文档
XmlNodeList xnl = xmlDoc.SelectSingleNode("guestBooks").ChildNodes;//读取所有结点
XmlElement xe ;//声明一个元素
ArrayList guest = new ArrayList();
foreach (XmlNode xn in xnl)
{
guestbook gb = new guestbook();
xe = (XmlElement)xn;//转换这个节点为一个元素
gb.Time = DateTime.Parse(xe.GetAttribute("time"));//得到留言时间
XmlNodeList xnlChild = xn.ChildNodes ;
gb.Id = gb.Time.ToString();//以时间为主键
gb.Name = xn.ChildNodes[0].InnerText;
gb.Ip = xn.ChildNodes[1].InnerText;
gb.Content = xn.ChildNodes[2].InnerText;
gb.Recontent = xn.ChildNodes[3].InnerText;
gb.Pic = xn.ChildNodes[4].InnerText;
gb.Email = xn.ChildNodes[5].InnerText;
guest.Add(gb);
}
guest.Reverse();//数组反转,最后一条留言在第一位显示
return (guestbook[])guest.ToArray(typeof(guestbook));
}
//得到一条留言
public guestbook[] getOnly(string id)
{
guestbook gb = new guestbook();
ArrayList guest = new ArrayList();
XmlDataDocument xmlDoc = new XmlDataDocument();
xmlDoc.Load(xmlGuestBookPath);
XmlNodeList xnl = xmlDoc.SelectSingleNode("guestBooks").ChildNodes;
XmlElement xe;
foreach (XmlNode xn in xnl)
{
xe = (XmlElement)xn;
gb.Time = DateTime.Parse (xe.GetAttribute("time"));
gb.Id = gb.Time.ToString();//以时间为主键
if (xe.GetAttribute("time") == id)
{
XmlNodeList xnlChild = xn.ChildNodes;
gb.Name = xn.ChildNodes[0].InnerText;
gb.Ip = xn.ChildNodes[1].InnerText;
gb.Content = xn.ChildNodes[2].InnerText;
gb.Recontent = xn.ChildNodes[3].InnerText;
gb.Pic = xn.ChildNodes[4].InnerText;
gb.Email = xn.ChildNodes[5].InnerText;
guest.Add(gb);
}
}
return (guestbook[])guest.ToArray(typeof(guestbook));
} //添加留言内容
public void setGuestBook(guestbook gb)
{
XmlDataDocument xmlDoc = new XmlDataDocument();
xmlDoc.Load(this.xmlGuestBookPath);
//选择根结点
XmlNode root = xmlDoc.SelectSingleNode("guestBooks");
XmlElement xe = xmlDoc.CreateElement("guestBook");//添加一个新结点元素
xe.SetAttribute("time", gb.Time.ToString());//添加元素属性
gb.Id = gb.Time.ToString();//以时间为关键字段 XmlElement xesub1 = xmlDoc.CreateElement("name");//创建子无素name
xesub1.InnerText = gb.Name;
xe.AppendChild(xesub1);//添加到xe结点中 XmlElement xesub2 = xmlDoc.CreateElement("ip");
xesub2.InnerText = gb.Ip;
xe.AppendChild(xesub2); XmlElement xesub3 = xmlDoc.CreateElement("content");
xesub3.InnerText = gb.Content;
xe.AppendChild(xesub3); XmlElement xesub4 = xmlDoc.CreateElement("recontent");
xesub4.InnerText = gb.Recontent;
xe.AppendChild(xesub4); XmlElement xesub5 = xmlDoc.CreateElement("pic");
xesub5.InnerText = gb.Pic;
xe.AppendChild(xesub5); XmlElement xesub6 = xmlDoc.CreateElement("email");
xesub6.InnerText = gb.Email;
xe.AppendChild(xesub6); root.AppendChild(xe);//新建节点添加到根结点中 xmlDoc.Save(this.xmlGuestBookPath);//保存 } //回复留言
public void setReGuestBook(guestbook Regb)
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(this.xmlGuestBookPath);
XmlNodeList xnl = xmlDoc.SelectSingleNode("guestBooks").ChildNodes;
foreach (XmlNode xn in xnl)
{
XmlElement xe = (XmlElement)xn;
if (xe.GetAttribute("time") == Regb.Id)
{
xe.ChildNodes[3].InnerText = Regb.Recontent;//添加回复内容
}
}
xmlDoc.Save(this.xmlGuestBookPath);
} //删除留言
public bool delGuestBook(string time)
{
XmlDataDocument xmlDoc = new XmlDataDocument();
xmlDoc.Load(this.xmlGuestBookPath);
XmlNodeList xnl = xmlDoc.SelectSingleNode("guestBooks").ChildNodes ;
XmlNode xnroot = xmlDoc.SelectSingleNode("guestBooks");//查找根结点,为删除子结点做准备
foreach (XmlNode xn in xnl)//循环所有子结点
{
XmlElement xe = (XmlElement)xn;
if (xe.GetAttribute("time") == time) //对比是否是要删除的结点
{
xnroot.RemoveChild(xe);//从根结点删除
xmlDoc.Save(this.xmlGuestBookPath);
return true;
}
}
return false;
}
}
}DAL ----- 2----FILE
--------------------------------------------
using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
using IDAL;
using Model;namespace DAL
{
class Sql : Iguestbook
{
private string connectionString = System.Configuration.ConfigurationSettings.AppSettings["sqlConnectionString"] + System.Web.HttpContext.Current.Server.MapPath("../App_Data/myPage.mdf");
/// <summary>
/// 读取数据库中所有留言
/// </summary>
/// <returns>返回guestbool数组</returns>
public guestbook[] getListGuestBook()
{
using (SqlConnection Conn = new SqlConnection(connectionString))
{
SqlCommand Cmd = new SqlCommand("select * from guestbook order by id desc",Conn);
Conn.Open(); ArrayList guest = new ArrayList();
SqlDataReader Ddr = Cmd.ExecuteReader();
while (Ddr.Read())
{
guestbook gb = new guestbook();
gb.Id = Ddr["id"].ToString();
gb.Name =Ddr["name"].ToString();
gb.Time = DateTime.Parse(Ddr["times"].ToString());
gb.Ip = Ddr["ip"].ToString();
gb.Content = Ddr["content"].ToString();
gb.Recontent =Ddr["recontent"].ToString();
gb.Pic = Ddr["pic"].ToString();
gb.Email = Ddr["email"].ToString();
guest.Add(gb);
}
Ddr.Close(); return (guestbook[])guest.ToArray(typeof(guestbook));
} }
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Reflection;//反射命名空间
using DAL;
using IDAL;
/// <summary>
/// 工厂类
/// </summary>
public class DriveGuestbook
{
public DriveGuestbook()
{
//
// TODO: 在此处添加构造函数逻辑
// } public Iguestbook drive()
{
string assemblyName = ConfigurationManager.AppSettings["assembly"];//获取程序集名称 string constructor = ConfigurationManager.AppSettings["constructor"];//获取默认构造器名称 return (Iguestbook)Assembly.Load(assemblyName).CreateInstance(constructor, false);
//return (Iguestbook)Assembly.Load("DAL").CreateInstance("DAL.Sql", false);
} public void driver()
{
//(Iblog)Assembly.Load("DAL .DataSet1TableAdapters ").CreateInstance("DAL .DataSet1TableAdapters .blogNoteTableAdapter", false);
}}
-------------------
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Model;
using IDAL;
/// <summary>
/// guestbook中间业务层
/// </summary>public class InterGuestbook //: Iguestbook
{
public InterGuestbook()
{
//
// TODO: 在此处添加构造函数逻辑
//
} //得到留言列表内容
public guestbook [] getListGuestBook()
{
// (new DriveGuestbook()).drive().getListGuestBook();
// 反射类 相应驱动 接口方法
return (new DriveGuestbook()).drive().getListGuestBook();//通过接口调用数据访问层
} //得到一条留言
public guestbook [] only(string id)
{
return (new DriveGuestbook()).drive().getOnly(id);
} //添加留言内容
public void setGuestBook(string name,string email,string content,string pic)
{ guestbook gb = new guestbook();
if (name == string.Empty)
{
gb.Name = "匿名";
}
else
{
gb.Name = ReplaceString.Replace(name);
}
if (email == string.Empty)
{
gb.Email = "无";
}
else
{
gb.Email = email ;
}
gb.Content = ReplaceString.Replace(content);
gb.Ip = System.Web.HttpContext.Current.Request.UserHostAddress.ToString();
gb.Pic = "../App_Themes/Default/images/" + face(pic);
gb.Time = DateTime.Now;
(new DriveGuestbook()).drive().setGuestBook(gb);
} /// <summary>
/// 留言头像
/// </summary>
/// <param name="pic"></param>
/// <returns></returns>
private string face(string pic)
{
switch (pic)
{
case "face1": return "1.gif";
case "face2": return "2.gif";
case "face3": return "3.gif";
case "face4": return "4.gif";
case "face5": return "5.gif";
}
return "1.gif";
} //回复留言
public void setReGuestBook(string content,string id)
{
guestbook gb = new guestbook();
gb.Recontent ="<hr /><font color='#FF0000'>管理员回复:</font>"+ReplaceString.Replace(content);
gb.Id = id;
(new DriveGuestbook()).drive().setReGuestBook(gb);
} //删除留言
public bool delGuestBook(string id)
{
return (new DriveGuestbook ()).drive ().delGuestBook (id);
}
}
文字描述:
Clients对UI进行操作,UI调用Business进行相应的运算和处理,Business通过Data Access对Data Base进行操作。优点:
1、增加了代码的重用。Data Access可在多个项目中公用;Business可在同一项目的不同地方使用(如某个软件B/S和C/S部分可以共用一系列的Business组件)。
2、使得软件的分层更加明晰,便于开发和维护。美工人员可以很方便地设计UI设计,并在其中调用Business给出的接口,而程序开发人员则可以专注的进行代码的编写和功能的实现。
二、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 }
三、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); }
}由于数据库结构比较简单,在此就不再给出详细的表结构。四、UI层对Business中接口的调用
首先,在页面中添加对News类的引用。
然后,在页面中添加一个(DataGrid)dgNews用来显示新闻列表。
在Page Behind的Page_Load方法中添加如下代码: News objNews = new News();
this.dgNews.DataSource = objNews.GetNewsList();
this.dgNews.DataBind(); 至此,大功告成!
http://download.microsoft.com/download/8/0/1/801ff297-aea6-46b9-8e11-810df5df1032/Microsoft%20.NET%20Pet%20Shop%204.0.msi
www.cnblogs.com/mail-ricklee