1. 上下文概述

1 /*** 2 * author:深度訓練 3 * blog:http://wangqingpei557.blog.51cto.com/ 4 * **/ 5 using System; 6 using System.Collections.Generic; 7 using System.Text; 8 using System.Reflection; 9 10 namespace ConsoleApplication1.BLL 11 { 12 [ContextModule.ContextEveningBound(IsEvening = true)] 13 public class BLL_Order : ContextModule.ContextModuleBaseObject<BLL_Order> 14 { 15 DAL.DAL_Order dal_order = new DAL.DAL_Order(); 16 17 [ContextModule.ContextExceptionHandler(OperationSort = 1)] 18 public Model.Model_Order InsertOrderSingle(Model.Model_Order ordermodel) 19 { 20 return ContextModule.ContextAction.PostMethod<DAL.DAL_Order, Model.Model_Order>( 21 dal_order, dal_order.GetMethodInfo("InsertOrderSingle"), ordermodel); 22 } 23 [ContextModule.ContextExceptionHandler(OperationSort = 1)] 24 public void SendOrder(Model.Model_Order ordermodel) 25 { 26 ContextModule.ContextAction.PostMethod<DAL.DAL_Order, object>( 27 dal_order, dal_order.GetMethodInfo("SendOrder"), ordermodel); 28 } 29 } 30 }
DAL的執行代碼:

1 /*** 2 * author:深度訓練 3 * blog:http://wangqingpei557.blog.51cto.com/ 4 * **/ 5 using System; 6 using System.Collections.Generic; 7 using System.Text; 8 9 namespace ConsoleApplication1.DAL 10 { 11 [ContextModule.ContextEveningBound(IsEvening = true)] 12 public class DAL_Order : ContextModule.ContextModuleBaseObject<DAL_Order> 13 { 14 [ContextModule.ContextLogHandler(OperationSort = 1)] 15 [ContextModule.ContextSecurityHanlder(OperationSort = 2)] 16 public Model.Model_Order InsertOrderSingle(Model.Model_Order ordermodel) 17 { 18 return new Model.Model_Order() { OrderGuid = Guid.NewGuid(), OrderTime = DateTime.Now }; 19 } 20 [ContextModule.ContextLogHandler(OperationSort = 1)] 21 public void SendOrder(Model.Model_Order ordermodel) 22 { 23 Console.WriteLine("訂單發送成功!"); 24 } 25 } 26 }
上述代碼是我模擬一個上下文的執行過程。
在每個獨立的上下文環境中應該有一片共享的數據存儲區域,以備多個上下文對象訪問。這種方便性多半存在於項目比較緊張的修改需求的時候或者加新業務的時候擴展方法用的。BLL調用代碼:

