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();
  • 相关阅读:
    合并代码与前后端联调分别什么意思?
    自动化执行禅道 自动化写用例
    01- 计算机网络体系结构
    12- 输出重定向
    11- 文件权限管理命令
    10- sudo,exit命令
    09- Linux下压缩和解压命令
    021- Java语言基础-基本数据类型的一些问题和总结
    根据上传的MultipartFile通过springboot转化为File类型并调用通过File文件流的方法上传特定服务器
    文件上传报错java.io.FileNotFoundException拒绝访问
  • 原文地址:https://www.cnblogs.com/baiqjh/p/2844557.html
Copyright © 2011-2022 走看看