The Labs \ Source Viewer \ SSCLI \ System.ComponentModel.Design \ DesignerTransaction

  1. //------------------------------------------------------------------------------
  2. // <copyright file="DesignerTransaction.cs" company="Microsoft">
  3. //
  4. // Copyright (c) 2006 Microsoft Corporation. All rights reserved.
  5. //
  6. // The use and distribution terms for this software are contained in the file
  7. // named license.txt, which can be found in the root of this distribution.
  8. // By using this software in any fashion, you are agreeing to be bound by the
  9. // terms of this license.
  10. //
  11. // You must not remove this notice, or any other, from this software.
  12. //
  13. // </copyright>
  14. //------------------------------------------------------------------------------
  15. /*
  16. */
  17. namespace System.ComponentModel.Design
  18. {
  19.     using System;
  20.     using System.Security.Permissions;
  21.    
  22.     /// <devdoc>
  23.     /// Identifies a transaction within a designer. Transactions are
  24.     /// used to wrap serveral changes into one unit of work, which
  25.     /// helps performance.
  26.     /// </devdoc>
  27.     [HostProtection(SharedState = true)]
  28.     [System.Security.Permissions.PermissionSetAttribute(System.Security.Permissions.SecurityAction.InheritanceDemand, Name = "FullTrust")]
  29.     public abstract class DesignerTransaction : IDisposable
  30.     {
  31.         private bool committed = false;
  32.         private bool canceled = false;
  33.         private bool suppressedFinalization = false;
  34.         private string desc;
  35.        
  36.         /// <devdoc>
  37.         /// <para>[To be supplied.]</para>
  38.         /// </devdoc>
  39.         protected DesignerTransaction() : this("")
  40.         {
  41.         }
  42.        
  43.        
  44.         /// <devdoc>
  45.         /// <para>[To be supplied.]</para>
  46.         /// </devdoc>
  47.         protected DesignerTransaction(string description)
  48.         {
  49.             this.desc = description;
  50.         }
  51.        
  52.        
  53.         /// <devdoc>
  54.         /// <para>[To be supplied.]</para>
  55.         /// </devdoc>
  56.         public bool Canceled {
  57.             get { return canceled; }
  58.         }
  59.        
  60.         /// <devdoc>
  61.         /// <para>[To be supplied.]</para>
  62.         /// </devdoc>
  63.         public bool Committed {
  64.             get { return committed; }
  65.         }
  66.        
  67.         /// <devdoc>
  68.         /// <para>[To be supplied.]</para>
  69.         /// </devdoc>
  70.         public string Description {
  71.             get { return desc; }
  72.         }
  73.        
  74.         /// <devdoc>
  75.         /// <para>[To be supplied.]</para>
  76.         /// </devdoc>
  77.         public void Cancel()
  78.         {
  79.             if (!canceled && !committed) {
  80.                 canceled = true;
  81.                 GC.SuppressFinalize(this);
  82.                 suppressedFinalization = true;
  83.                 OnCancel();
  84.             }
  85.         }
  86.        
  87.         /// <devdoc>
  88.         /// Commits this transaction. Once a transaction has
  89.         /// been committed, further calls to this method
  90.         /// will do nothing. You should always call this
  91.         /// method after creating a transaction to ensure
  92.         /// that the transaction is closed properly.
  93.         /// </devdoc>
  94.         public void Commit()
  95.         {
  96.             if (!committed && !canceled) {
  97.                 committed = true;
  98.                 GC.SuppressFinalize(this);
  99.                 suppressedFinalization = true;
  100.                 OnCommit();
  101.             }
  102.         }
  103.        
  104.         /// <devdoc>
  105.         /// User code should implement this method to perform
  106.         /// the actual work of committing a transaction.
  107.         /// </devdoc>
  108.         protected abstract void OnCancel();
  109.        
  110.         /// <devdoc>
  111.         /// User code should implement this method to perform
  112.         /// the actual work of committing a transaction.
  113.         /// </devdoc>
  114.         protected abstract void OnCommit();
  115.        
  116.         /// <devdoc>
  117.         /// Overrides Object to commit this transaction
  118.         /// in case the user forgot.
  119.         /// </devdoc>
  120.         ~DesignerTransaction()
  121.         {
  122.             Dispose(false);
  123.         }
  124.        
  125.         /// <internalonly/>
  126.         /// <devdoc>
  127.         /// Private implementation of IDisaposable.
  128.         /// When a transaction is disposed it is
  129.         /// committed.
  130.         /// </devdoc>
  131.         void IDisposable.Dispose()
  132.         {
  133.             Dispose(true);
  134.            
  135.             // note - Dispose calls Cancel which sets this bit, so
  136.             // this should never be hit.
  137.             //
  138.             if (!suppressedFinalization) {
  139.                 System.Diagnostics.Debug.Fail("Invalid state. Dispose(true) should have called cancel which does the SuppressFinalize");
  140.                 GC.SuppressFinalize(this);
  141.             }
  142.         }
  143.         protected virtual void Dispose(bool disposing)
  144.         {
  145.             System.Diagnostics.Debug.Assert(disposing, "Designer transaction garbage collected, unable to cancel, please Cancel, Close, or Dispose your transaction.");
  146.             System.Diagnostics.Debug.Assert(disposing || canceled || committed, "Disposing DesignerTransaction that has not been comitted or canceled; forcing Cancel");
  147.             Cancel();
  148.         }
  149.     }
  150. }

Developer Fusion