日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 >

把事务封装成类似Serializable用法的特性

發(fā)布時間:2025/5/22 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 把事务封装成类似Serializable用法的特性 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

? 最近幾天上班沒事可做就想著整理常用的類庫方法,驗證、消息、分頁、模版引擎、數(shù)據(jù)庫操作、ini操作、文本操作、xml操作等,最后就是現(xiàn)在這個事務(wù)特性。

? ?

1 /// <summary> 2 /// 事務(wù)形象屬性的接口 3 /// </summary> 4 internal class TransactionAspectProperty : IContextProperty, IContributeObjectSink 5 { 6 //凍結(jié)策略 7 public void Freeze(Context newContext) 8 { 9 } 10 //獲取事務(wù)接口容器 11 public IMessageSink GetObjectSink(MarshalByRefObject obj, IMessageSink nextSink) 12 { 13 return new TransactionAspect((BaseBusinessComponent) obj, nextSink); 14 } 15 //判斷是否存在策略 16 public bool IsNewContextOK(Context newCtx) 17 { 18 return true; 19 } 20 //獲取特性名稱 21 public string Name 22 { 23 get 24 { 25 return "TransactionAspectProperty"; 26 } 27 } 28 //獲取Remoting特性名稱 29 string System.Runtime.Remoting.Contexts.IContextProperty.Name 30 { 31 get 32 { 33 return "TransactionAspectProperty"; 34 } 35 } 36 }

Freeze、IsNewContextOK、Name是IContextProperty 成員?GetObjectSink是IContributeObjectSink 成員

TransactionAspectProperty繼承于IContextProperty、IContributeObjectSink,用于返回權(quán)限驗證消息接收器的實例TransactionAspect

1 //事務(wù)的特性 2 internal sealed class TransactionAspectAttribute : ContextAttribute 3 { 4 public TransactionAspectAttribute() : base("TransactionAspect") 5 { 6 } 7 /// <summary> 8 /// 從寫父類的特性 9 /// </summary> 10 /// <param name="ccm"></param> 11 public override void GetPropertiesForNewContext(IConstructionCallMessage ccm) 12 { 13 if (ccm != null) 14 { 15 ccm.ContextProperties.Add(new TransactionAspectProperty()); 16 } 17 } 18 }

TransactionAspectAttribute繼承ContextAttribute的特性

