不知道这是什么模式,好像是工厂吧

看了一个博客系统的源代码,里面的这种设计觉得挺好,我不懂设计模式,反正用反射处理了,摘录分享下。

public class UserInfo : IComparable
    {
        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;
        /// 
        /// 用户名
        /// 
        public string Name
        {
            get { return _name; }
            set { _name = value; }
        }

        private string _password;
        /// 
        /// 密码
        /// 
        public string Password
        {
            get { return _password; }
            set { _password = value; }
        }

        private int _displayorder;
        /// 
        /// 排序
        /// 
        public int Displayorder
        {
            get { return _displayorder; }
            set { _displayorder = value; }
        }

        //...
    }
public interface IUser
    {
        /// 
        /// 添加用户
        /// 
        /// 
        /// 
        int InsertUser(UserInfo _userinfo);

        /// 
        /// 修改用户
        /// 
        /// 
        /// 
        int UpdateUser(UserInfo _userinfo);

        /// 
        /// 删除用户
        /// 
        /// 
        /// 
        int DeleteUser(int userID);

        ///// 
        ///// 根据用户名和密码获取用户
        ///// 
        ///// 
        ///// 
        ///// 
        //UserInfo GetUser(string userName, string password);

        /// 
        /// 获取全部用户
        /// 
        /// 
        List GetUserList();

        /// 
        /// 是否存在
        /// 
        /// 
        /// 
        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;

        /// 
        /// 构造函数
        /// 
        private DataAccess() { }

        public static IUser CreateUser()
        {
            string className = path + ".User";
            //  return (IUser)Assembly.Load(path).CreateInstance(className);
            return CreateInstance(_iuser, className);
        }

        //.....

        /// 
        /// 实例化
        /// 
        /// 
        /// 
        /// 
        /// 
        public static T CreateInstance(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();
        /// 
        /// 列表
        /// 
        private static List _users;

        private static object lockHelper = new object();

        static UserManager()
        {
            LoadUser();
        }

        public static void LoadUser()
        {
            if (_users == null)
            {
                lock (lockHelper)
                {
                    _users = dao.GetUserList();
                }
            }
        }

        /// 
        /// 添加用户
        /// 
        /// 
        /// 
        public static int InsertUser(UserInfo _userinfo)
        {
            _userinfo.Id = dao.InsertUser(_userinfo);
            _users.Add(_userinfo);
            _users.Sort();

            return _userinfo.Id;
        }

        /// 
        /// 修改用户
        /// 
        /// 
        /// 
        public static int UpdateUser(UserInfo _userinfo)
        {
            _users.Sort();
            return dao.UpdateUser(_userinfo);
        }

        /// 
        /// 删除用户
        /// 
        /// 用户id
        public static int DeleteUser(int userId)
        {
            UserInfo user = GetUser(userId);
            if (user != null)
            {
                _users.Remove(user);
            }

            return dao.DeleteUser(userId);
        }

        /// 
        /// 获取全部用户
        /// 
        public static List GetUserList()
        {
            return _users;
        }

        /// 
        /// 是否存在
        /// 
        /// 用户名
        public static bool ExistsUserName(string userName)
        {
            return dao.ExistsUserName(userName);
        }

        /// 
        /// 获取用户
        /// 
        /// 用户id
        public static UserInfo GetUser(int userId)
        {
            foreach (UserInfo user in _users)
            {
                if (user.Id == userId)
                {
                    return user;
                }
            }
            return null;
        }

        /// 
        /// 根据用户名获取用户 
        /// 
        /// 用户名
        public static UserInfo GetUser(string userName)
        {
            foreach (UserInfo user in _users)
            {
                if (user.Name.ToLower() == userName.ToLower())
                {
                    return user;
                }
            }
            return null;
        }

        /// 
        /// 根据用户名和密码获取用户
        /// 
        /// 
        /// 
        /// 
        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
    {
        /// 
        /// 获取全部
        /// 
        /// 
        public List GetUserList()
        {
            string cmdText = "select * from [tb_users]  order by [displayorder] asc,[userid] asc";
            return DataReaderToUserList(SqliteDbHelper.ExecuteReader(cmdText));

        }

        /// 
        /// 数据转换
        /// 
        /// 
        /// 
        private List DataReaderToUserList(SqliteDataReader read)
        {
            List list = new List();
            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;
        }

        //.....

    }
}



你可能感兴趣的:(C#)