第一部份:工厂模式
一、项目名称及描述:(实现步骤为:4-3-6-5-2-1)
1、WEB=表示层
2、BLL=业务逻辑层
3、IDAL=数据访问层接口定义
4、Model=业务实体
5、DALFactory=数据层的抽象工厂(创建反射)
6、SQLServerDAL=SQLServer数据访问层 / OracleDAL=Oracle数据访问层
DBUtility 数据库访问组件基础类(SqlDbHelper,OracleDbHelper,等)
二、项目引用关系
1、Web 引用 BLL。
2、BLL 引用 IDAL,Model,使用DALFactory创建实例。
3、IDAL 引用 Model。
4、Model 无引用。
5、DALFactory 引用IDAL,通过读取web.config里设置的程序集,加载类的实例,返回给BLL使用。
6、SQLServerDAL 引用 Model和IDAL,被DALFactory加载的程序集,实现接口里的方法。
三、实现步骤(7步走)
数据库user表
1 create table t_user 2 ( 3 id int identity(1,1), /*流水号*/ 4 uid varchar(10) primary key, /*登录名*/ 5 uname varchar(50), /*昵称*/ 6 upassword varchar(50), /*密码*/ 7 ulevel varchar(10) /*用户级别*/ 8 )
1、创建Model,实现业务实体。(每张表对应一个实体模型)
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 6 namespace Model 7 { 8 public class UserInfo 9 { 10 private string _uId; 11 private string _uName; 12 private string _uPassword; 13 private string _uLevel; 14 15 //构造函数 16 public UserInfo(string uid,string uname,string upassword,string ulevel) 17 { 18 this._uId = uid; 19 this._uName = uname; 20 this._uPassword = upassword; 21 this._uLevel = ulevel; 22 } 23 public string uId 24 { 25 get {return this._uId;} 26 set { this._uId = value; } 27 } 28 public string uName 29 { 30 get { return this._uName; } 31 set { this._uName = value; } 32 } 33 public string uPassword 34 { 35 get { return this._uPassword; } 36 set { this._uPassword = value; } 37 } 38 public string uLevel 39 { 40 get { return this._uLevel; } 41 set { this._uLevel = value; } 42 } 43 44 } 45 }
2、创建IDAL,实现接口。(增删改查)(每张表对应一个接口)
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using Model; 6 7 namespace IDAL 8 { 9 public interface IUser 10 { 11 //取得所有User 12 IList<UserInfo> GetAllUser(); 13 //按ID取得单个User 14 UserInfo GetUserByID(string uId); 15 //增加 16 int AddUser(UserInfo userInfo); 17 //修改 18 int UpdateUser(UserInfo userInfo); 19 //删除 20 int DeleteUser(string uId); 21 //判断用户名和密码是否存在 22 bool CheckUser(string uId, string uPassword); 23 } 24 }
3、创建SQLServerDAL,实现接口里的方法。(引用DBUtility的DBHelper 实现增删改查)(每张表对应一个操作类Operater)
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data.SqlClient; using System.Data; using Model; using DBUtility; using IDAL; namespace SQLServerDAL { public class UserOper:IUser { #region IUser 成员 //获取所有user信息 public IList<UserInfo> GetAllUser() { //throw new NotImplementedException(); IList<UserInfo> userInfo = new List<UserInfo>(); using (SqlDataReader dr = DBUtility.SqlDbHelper.ExecuteReader("select * from t_user")) { while (dr.Read()) { UserInfo _user = new UserInfo(dr[1].ToString(), dr[2].ToString(), dr[3].ToString(), dr[4].ToString()); userInfo.Add(_user); } } return userInfo; } //按ID查询USER信息 public UserInfo GetUserByID(string uId) { UserInfo _user=null; using (SqlDataReader dr = DBUtility.SqlDbHelper.ExecuteReader("select top 1 * from t_user where uid='" + uId + "'")) { if (dr.Read()) { _user=new UserInfo(dr[1].ToString(), dr[2].ToString(), dr[3].ToString(), dr[4].ToString()); } } return _user; } //增加一条记录 public int AddUser(UserInfo userInfo) { //throw new NotImplementedException(); //return 1; string sql = "insert into t_user values('" + userInfo.uId + "','" + userInfo.uName + "','" + userInfo.uPassword + "','" + userInfo.uLevel + "')"; return DBUtility.SqlDbHelper.ExecuteNonQuery(sql); } //更新一条数据 public int UpdateUser(UserInfo userInfo) { string sql = "update t_user set uname='" + userInfo.uName + "',upassword='" + userInfo.uPassword + "',ulevel='" + userInfo.uLevel + "' where uid='" + userInfo.uId + "'"; return DBUtility.SqlDbHelper.ExecuteNonQuery(sql); } //删除一条数据 public int DeleteUser(string uId) { string sql = "delete from t_user where uid='" + uId + "'"; return DBUtility.SqlDbHelper.ExecuteNonQuery(sql); } //判断用户名和密码是否正确 public bool CheckUser(string uId, string uPassword) { string sql = "select count(*) from t_user where uid='" + uId + "' and upassword='" + uPassword + "'"; object obj = DBUtility.SqlDbHelper.ExecuteScalar(sql); if (obj != null) { if(Convert.ToInt32(obj)>0) { return true; } else { return false; } } else { return false; } } #endregion } }
4、增加(Web层)web.config里的配置信息,为SQLServerDAL的程序集。
<appSettings><add key="WebDAL" value="SQLServerDAL"/> </appSettings>
5、创建DALFactory,返回程序集的指定类的实例(DataAccess类)。代码
1 using System; 2 using System.Collections.Generic; 3 using System.Reflection; 4 using System.Linq; 5 using System.Text; 6 using System.Configuration; 7 using IDAL; 8 9 namespace DALFactory 10 { 11 public sealed class DataAccess 12 { 13 //读取从web.config程序集 14 private static readonly string AssemblyPath = ConfigurationSettings.AppSettings["WebDAL"].ToString(); 15 16 //创建SQLServerDAL实例对象 17 public static object CreateObject(string AssemblyPath, string ClassNamespace) 18 { 19 //有加命名空间时 才需要增加这句 20 //ClassNamespace = "FactoryTest." + ClassNamespace; 21 22 // object objType = DataCache.GetCache(ClassNamespace);//从缓存读取 23 object objType = null; 24 if (objType == null) 25 { 26 try 27 { 28 objType = Assembly.Load(AssemblyPath).CreateInstance(ClassNamespace);//反射创建 29 // DataCache.SetCache(ClassNamespace, objType);// 写入缓存 30 } 31 catch 32 { } 33 } 34 return objType; 35 } 36 37 //创建user实例实例对象 38 public static IUser CreateUser() 39 { 40 string ClassNamespace = AssemblyPath + ".UserOper"; 41 object obj = CreateObject(AssemblyPath, ClassNamespace); 42 return (IDAL.IUser)obj; 43 } 44 } 45 }
6、创建BLL,调用DALFactory,得到程序集指定类的实例,完成数据操作方法。代码:
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using IDAL; 6 using Model; 7 8 9 10 11 namespace BLL 12 { 13 public class UserBLL 14 { 15 private static readonly IUser dal = DALFactory.DataAccess.CreateUser(); 16 17 //获取所有user信息 18 public IList<UserInfo> GetAllUser() 19 { 20 return dal.GetAllUser(); 21 } 22 //增加一条记录 23 public int AddUser(UserInfo userInfo) 24 { 25 return dal.AddUser(userInfo); 26 } 27 //更新一条数据 28 public int UpdateUser(UserInfo userInfo) 29 { 30 return dal.UpdateUser(userInfo); 31 } 32 33 //按ID查询USER信息 34 public UserInfo GetUserByID(string uid) 35 { 36 return dal.GetUserByID(uid); 37 } 38 //删除一条数据 39 public int DeleteUser(string uid) 40 { 41 return dal.DeleteUser(uid); 42 } 43 //检查用户名和密码是否正确 44 public bool CheckUser(string uid,string upassword) 45 { 46 return dal.CheckUser(uid, upassword); 47 } 48 } 49 50 }
7、创建WEB,调用BLL里的数据操作方法。
1 private void bindToGv() 2 { 3 BLL.UserBLL user = new BLL.UserBLL(); 4 this.GridView2.DataSource = user.GetAllUser(); 5 this.GridView2.DataBind(); 6 }