⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 transactionalenlistment.cs

📁 C#高级编程第6版随书源代码 值得下载
💻 CS
字号:
using System;
using System.Diagnostics;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Transactions;

namespace Wrox.ProCSharp.Transactions
{
   public partial class Transactional<T>
   {
      internal class ResourceManager<T1> : IEnlistmentNotification
      {
         private Transactional<T1> parent;
         private Transaction currentTransaction;
         //private T1 tempValue;

         //public T1 Value
         //{
         //    get { return tempValue; }
         //    set { tempValue = value; }
         //}

         public T1 Value { get; set; }

         internal ResourceManager(Transactional<T1> parent, Transaction tx)
         {
            this.parent = parent;
            Value = DeepCopy(parent.liveValue);
            currentTransaction = tx;
         }

         static ResourceManager()
         {
            Type t = typeof(T1);
            Trace.Assert(t.IsSerializable, "Type " + t.Name + " is not serializable");
         }

         private T1 DeepCopy(T1 value)
         {
            using (MemoryStream stream = new MemoryStream())
            {
               BinaryFormatter formatter = new BinaryFormatter();
               formatter.Serialize(stream, value);
               stream.Flush();
               stream.Seek(0, SeekOrigin.Begin);

               return (T1)formatter.Deserialize(stream);
            }
         }





         #region IEnlistmentNotification Members

         public void Commit(Enlistment enlistment)
         {
            parent.Commit(Value, currentTransaction);
            enlistment.Done();
         }

         public void InDoubt(Enlistment enlistment)
         {
            throw new Exception("The method or operation is not implemented.");
         }

         public void Prepare(PreparingEnlistment preparingEnlistment)
         {
            preparingEnlistment.Prepared();
         }

         public void Rollback(Enlistment enlistment)
         {
            parent.Rollback(currentTransaction);
            enlistment.Done();
         }

         #endregion
      }
   }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -