看了一个博客系统的源代码,里面的这种设计觉得挺好,我不懂设计模式,反正用反射处理了,摘录分享下。
public class UserInfo : IComparable<UserInfo> { public int CompareTo(UserInfo other) { if (this.Displayorder != other.Displayorder) return this.Displayorder.CompareTo(other.Displayorder); return this.Id.CompareTo(other.Id); } private int _id; public int Id { get { return _id; } set { _id = value; } } private string _name; /// <summary> /// 用户名 /// </summary> public string Name { get { return _name; } set { _name = value; } } private string _password; /// <summary> /// 密码 /// </summary> public string Password { get { return _password; } set { _password = value; } } private int _displayorder; /// <summary> /// 排序 /// </summary> public int Displayorder { get { return _displayorder; } set { _displayorder = value; } } //... }
public interface IUser { /// <summary> /// 添加用户 /// </summary> /// <param name="_userinfo"></param> /// <returns></returns> int InsertUser(UserInfo _userinfo); /// <summary> /// 修改用户 /// </summary> /// <param name="_userinfo"></param> /// <returns></returns> int UpdateUser(UserInfo _userinfo); /// <summary> /// 删除用户 /// </summary> /// <param name="userID"></param> /// <returns></returns> int DeleteUser(int userID); ///// <summary> ///// 根据用户名和密码获取用户 ///// </summary> ///// <param name="userName"></param> ///// <param name="password"></param> ///// <returns></returns> //UserInfo GetUser(string userName, string password); /// <summary> /// 获取全部用户 /// </summary> /// <returns></returns> List<UserInfo> GetUserList(); /// <summary> /// 是否存在 /// </summary> /// <param name="userName"></param> /// <returns></returns> bool ExistsUserName(string userName); }
public class DataAccess { private static readonly string path = "Web.Data.Access"; private static object lockHelper = new object(); public static IUser _iuser = null; /// <summary> /// 构造函数 /// </summary> private DataAccess() { } public static IUser CreateUser() { string className = path + ".User"; // return (IUser)Assembly.Load(path).CreateInstance(className); return CreateInstance<IUser>(_iuser, className); } //..... /// <summary> /// 实例化 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="_instance"></param> /// <param name="className"></param> /// <returns></returns> public static T CreateInstance<T>(T _instance, string className) { if (_instance == null) { lock (lockHelper) { _instance = (T)Assembly.Load(path).CreateInstance(className); } } return _instance; } }
public class UserManager { private static IUser dao = DataAccess.CreateUser(); /// <summary> /// 列表 /// </summary> private static List<UserInfo> _users; private static object lockHelper = new object(); static UserManager() { LoadUser(); } public static void LoadUser() { if (_users == null) { lock (lockHelper) { _users = dao.GetUserList(); } } } /// <summary> /// 添加用户 /// </summary> /// <param name="_userinfo"></param> /// <returns></returns> public static int InsertUser(UserInfo _userinfo) { _userinfo.Id = dao.InsertUser(_userinfo); _users.Add(_userinfo); _users.Sort(); return _userinfo.Id; } /// <summary> /// 修改用户 /// </summary> /// <param name="_userinfo"></param> /// <returns></returns> public static int UpdateUser(UserInfo _userinfo) { _users.Sort(); return dao.UpdateUser(_userinfo); } /// <summary> /// 删除用户 /// </summary> /// <param name="userId">用户id</param> public static int DeleteUser(int userId) { UserInfo user = GetUser(userId); if (user != null) { _users.Remove(user); } return dao.DeleteUser(userId); } /// <summary> /// 获取全部用户 /// </summary> public static List<UserInfo> GetUserList() { return _users; } /// <summary> /// 是否存在 /// </summary> /// <param name="userName">用户名</param> public static bool ExistsUserName(string userName) { return dao.ExistsUserName(userName); } /// <summary> /// 获取用户 /// </summary> /// <param name="userId">用户id</param> public static UserInfo GetUser(int userId) { foreach (UserInfo user in _users) { if (user.Id == userId) { return user; } } return null; } /// <summary> /// 根据用户名获取用户 /// </summary> /// <param name="userName">用户名</param> public static UserInfo GetUser(string userName) { foreach (UserInfo user in _users) { if (user.Name.ToLower() == userName.ToLower()) { return user; } } return null; } /// <summary> /// 根据用户名和密码获取用户 /// </summary> /// <param name="userName"></param> /// <param name="password"></param> /// <returns></returns> public static UserInfo GetUser(string userName, string password) { foreach (UserInfo user in _users) { if (user.Name.ToLower() == userName.ToLower() && user.Password.ToLower() == password.ToLower()) { return user; } } return null; } }
namespace Web.Data.Access { public class User { /// <summary> /// 获取全部 /// </summary> /// <returns></returns> public List<UserInfo> GetUserList() { string cmdText = "select * from [tb_users] order by [displayorder] asc,[userid] asc"; return DataReaderToUserList(SqliteDbHelper.ExecuteReader(cmdText)); } /// <summary> /// 数据转换 /// </summary> /// <param name="read"></param> /// <returns></returns> private List<UserInfo> DataReaderToUserList(SqliteDataReader read) { List<UserInfo> list = new List<UserInfo>(); while (read.Read()) { UserInfo _userinfo = new UserInfo(); _userinfo.Id = Convert.ToInt32(read["Id"]); _userinfo.Name = Convert.ToString(read["Name"]); _userinfo.Displayorder = Convert.ToInt32(read["Displayorder"]); list.Add(_userinfo); } read.Close(); return list; } //..... } }