这几天我们复习三层架构因此对三层架构有了更新一层的认识,下面就由我来介绍一下.net中的三层架构
其实一般说是三层但是可以根据不同的需求可以多加几层我一般会用以下几层
1、表现层(UI):通俗讲就是展现给用户的界面,即用户在使用一个系统的时候他的所见所得。
2、业务逻辑层(BLL):针对具体问题的操作,也可以说是对数据层的操作,对数据业务逻辑处理。
3、数据访问层(DAL):该层所做事务直接操作数据库,针对数据的增添、删除、修改、更新、查找等。s
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Model
{
/// <summary>
/// 新闻信息
/// </summary>
public class NewsInfo
{
/// <summary>
/// 新闻id
/// </summary>
private int id;
public int Id
{
get { return id; }
set { id = value; }
}
/// <summary>
/// 新闻标题
/// </summary>
private string title;
public string Title
{
get { return title; }
set { title = value; }
}
/// <summary>
/// 新闻内容
/// </summary>
private string contents;
public string Contents
{
get { return contents; }
set { contents = value; }
}
/// <summary>
/// 发布新闻时间
/// </summary>
private DateTime times;
public DateTime Times
{
get { return times; }
set { times = value; }
}
}
}
DButility 此层引用sqlhleper
DAL层
引用DButility 和 Model
代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using DButility;
using Model;
using System.Data;
using BibleDAL.SqlHelper;
namespace DAL
{
public class News
{
/// <summary>
/// 得到N条新闻列表(按时间)
/// </summary>
/// <param name="num">n条</param>
/// <returns>新闻列表</returns>
public List<NewsInfo> GetNewsList(int num)
{
List<NewsInfo> newsList = new List<NewsInfo>();
SqlConnection conn = GetConn();
string sql = "select top " + num + " * from news order by times desc";
DataSet ds = SqlHelper.ExecuteDataset(conn,CommandType.Text,sql);
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
NewsInfo info = new NewsInfo();
info.Id= Convert.ToInt32(ds.Tables[0].Rows[i]["id"].ToString());
info.Title = ds.Tables[0].Rows[i]["title"].ToString();
info.Contents = ds.Tables[0].Rows[i]["contents"].ToString();
info.Times = Convert.ToDateTime(ds.Tables[0].Rows[i]["times"].ToString());
newsList.Add(info);
}
return newsList;
}
/// <summary>
/// 得到数据库连接对象
/// </summary>
/// <returns></returns>
public static SqlConnection GetConn()
{
SqlConnection conn = new SqlConnection();
conn.ConnectionString = "server=.;database=zhihuigu;uid=sa;pwd=;";
conn.Open();
return conn;
}
}
}
BLL层
引用 DAL和Model层
代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Model;
using DAL;
namespace BLL
{
public class News
{
public DAL.News news = new DAL.News();
public String GetNewsList(int num)
{
StringBuilder sb = new StringBuilder();
List<NewsInfo> newsList= news.GetNewsList(num);
sb.Append("<ul>");
for (int i = 0; i <newsList.Count;i++)
{
//sb.Append("<li><a href='looknews.aspx?id=" + newsList[i].Id + "'>"+newsList[i].Title+"</a></li>");
sb.Append("<li><a href=\"looknews.aspx?id=" + newsList[i].Id + "\">" + newsList[i].Title + "</a></li>");
}
sb.Append("</ul>");
return sb.ToString();
}
}
}
UI层 (界面层)
引用 BLL层
cs代码:
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Model;
using BLL;
public partial class _Default : System.Web.UI.Page
{
public BLL.News news = new News();
protected void Page_Load(object sender, EventArgs e)
{
}
}
aspx 代码:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="newslist.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<%=news.GetNewsList(10) %>
</div>
</form>
</body>
</html>