1 internal class TransactionAspect : IMessageSink 2 { 3 private BaseBusinessComponent _biz;//抽象類 4 private IMessageSink _nextSink;//消息管理 5 6 public TransactionAspect(BaseBusinessComponent biz, IMessageSink nextSink) 7 { 8 this._biz = biz; 9 this._nextSink = nextSink; 10 } 11 12 13 /// <summary> 14 /// 異步處理給定的消息。 15 /// </summary> 16 /// <param name="msg">要處理的消息。</param> 17 /// <param name="replySink"> 答復(fù)消息的答復(fù)接收器。</param> 18 /// <returns>返回 System.Runtime.Remoting.Messaging.IMessageCtrl 接口,該接口提供一種在調(diào)度異步消息之后控制這些消息的方法。</returns> 19 public IMessageCtrl AsyncProcessMessage(IMessage msg, IMessageSink replySink) 20 { 21 return null; 22 } 23 /// <summary> 24 /// 檢查是否存在事務(wù) 25 /// </summary> 26 /// <param name="m"></param> 27 /// <returns></returns> 28 private static bool ExistsEnableCommit(MethodBase m) 29 { 30 object[] attributes = m.GetCustomAttributes(true); 31 object[] VB_t_array_L0 = attributes; 32 for (int VB_t_i4_L0 = 0; VB_t_i4_L0 < VB_t_array_L0.Length; VB_t_i4_L0++) 33 { 34 if (RuntimeHelpers.GetObjectValue(VB_t_array_L0[VB_t_i4_L0]) is EnableCommitAttribute) 35 { 36 return true; 37 } 38 } 39 return false; 40 } 41 /// <summary> 42 /// 消息管理接口 43 /// </summary> 44 /// <param name="msg"></param> 45 /// <returns></returns> 46 public IMessage SyncProcessMessage(IMessage msg) 47 { 48 IMessage returnMessage;//通信信息 49 IMethodMessage methodMesssage = (IMethodMessage) msg;//方法的消息 50 this._biz.Rollback = false; 51 if (ExistsEnableCommit(methodMesssage.MethodBase))//判斷是否授權(quán)的事務(wù) 52 { 53 TimeSpan VB_t_struct_S0 = new TimeSpan(0L);//間隔 54 using (TransactionScope scope = new TransactionScope(TransactionScopeOption.RequiresNew, VB_t_struct_S0))//事務(wù)模式 55 { 56 returnMessage = this._nextSink.SyncProcessMessage(msg); 57 Exception resultCommitException = ((IMethodReturnMessage) returnMessage).Exception;//錯誤信息 58 if (resultCommitException != null) 59 { 60 throw resultCommitException; 61 } 62 if (!this._biz.Rollback) 63 { 64 scope.Complete();//提交事務(wù) 65 } 66 } 67 return returnMessage; 68 } 69 returnMessage = this._nextSink.SyncProcessMessage(msg);//返回同步消息 70 Exception resultException = ((IMethodReturnMessage) returnMessage).Exception;//錯誤的信息 71 if (resultException != null) 72 { 73 throw resultException; 74 } 75 return returnMessage; 76 } 77 /// <summary> 78 /// 獲取接收器鏈中的下一個消息接收器。 79 /// </summary> 80 public IMessageSink NextSink 81 { 82 get 83 { 84 return this._nextSink; 85 } 86 } 87 //System.Runtime.Remoting.Messaging.IMessageSink.NextSink 88 IMessageSink System.Runtime.Remoting.Messaging.IMessageSink.NextSink 89 { 90 get 91 { 92 return this._nextSink; 93 } 94 } 95 }

TransactionAspect繼承IMessageSink處理事務(wù)消息這個是關(guān)鍵部分 。于通過反射取得業(yè)務(wù)層方法上所有的驗證權(quán)限屬性EnableCommitAttribute,并逐個進行驗證。

1 [TransactionAspect]//定義事務(wù)提交的抽象類 2 public abstract class BaseBusinessComponent : ContextBoundObject 3 { 4 private bool _rollback; 5 6 protected BaseBusinessComponent() 7 { 8 } 9 //執(zhí)行的狀態(tài) 是否回滾 10 public bool Rollback 11 { 12 get 13 { 14 return this._rollback; 15 } 16 set 17 { 18 this._rollback = value; 19 } 20 } 21 }

定義BaseBusinessComponent抽象類 ,用于具體實現(xiàn)的業(yè)務(wù)類繼承實現(xiàn)

[AttributeUsage(AttributeTargets.Method)]public sealed class EnableCommitAttribute : Attribute{}

EnableCommitAttribute繼承Attribute特性,驗證調(diào)用方法所需要的權(quán)限,具體的驗證規(guī)則與用戶信息由繼承類編寫。

調(diào)用處:

1 namespace demo 2 { 3 public class hous : BaseBusinessComponent//繼承這個抽象基類 4 { 5 6 [EnableCommit]//加上這個就啟動事務(wù)特性 7 public bool add(string w) 8 { 9 10 bool bl = false; 11 //過濾器 12 13 //業(yè)務(wù)邏輯部分省略 14 15 if (bl) 16 { 17 this.Rollback = true;//表示不提交事務(wù),回滾 18 return false; 19 } 20 21 return true; 22 23 } 24 25 } 26 }

到此就實現(xiàn)了 類似 web.Match 與?Serializable 一樣的特性用法只要在普通方法上加上?[EnableCommit]就實現(xiàn)了事務(wù) 不要事務(wù)去除即可,講的不好請見諒。

轉(zhuǎn)載于:https://www.cnblogs.com/jxluowei/p/3286056.html

總結(jié)

以上是生活随笔為你收集整理的把事务封装成类似Serializable用法的特性的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。