zoukankan      html  css  js  c++  java
  • 抽象工厂模式+反射+配置文件 实现数据访问程序

    User表和Department表

        class User
        {
            private int _id;
            public int Id
            {
                get { return _id; }
                set { _id = value; }
            }
    
            private string _name;
            public string Name
            {
                get { return _name; }
                set { _name = value; }
            }
        }
    
        class Department
        {
            private int _id;
            public int Id
            {
                get { return _id; }
                set { _id = value; }
            }
    
            private string _name;
            public string Name
            {
                get { return _name; }
                set { _name = value; }
            }
        }

    IUser类(Department类相类似)

        interface IUser
        {
            void Insert(User user);
    
            User GetUser(int id);
        }
    
        class SqlserverUser : IUser
        {
            public void Insert(User user)
            {
                Console.WriteLine("在SQL Server中User表增加一条记录");
            }
    
            public User GetUser(int id)
            {
                Console.WriteLine("在SQL Server中根据ID得到User表的一条记录");
                return null;
            }
        }
    
        class AccessUser : IUser
        {
            public void Insert(User user)
            {
                Console.WriteLine("在Access中User表增加一条记录");
            }
    
            public User GetUser(int id)
            {
                Console.WriteLine("在Access中根据ID得到User表的一条记录");
                return null;
            }
        }

    DataAccess类

        class DataAccess
        {
            private static readonly string AssemblyName = "抽象工厂";
            //直接写字符串获得db类型
            //private static readonly string db = "Sqlserver";
            //private static readonly string db = "Access";
            //读配置文件获得db类型
            private static readonly string db = ConfigurationManager.AppSettings["DB"];
    
            public static IUser CreateUser()
            {
                string className = AssemblyName + "." + db + "User";
                return (IUser)Assembly.Load(AssemblyName).CreateInstance(className);
            }
    
            public static IDepartment CreateDepartment()
            {
                string className = AssemblyName + "." + db + "Department";
                return (IDepartment)Assembly.Load(AssemblyName).CreateInstance(className);
            }
        }

    App.config配置文件

    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
      <appSettings>
        <add key="DB" value="Sqlserver"/>
      </appSettings>
    </configuration>

    客户端代码

                User user = new User();
                IUser iu = DataAccess.CreateUser();
                iu.Insert(user);
                iu.GetUser(1);
    
                Department dept = new Department();
                IDepartment id = DataAccess.CreateDepartment();
                id.Insert(dept);
                id.GetDepartment(1);
    
                Console.ReadKey();
  • 相关阅读:
    获取本地计算机的主机信息
    用函数SendARP()获取局域网计算机的MAC地址
    获取本地计算机的操作系统
    获取本地计算机名和IP地址
    基于TCP(面向连接)的Socket编程
    SMTP协议
    使用截获消息响应截断某些消息
    《Windows核心编程》读书笔记 上
    vc++高级班之多线程篇[7]---线程间的同步机制②
    vc++高级班之多线程篇[6]---线程间的同步机制①
  • 原文地址:https://www.cnblogs.com/baiqjh/p/2844557.html
Copyright © 2011-2022 走看看