前几天在论坛看到有人讨论三层架构,有人说应该吧UI层和其他层完全分离,UI层只接收传值工作。觉得很有道理。所以动手做了个。在DAL层查询语句。BLL层把数据封装为数据集,然后在UI层直接引用数据集。相当于把DAL层和BLL层看做一个DataSource。UI层只绑定这个DataSource。
描述的不怎么清楚,直接贴代码:
UI层:
DataTable dt = BLL.Select();
GridView1.DataSource = dt;
GridView1.DataBind();
BLL层:
public static DataTable Select()
{
DataTable ds = DAL.Select();
DataTable dt = new DataTable("uss");
DataColumn dc1 = new DataColumn("no", Type.GetType("System.Int32"));
DataColumn dc2 = new DataColumn("name", Type.GetType("System.String"));
DataColumn dc3 = new DataColumn("width", Type.GetType("System.Int32"));
DataColumn dc4 = new DataColumn("height", Type.GetType("System.Int32"));
DataColumn dc5 = new DataColumn("ip", Type.GetType("System.String"));
DataColumn dc6 = new DataColumn("scrType", Type.GetType("System.String"));
DataColumn dc7 = new DataColumn("conType", Type.GetType("System.String"));
dt.Columns.Add(dc1);
dt.Columns.Add(dc2);
dt.Columns.Add(dc3);
dt.Columns.Add(dc4);
dt.Columns.Add(dc5);
dt.Columns.Add(dc6);
dt.Columns.Add(dc7);
foreach (DataRow row in ds.Rows)
{
DataRow dr = dt.NewRow();
dr["no"] = int.Parse(row["nNo"].ToString());
dr["name"] = row["nName"].ToString();
dr["width"] = int.Parse(row["nWidth"].ToString());
dr["height"] = int.Parse(row["nHeight"].ToString());
dr["ip"] = row["pIP"].ToString();
dr["scrType"] = System.Enum.GetName(typeof(EnumTest.ScrType), int.Parse(row["nScrType"].ToString()));
dr["conType"] = (ConTypeService.GetTypeById(row["nConType"].ToString())).Controlcode;
dt.Rows.Add(dr);
}
return dt;
}
DAL层:
public static DataTable Select()
{
DBHelpers DBHelpers1 = new DBHelpers();
string strsql;
strsql = "select * from users";
DataTable dt = new DataTable();
dt = DBHelpers1.Readstr(strsql);
return dt;
}
DBHelpers.cs文件:
public DataTable Readstr(String strSql)
{
DataTable ds = new DataTable();
MySqlConnection Conn = new MySqlConnection(strConn);
Conn.Open();
MySqlDataAdapter Cmd = new MySqlDataAdapter(strSql, Conn);
Cmd.Fill(ds);
Conn.Close();
return ds;
}
model层:
1)枚举
public enum ScrType
{
单身=1,
恋爱=2,
失恋=3,
}
2)user.cs文件
private ConType ncontype;
/// <summary>
/// PK
/// </summary>
public ConType Ncontype
{get { return ncontype; }
set { ncontype = value; }}
private string nname;
public string Nname
{get { return nname; }
set { nname = value; }}
private int nno;
public int Nno
{get { return nno; }
set { nno = value; }}
private int nwidth;
public int Nwidth
{get { return nwidth; }
set { nwidth = value; }}
private int nheight;
public int Nheight
{get { return nheight; }
set { nheight = value; }}
private int nscrtype;
public int Nscrtype
{get { return nscrtype; }
set { nscrtype = value; }}
private string pip;
public string Pip
{get { return pip; }
set { pip = value; }}
3)ConType.cs文件
private string conde;
public string Concode
{get { return concode; }
set { concode = value; }}
private string contype;
public string Contype
{get { return contype; }
set { contype = value; }}
这个是涉及到多表查询的。还有枚举......
因为原先找人教的时候他教我的很多东西都是在UI层做。导致UI层的代码很多。无法实现UI层与下层的完全分离,到时候换UI层的话要修改好多代码。所以我想直接封装好然后在UI调用,这样美工即使不懂代码也能在拿到下层的时候直接弄出UI层.....
如果需要单项绑定的话用IList<>同样的全部封装好,然后UI层直接给参数名,修改、删除页同样的封装。如此UI层不就和其他层完全分离了么,到时候要换UI层的话随便怎么换,把WEBFROM转到WINFrom都没问题。都只要换UI,相当于把BLL和DAL与UI完全分离....