Click here to Skip to main content
Click here to Skip to main content
Articles » Database » Database » Utilities » Downloads
 
Add your own
alternative version

DataTable Synchronization Manager

, 4 Mar 2006
Adds synchronization to the DataTable Transaction Logger.
/*
Copyright (c) 2006, Marc Clifton
All rights reserved.

Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright notice, this list
  of conditions and the following disclaimer. 

* Redistributions in binary form must reproduce the above copyright notice, this 
  list of conditions and the following disclaimer in the documentation and/or other
  materials provided with the distribution. 
 
* Neither the name Marc Clifton nor the names of contributors may be
  used to endorse or promote products derived from this software without specific
  prior written permission. 

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

*/

// DataTable.PrimaryKey

using System;
using System.Collections.Generic;
using System.Data;

namespace Clifton.Data
{
	public class DataTableSynchronizationManagerException : ApplicationException
	{
		public DataTableSynchronizationManagerException(string msg)
			: base(msg)
		{
		}
	}

	public class DataTableSynchronizationManager
	{
		protected DataTableTransactionLog logger;

		public DataTableTransactionLog Logger
		{
			get { return logger; }
			set { logger = value; }
		}

		public DataTableSynchronizationManager()
		{
		}

		public DataTableSynchronizationManager(DataTableTransactionLog logger)
		{
			this.logger = logger;
		}

		public List<TransactionRecordPacket> GetTransactions()
		{
			if (logger.SourceTable.PrimaryKey == null)
			{
				throw new DataTableTransactionException("GetTransactions requires at least one PK.");
			}

			List<TransactionRecordPacket> packets = new List<TransactionRecordPacket>();

			foreach (DataTableTransactionRecord record in logger.Log)
			{
				TransactionRecordPacket trp = new TransactionRecordPacket(record);
				packets.Add(trp);
			}

			return packets;
		}

		public void AddTransactions(List<TransactionRecordPacket> transactionPackets)
		{
			foreach (TransactionRecordPacket trp in transactionPackets)
			{
				logger.Log.Add(new DataTablePKTransactionRecord(trp));
			}
		}

		/// <summary>
		/// Synchronizes, by applying, the records in the transaction log.  This method assumes
		/// that rows will be identified by their primary keys.  Adding a new row does not fix up
		/// forward references, as does DataTableTransactionLog.Apply, because it is assumed that
		/// a new row transaction is going to be seen here for the first time (rather than undoing
		/// a delete, for example).
		/// </summary>
		public void Sync()
		{
			SetupSort();
			logger.SuspendLogging();

			foreach (DataTableTransactionRecord record in logger.Log)
			{
				if (!(record is DataTablePKTransactionRecord))
				{
					throw new DataTableSynchronizationManagerException("Expected a record of type DataTablePKTransactionRecord.");
				}

				record.Apply(logger.DataView);
			}

			logger.ResumeLogging();
		}

		/// <summary>
		/// Sets up the Sort property for the log's internal DataView.  This method need only
		/// be used when synchronizing a DataTable with TransactionRecordPacket records.
		/// </summary>
		protected void SetupSort()
		{
			if (logger.SourceTable.PrimaryKey == null)
			{
				throw new DataTableTransactionException("GetTransactions requires at least one PK.");
			}

			string sortBy = String.Empty;
			string comma = String.Empty;

			foreach (DataColumn dc in logger.SourceTable.PrimaryKey)
			{
				sortBy += comma + dc.ColumnName;
				comma = ", ";
			}

			logger.DataView.Sort = sortBy;
		}
	}
}

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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

Marc Clifton

United States United States
Marc is the creator of two open source projects, MyXaml, a declarative (XML) instantiation engine and the Advanced Unit Testing framework, and Interacx, a commercial n-tier RAD application suite.  Visit his website, www.marcclifton.com, where you will find many of his articles and his blog.
 
Marc lives in Philmont, NY.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150129.1 | Last Updated 4 Mar 2006
Article Copyright 2006 by Marc Clifton
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid