sportdog

导航

 

业务描述:通过简单工厂设计模式实现对不同数据库的支持(如: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

 

posted on 2018-07-30 15:37  sportdog  阅读(149)  评论(0编辑  收藏  举报