Click here to Skip to main content
15,891,033 members
Articles / Programming Languages / C#

Optimizing Serialization in .NET - part 2

Rate me:
Please Sign up or sign in to vote.
4.95/5 (34 votes)
27 Nov 2006Public Domain33 min read 328.1K   3.7K   156  
Provides code and techniques to enable developers to optimize serialization of DataSets/DataTables.
using System;
using System.Data;
using System.Runtime.Serialization;

namespace SimmoTech.Utils.Data
{

	/// <summary>
	/// Takes a DataSet and 'wraps' it so that it can be passed via Remoting
	/// using Fast serialization.
	/// </summary>
	[Serializable]
	public class WrappedDataSet: ISerializable
	{
		#region Casting Operators
		public static implicit operator DataSet (WrappedDataSet wrappedDataSet)
		{
			return wrappedDataSet.DataSet;
		}
		
		public static implicit operator WrappedDataSet (DataSet dataSet)
		{
			return new WrappedDataSet(dataSet);
		}
		#endregion Casting Operators
		
		#region Constructors
		public WrappedDataSet(DataSet dataSet)
		{
			if (dataSet == null) throw new ArgumentNullException("dataSet");
			this.dataSet = dataSet;
		}
		#endregion Constructors
		
		#region Properties
		public DataSet DataSet {
			get { return dataSet; }
		} DataSet dataSet;
		#endregion Properties
		
		#region ISerializable Members
		protected WrappedDataSet(SerializationInfo info, StreamingContext context)
		{
			dataSet = AdoNetHelper.DeserializeDataSet((byte[]) info.GetValue("_", typeof(byte[])));
		}

		public void GetObjectData(SerializationInfo info, StreamingContext context)
		{
			info.AddValue("_", AdoNetHelper.SerializeDataSet(dataSet));
		}
		#endregion
	}

}

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 A Public Domain dedication


Written By
Software Developer (Senior) Hunton Information Systems Ltd.
United Kingdom United Kingdom
Simon Hewitt is a freelance IT consultant and is MD of Hunton Information Systems Ltd.

He is currently looking for contract work in London.

He is happily married to Karen (originally from Florida, US), has a lovely daughter Bailey, and they live in Kings Langley, Hertfordshire, UK.

Comments and Discussions