Click here to Skip to main content
15,881,882 members
Articles / Programming Languages / C# 4.0

Relationship Oriented Programming - The IDE plus More on Agile Project Management

Rate me:
Please Sign up or sign in to vote.
4.98/5 (25 votes)
12 Mar 2012CPOL81 min read 77.4K   1.2K   49  
An Integrated Development Environment (IDE) for the Relationship Oriented Programming Tool.
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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect Interacx
United States United States
Blog: https://marcclifton.wordpress.com/
Home Page: http://www.marcclifton.com
Research: http://www.higherorderprogramming.com/
GitHub: https://github.com/cliftonm

All my life I have been passionate about architecture / software design, as this is the cornerstone to a maintainable and extensible application. As such, I have enjoyed exploring some crazy ideas and discovering that they are not so crazy after all. I also love writing about my ideas and seeing the community response. As a consultant, I've enjoyed working in a wide range of industries such as aerospace, boatyard management, remote sensing, emergency services / data management, and casino operations. I've done a variety of pro-bono work non-profit organizations related to nature conservancy, drug recovery and women's health.

Comments and Discussions