三層架構之泛型抽象工廠


原來寫過一篇三層架構之泛型應用的簡單登錄,已經過去2年了,今天有一朋友問我關於抽象工廠的問題,就把自己后來解耦的方法從項目中拿出來了,方便大家學習。

我重新寫了一個例子項目,如下截圖:

XU.Model層中有一個抽象類BaseModel.cs,User.cs是用戶實體類,繼承與BaseModel類,是用於類型安全考慮的

1 using System;
2 
3 namespace XU.Model
4 {
5     public abstract class BaseModel
6     {
7     }
8 }
BaseModel.cs
 1 using System;
 2 
 3 namespace XU.Model
 4 {
 5     public class User : BaseModel
 6     {
 7         public string UserName { set; get; }
 8         public string Password { set; get; }
 9     }
10 }
User.cs

XU.FactoryDAL層是用於反射獲取實例,其中只有一個類

 1 using System;
 2 
 3 namespace XU.FactoryDAL
 4 {
 5     public class DataAccess<T> where T : class
 6     {
 7         //獲取配置路徑
 8         private static readonly string path = System.Configuration.ConfigurationManager.AppSettings["DAL"];
 9         private DataAccess() { }
10 
11         /// <summary>
12         /// 創建實例
13         /// </summary>
14         /// <param name="type"></param>
15         /// <returns></returns>
16         public static T CreateDAL(string type)
17         {
18             string className = string.Format(path + ".{0}", type);
19             try
20             {
21                 return (T)System.Reflection.Assembly.Load(path).CreateInstance(className);
22             }
23             catch (Exception ex)
24             {
25                 throw new Exception(ex.Message.ToString());
26             }
27         }
28     }
29 }
DataAccess<T>.cs

XU.IDAL層依賴與XU.Model,其中包含一個基接口IBaseDAL.cs,還有一個用於定義一些基接口中沒有方法的接口IUserDAL,繼承與基接口IBaseDAL<T>

 1 using System;
 2 
 3 namespace XU.IDAL
 4 {
 5     public interface IBaseDAL<T> where T : XU.Model.BaseModel, new()
 6     {
 7         bool Add(T model);
 8         bool Delete(int ID);
 9         bool Update(T model);
10         T GetModel(int ID);
11     }
12 }
IBaseDAL<T>.cs
 1 using System;
 2 
 3 namespace XU.IDAL
 4 {
 5     public interface IUserDAL : IBaseDAL<XU.Model.User>
 6     {
 7         /// <summary>
 8         /// 判斷用戶名是否存在
 9         /// </summary>
10         /// <param name="userName"></param>
11         /// <returns></returns>
12         bool Exists(string userName);
13     }
14 }
IUserDAL.cs

實現XU.IDAL中接口有2個類庫,一個是MSSQL方案,一個是MYSQL方案,這2個類庫都依賴於XU.Model和XU.IDAL,下面是實現方案

XU.MSSQLDAL的實現如下

 1 using System;
 2 
 3 namespace XU.MSSQLDAL
 4 {
 5     public class UserDAL : XU.IDAL.IUserDAL
 6     {
 7         public bool Exists(string userName)
 8         {
 9             return true;
10         }
11 
12         public bool Add(Model.User model)
13         {
14             return true;
15         }
16 
17         public bool Delete(int ID)
18         {
19             return true;
20         }
21 
22         public bool Update(Model.User model)
23         {
24             return true;
25         }
26 
27         public Model.User GetModel(int ID)
28         {
29             return new XU.Model.User() { UserName = "MSSQL", Password = "123456" };
30         }
31     }
32 }
UserDAL.cs

