using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; namespace Clifton.Data { /// <summary> /// Creates a deep copy of the source DataTable and tracks changes to the table with an /// internal transaction manager. On a call to UpdateSource, the transactions are posted /// to the source synchronization manager and the source table is updated. Care should be /// taken that this sandbox is used for manageable DataTable sizes. /// </summary> public class TableTransactionSandbox : ISupportInitialize { protected object tag; protected DataTable sandboxTable; protected DataTableTransactionLog sourceLogger; protected DataTableTransactionLog sandboxLogger; protected DataTableSynchronizationManager sourceSyncMgr; protected DataTableSynchronizationManager sandboxSyncMgr; /// <summary> /// Gets/sets tag /// </summary> public object Tag { get { return tag; } set { tag = value; } } /// <summary> /// Gets/sets sandboxTable /// </summary> public DataTable SandboxTable { get { return sandboxTable; } } /// <summary> /// Gets/sets sourceTable /// </summary> public DataTableTransactionLog SourceLogger { get { return sourceLogger; } set { sourceLogger = value; } } /// <summary> /// Constructor. /// </summary> public TableTransactionSandbox() { } public TableTransactionSandbox(DataTableTransactionLog log) { sourceLogger = log; Initialize(); } /// <summary> /// Does nothing. /// </summary> public virtual void BeginInit() { } /// <summary> /// If instantiated declaratively, initializes the internal objects. /// </summary> public virtual void EndInit() { Initialize(); } /// <summary> /// Clones the DataTable encapsulated by the source logger and initializes the internal /// logger and synchronization managers. /// </summary> public void Initialize() { if (sourceLogger == null) { throw new DataTableTransactionException("SourceLogger must be initialized."); } // Create a DataView of the source table. DataView dvSource = new DataView(sourceLogger.SourceTable); // Use the new .NET 2.0 ToTable method to make a copy. sandboxTable = dvSource.ToTable(); // Initialize a primary key array. DataColumn[] pkCols = new DataColumn[sourceLogger.SourceTable.PrimaryKey.Length]; // Copy the primary keys to the cloned table (which is stupid, that the // ToTable method doesn't do this). for (int i=0; i<pkCols.Length; i++) { pkCols[i] = sandboxTable.Columns[sourceLogger.SourceTable.PrimaryKey[i].ColumnName]; } // Assign the PK's to the sandbox table. sandboxTable.PrimaryKey = pkCols; // Initialize the sandbox logger. sandboxLogger = new DataTableTransactionLog(sandboxTable); // Initialize the synchronization managers. sourceSyncMgr = new DataTableSynchronizationManager(sourceLogger); sandboxSyncMgr = new DataTableSynchronizationManager(sandboxLogger); } /// <summary> /// Call this method to update the source table with all the transactions that have /// taken place in the sandbox. /// </summary> public void UpdateSource() { // Get the transactions. List<TransactionRecordPacket> trpList = sandboxSyncMgr.GetTransactions(); // Add to the source synchronization manager. sourceSyncMgr.AddTransactions(trpList); // Handles additional fields that were added during an add row. sourceSyncMgr.Logger.LogOnlyNewFieldChanges(); // Sync the source. sourceSyncMgr.Sync(); // Clear the sandbox log. sandboxLogger.ClearLog(); } } }
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.
This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)