• 简单工厂实现支持不同数据库


    业务描述:通过简单工厂设计模式实现对不同数据库的支持(如:Ms Sqlserver,MySql)

    思路:

    解决方案下包括以下项目

    1. 项目StudyEntity(定义对象的结构)、接口
    2. 项目StudyDal(MySqlStudyDal) 数据层(两个项目)
    3. 项目StudyBll  业务逻辑层
    4. Web层

    引用关系:

    数据层引用StudyEntity

    业务逻辑层引用StudyEntity、数据层

    Web层引用StudyEntity、业务逻辑层

    StudyEntity层代码 User.cs:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace StudyEntity
    {
        public class User
        {
            public string Staffno
            {
                get;
                set;
            }
    
            public string UserName
            {
                get;
                set;
            }
    
        }
    }
    

    StudyEntity层代码 IUser.cs:

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.Threading.Tasks;
     6 
     7 namespace StudyEntity
     8 {
     9     public interface IUser
    10     {
    11         User GetItem(string userStaffno);
    12         User Save(User user);
    13     }
    14 }
    View Code

    数据层StudyDal [MSSqlServer 数据库]

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.Threading.Tasks;
     6 using StudyEntity;
     7 
     8 namespace StudyDal
     9 {
    10     public class UserDal:IUser
    11     {
    12         #region IUser 成员
    13 
    14         public User GetItem(string userStaffno)
    15         {
    16             User user = new User();
    17             user.Staffno = "200718";
    18             user.UserName = "Snow";
    19             return user;
    20         }
    21 
    22         public User Save(User user)
    23         {
    24             throw new NotImplementedException();
    25             
    26         }
    27 
    28         #endregion
    29     }
    30 }
    View Code

    数据层 MySqlDal [My Sql 数据库]

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.Threading.Tasks;
     6 using StudyEntity;
     7 
     8 namespace MySqlDal
     9 {
    10     public class MySqlUserDal:IUser
    11     {
    12         #region IUser 成员
    13 
    14         public User GetItem(string userStaffno)
    15         {
    16             User user = new User();
    17             user.Staffno = "200500";
    18             user.UserName = "MySql";
    19             return user;
    20         }
    21 
    22         public User Save(User user)
    23         {
    24             throw new NotImplementedException();
    25         }
    26 
    27         #endregion
    28     }
    29 }
    View Code

    业务逻辑层StudyBLL 之 UserBll

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using StudyEntity;
    using StudyDal;
    
    namespace StudyBLL
    {
        public class UserBll
        {
            /// <summary>
            /// 通过工号获取员工
            /// </summary>
            /// <param name="staffNumber"></param>
            /// <returns></returns>
            public User GetItem(string staffNumber)
            {
                UserDal userDal = new UserDal();
    
                return userDal.GetItem(staffNumber);
            }
        }
    }
    View Code

    业务逻辑层StudyBLL 之工厂类(Factory.cs)

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using StudyEntity;
    using StudyDal;
    using MySqlDal;
    
    namespace StudyBLL
    {
        public class Factoy
        {
            public static IUser CreateUserInstance(string dbType)
            {
                IUser user;
    
                switch (dbType)
                { 
                    case "1":
                        user = new UserDal();
                        break;
                    case "2":
                        user = new MySqlUserDal();
                        break;
                    default:
                        user = null;
                        break;
                }
                return user;
            }
        }
    }
    View Code

    Web层调用:传递的参数1 或 2 可通过webConfig文件配置

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Web;
     5 using System.Web.UI;
     6 using System.Web.UI.WebControls;
     7 using StudyBLL;
     8 using StudyEntity;
     9 
    10 
    11 namespace Study2018
    12 {
    13     /// <summary>
    14     /// 程序支持不同数据库的代码写法
    15     /// </summary>
    16     public partial class TestGetUser : System.Web.UI.Page
    17     {
    18         protected void Page_Load(object sender, EventArgs e)
    19         {
    20 
    21         }
    22 
    23         protected void btnGetUser_Click(object sender, EventArgs e)
    24         {
    25             User user = Factoy.CreateUserInstance("1").GetItem("xxx");
    26             Response.Write(user.Staffno + user.UserName); 
    27 
    28         }
    29 
    30         protected void btnGetMySqlUser_Click(object sender, EventArgs e)
    31         {
    32             User user = Factoy.CreateUserInstance("2").GetItem("xxx");
    33             Response.Write(user.Staffno + user.UserName); 
    34         }
    35     }
    36 }
    View Code
  • 相关阅读:
    Vue.directive()方法创建全局自定义指令
    vue中通过ref属性来获取dom的引用
    v-cloak指令
    v-if和v-show
    vue中的v-on事件监听机制
    vue指令v-model
    vue中v-for系统指令的使用
    从零开始在虚拟机中搭建一个4个节点的CentOS集群(一)-----下载及配置CentOS
    MySQL-数据库创建与删除
    MySQL-PREPARE语句
  • 原文地址:https://www.cnblogs.com/sportdog/p/9390864.html
Copyright © 2020-2023  润新知