1 [ContextModule.ContextExceptionHandler(OperationSort = 1)] 2 public void UpdateOrderSingle() 3 { 4 Model.Model_Order ordermodel = new Model.Model_Order() { OrderGuid = Guid.NewGuid(), OrderTime = DateTime.Now }; 5 //放入上下文共享對象池 6 ContextModule.ContextRuntime.CurrentContextRuntime.SetValue("updateorder", ordermodel); 7 ContextModule.ContextAction.PostMethod<DAL.DAL_Order, object>( 8 dal_order, dal_order.GetMethodInfo("UpdateOrderSingle"), null); 9 }
DAL執行代碼:

1 [ContextModule.ContextLogHandler(OperationSort = 1)] 2 public void UpdateOrderSingle() 3 { 4 Model.Model_Order ordermodel = 5 ContextModule.ContextRuntime.CurrentContextRuntime.GetValue("updateorder") as Model.Model_Order; 6 }
對於上下文運行時環境的構建需要考慮到運行時是共享的上下文對象。對於納入上下文管理的所有對象都需要共享或者說是受控於上下文運行時。
上下文構建:

1 /*** 2 * author:深度訓練 3 * blog:http://wangqingpei557.blog.51cto.com/ 4 * **/ 5 using System; 6 using System.Collections.Generic; 7 using System.Text; 8 9 namespace ContextModule 10 { 11 /// <summary> 12 /// 上下文運行時環境。 13 /// 上下文邏輯運行時環境,環境中的功能都是可以通過附加進來的。 14 /// </summary> 15 public class ContextRuntime : IDisposable 16 { 17 #region IDisposable成員 18 void IDisposable.Dispose() 19 { 20 _currentContextRuntime = null; 21 } 22 #endregion 23 24 protected ContextRuntime() { } 25 private DateTime _initTime = DateTime.Now; 26 /// <summary> 27 /// 獲取運行時創建上下文的時間 28 /// </summary> 29 public virtual DateTime InitTime { get { return _initTime; } } 30 private Dictionary<object, object> _runTimeResource = new Dictionary<object, object>(); 31 private ContextFilterHandlerMap _filterMap = new ContextFilterHandlerMap(); 32 /// <summary> 33 /// 獲取上下文中的方法、類過濾器映射表 34 /// </summary> 35 public ContextFilterHandlerMap FilterMap { get { return _filterMap; } } 36 private Guid _initPrimaryKey = Guid.NewGuid(); 37 /// <summary> 38 /// 獲取運行時創建上下文的唯一標識 39 /// </summary> 40 public virtual Guid InitPrimaryKey { get { return _initPrimaryKey; } } 41 /// <summary> 42 /// 獲取上下文共享區域中的數據 43 /// </summary> 44 /// <param name="key">數據Key</param> 45 /// <returns>object數據對象</returns> 46 public virtual object GetValue(object key) 47 { 48 return _runTimeResource[key]; 49 } 50 /// <summary> 51 /// 設置上下文共享區域中的數據 52 /// </summary> 53 /// <param name="key">數據Key</param> 54 /// <param name="value">要設置的數據對象</param> 55 public virtual void SetValue(object key, object value) 56 { 57 _runTimeResource[key] = value; 58 } 59 60 [ThreadStatic] 61 private static ContextRuntime _currentContextRuntime; 62 /// <summary> 63 /// 獲取當前上下文運行時對象. 64 /// </summary> 65 public static ContextRuntime CurrentContextRuntime { get { return _currentContextRuntime; } } 66 /// <summary> 67 /// 開始運行時上下文 68 /// </summary> 69 /// <returns>ContextRuntime</returns> 70 public static ContextRuntime BeginContextRuntime() 71 { 72 //可以通過配置文件配置上下文運行時環境的參數。這里只是實現簡單的模擬。 73 _currentContextRuntime = new ContextRuntime(); 74 return _currentContextRuntime; 75 } 76 } 77 }

1 using (ContextModule.ContextRuntime.BeginContextRuntime()) 2 { 3 4 }
通過Using的方式我們開始上下文生命周期。
上下文對象的綁定需要延后,不能在對象的構建時就創建上下文。
使用后期綁定動態的切入到執行的上下文中。
調用代碼,上下文入口:

1 /*** 2 * author:深度訓練 3 * blog:http://wangqingpei557.blog.51cto.com/ 4 * **/ 5 using System; 6 using System.Collections.Generic; 7 using System.Text; 8 using System.Data; 9 using ConsoleApplication1.BLL; 10 using ConsoleApplication1.Model; 11 12 namespace ConsoleApplication1 13 { 14 public class Program 15 { 16 public static void Main(string[] args) 17 { 18 BLL.BLL_Order order = new BLL.BLL_Order(); 19 using (ContextModule.ContextRuntime.BeginContextRuntime()) 20 { 21 Model.Model_Order ordermodel = new Model_Order() { OrderGuid = Guid.NewGuid(), OrderTime = DateTime.Now }; 22 Model.Model_Order resultmodel = ContextModule.ContextAction.PostMethod<BLL.BLL_Order, Model.Model_Order>(order, order.GetMethodInfo("InsertOrderSingle"), ordermodel); 23 ContextModule.ContextAction.PostMethod<BLL.BLL_Order, object>(order, order.GetMethodInfo("SendOrder"), ordermodel); 24 } 25 26 } 27 } 28 }
6. 上下文在分層架構中的運用
有了上下文的核心原型之后我們可以擴展到分層架構中來,對於分層架構的使用其實很有必要,一般的大型業務系統都是混合的使用模式,可能有C/S、B/S、Mobile終端等等。
對於加入Service層之后BLL、DAL將位於服務之后,對於來自客戶端的調用需要經過一些列的身份驗證及權限授予。有了WCF之后面向SOA的架構開發變的相對容易點,對安全、性能、負載等等都很完美,所以大部分的情況下我們很少需要控制BLL、DAL的執行運行。
那么沒有使用WCF構建分布式的系統時或者是沒有分布式的需求就是直接的調用,如WEB的一般開發,從UI到BLL到DAL。或者是普通的Winfrom的項目、控制台項目屬於內網的使用,可能就需要控制到代碼的執行。
下面我通過演示一個具體的實例來看看到底效果如何。
我以控制台的程序作為演示項目類型,也使用簡單的三層架構。
這個再簡單不過了吧,為了演示越簡單越好,關鍵是突出重點。
需求:
在DAL對象里面加入一個插入Order實體對象的方法:

1 /*** 2 * author:深度訓練 3 * blog:http://wangqingpei557.blog.51cto.com/ 4 * **/ 5 using System; 6 using System.Collections.Generic; 7 using System.Text; 8 9 namespace ConsoleApplication1.DAL 10 { 11 [ContextModule.ContextEveningBound(IsEvening = true)] 12 public class DAL_Order : ContextModule.ContextModuleBaseObject<DAL_Order> 13 { 14 [ContextModule.ContextLogHandler(OperationSort = 1)] 15 [ContextModule.ContextSecurityHanlder(OperationSort = 2)] 16 public Model.Model_Order InsertOrderSingle(Model.Model_Order ordermodel) 17 { 18 return new Model.Model_Order() { OrderGuid = Guid.NewGuid(), OrderTime = DateTime.Now }; 19 } 20 } 21 }

1 /*** 2 * author:深度訓練 3 * blog:http://wangqingpei557.blog.51cto.com/ 4 * **/ 5 using System; 6 using System.Collections.Generic; 7 using System.Text; 8 using System.Reflection; 9 10 namespace ConsoleApplication1.BLL 11 { 12 [ContextModule.ContextEveningBound(IsEvening = true)] 13 public class BLL_Order : ContextModule.ContextModuleBaseObject<BLL_Order> 14 { 15 DAL.DAL_Order dal_order = new DAL.DAL_Order(); 16 17 [ContextModule.ContextExceptionHandler(OperationSort = 1)] 18 public Model.Model_Order InsertOrderSingle(Model.Model_Order ordermodel) 19 { 20 return ContextModule.ContextAction.PostMethod<DAL.DAL_Order, Model.Model_Order>( 21 dal_order, dal_order.GetMethodInfo("InsertOrderSingle"), ordermodel); 22 } 23 } 24 }

1 /*** 2 * author:深度訓練 3 * blog:http://wangqingpei557.blog.51cto.com/ 4 * **/ 5 using System; 6 using System.Collections.Generic; 7 using System.Text; 8 using System.Data; 9 using ConsoleApplication1.BLL; 10 using ConsoleApplication1.Model; 11 12 namespace ConsoleApplication1 13 { 14 public class Program 15 { 16 public static void Main(string[] args) 17 { 18 BLL.BLL_Order order = new BLL.BLL_Order(); 19 //開啟上下文 20 using (ContextModule.ContextRuntime.BeginContextRuntime()) 21 { 22 Model.Model_Order ordermodel = new Model_Order() { OrderGuid = Guid.NewGuid(), OrderTime = DateTime.Now }; 23 24 Model.Model_Order resultmodel = 25 ContextModule.ContextAction.PostMethod<BLL.BLL_Order, Model.Model_Order>( 26 order, order.GetMethodInfo("InsertOrderSingle"), ordermodel); 27 } 28 29 } 30 } 31 }
執行效果:
會先執行日志的記錄,然后要求我們輸入用戶憑證才能繼續執行下面的方法。