XU.MYSQLDAL的實現如下

 1 using System;
 2 
 3 namespace XU.MYSQLDAL
 4 {
 5     public class UserDAL : XU.IDAL.IUserDAL
 6     {
 7         public bool Exists(string userName)
 8         {
 9             return false;
10         }
11 
12         public bool Add(Model.User model)
13         {
14             return false;
15         }
16 
17         public bool Delete(int ID)
18         {
19             return false;
20         }
21 
22         public bool Update(Model.User model)
23         {
24             return false;
25         }
26 
27         public Model.User GetModel(int ID)
28         {
29             return new XU.Model.User() { UserName = "MYSQL", Password = "123456" };
30         }
31     }
32 }
UserDAL.cs

XU.BLL業務邏輯層中包含了一個用於繼承的基類BaseBLL<T>和用戶業務邏輯UserBLL類,這層依賴XU.IDAL,XU.Model,XU.FactoryDAL庫

 1 using System;
 2 
 3 namespace XU.BLL
 4 {
 5     public class BaseBLL<T> where T : XU.Model.BaseModel, new()
 6     {
 7         protected XU.IDAL.IBaseDAL<T> Dal;
 8         public BaseBLL(string type)
 9         {
10             Dal = XU.FactoryDAL.DataAccess<XU.IDAL.IBaseDAL<T>>.CreateDAL(type);
11         }
12         public virtual bool Add(T model)
13         {
14             return Dal.Add(model);
15         }
16         public virtual bool Delete(int ID)
17         {
18             return Dal.Delete(ID);
19         }
20         public virtual bool Update(T model)
21         {
22             return Dal.Update(model);
23         }
24         public virtual T GetModel(int ID)
25         {
26             return Dal.GetModel(ID);
27         }
28     }
29 }
BaseBLL.cs
 1 using System;
 2 
 3 namespace XU.BLL
 4 {
 5     public class UserBLL : BaseBLL<XU.Model.User>
 6     {
 7         private const string _Type = "UserDAL";
 8         private XU.IDAL.IUserDAL _Dal;
 9 
10         public UserBLL()
11             : base(_Type)
12         {
13             _Dal = base.Dal as XU.IDAL.IUserDAL;
14             if (_Dal == null)
15             {
16                 throw new NullReferenceException(_Type);
17             }
18         }
19 
20         public bool Exists(string userName)
21         {
22             return _Dal.Exists(userName);
23         }
24     }
25 }
UserBLL.cs

XU.ConsoleDemo是一個控制台程序,本准備弄一個網站測試,覺得麻煩,用這個講解抽象工廠更簡單和直觀

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings>
    <!--DAL路徑-->
    <add key="DAL" value="XU.MYSQLDAL"/>
  </appSettings>
</configuration>
App.config
 1 using System;
 2 
 3 namespace XU.ConsoleDemo
 4 {
 5     class Program
 6     {
 7         static void Main(string[] args)
 8         {
 9             XU.BLL.UserBLL userBLL = new XU.BLL.UserBLL();
10             XU.Model.User user=new XU.Model.User();
11             int id = 1;
12             Console.WriteLine("添加------{0}", userBLL.Add(user));
13             Console.WriteLine("刪除------{0}", userBLL.Delete(id));
14             Console.WriteLine("更新------{0}", userBLL.Update(user));
15             XU.Model.User model = userBLL.GetModel(id);
16             Console.WriteLine("查詢\n用戶名:{0}\n密碼:{1}", model.UserName, model.Password);
17             Console.ReadLine();
18         }
19     }
20 }
Program.cs

注意:XU.ConsoleDemo是不會直接引用XU.MSSQLDAL和XU.MYSQLDAL的,但是XU.ConsoleDemo中的Debug目錄下要把編譯好的XU.MSSQLDAL.dll和XU.MYSQLDAL.dll放進去,如果是網站就要放入網站中的Bin文件夾下

 

 

以上就完成了抽象工廠的實現,是不是很簡單,大家相互交流學習,如想討論,請加群.NET/C#/Web開發(1)83455635

例子下載請猛擊這里

如發現哪里不足,請留言,謝謝!

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM