LinqHelper Refactor Idea

Purpose

The purpose of this post is to provide a simple refactoring of Mike Hunter’s most excellent LinqHelper.

Please see the code below.

Please see the orginal post by Mike, linked below.

Code

using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime;
using System.Runtime.Serialization;
using System.Text;
using System.Xml;
using Team;
using Team.Framework;
using Team.Framework.Common;
using Team.Framework.Common.Linq;

namespace Team.Framework.Common.Linq
{
	/// <summary>
	/// This is a utility for using L2s-based objects.
	/// </summary>
	/// <remarks>
	/// This code was borrowed '2009-Aug-11-Tue 08:38:14.1223174 AM -04:00' and it should not be modified manually.
	/// For details, see...
	/// http://complexitykills.blogspot.com/2008/03/disconnected-linq-to-sql-tips-part-1.html 
	/// </remarks>
	public static class LINQHelper
	{
		/// <summary> 
		/// Makes a copy of an existing LINQ to SQL entity and it's children. 
		/// </summary> 
		/// <typeparam name="T"></typeparam> 
		/// <param name="entitySource">The LINQ to SQL entity to copy</param> 
		/// <returns></returns> 
		public static T CopyEntityDeep<T>(T entitySource)
		{
			if (entitySource == null)
				return default(T);
			return (T)DeserializeEntity(SerializeEntity(entitySource), entitySource.GetType());
		}

		/// <summary> 
		/// Makes a copy of a list of existing LINQ to SQL entities and their children. 
		/// </summary> 
		/// <typeparam name="T"></typeparam> 
		/// <param name="source">The LIST of SQL entities to copy 
		/// </param> 
		/// <returns></returns> 
		public static List<T> CopyEntityListDeep<T>(List<T> entitySourceList)
		{
			List<T> result = new List<T>();
			if (entitySourceList == null) return null;
			foreach (T entitySource in entitySourceList)
			{
				T entityTarget = CopyEntityDeep(entitySource);
				result.Add(entityTarget);
			}
			return result;
		}

		public static string SerializeEntity<T>(T entitySource)
		{
			DataContractSerializer dcs = new DataContractSerializer(entitySource.GetType());
			if (entitySource == null) return null; StringBuilder sb = new StringBuilder(); XmlWriter xmlw = XmlWriter.Create(sb); dcs.WriteObject(xmlw, entitySource); xmlw.Close();
			return sb.ToString();
		}

		/// <summary>
		/// This will serialize and optionally convert to "utf-8" format.
		/// </summary>
		/// <typeparam name="T">This is the type.</typeparam>
		/// <param name="entitySource">This is the object to use.</param>
		/// <param name="convertToUtf8">This is the flag for utf conversion.</param>
		/// <returns>This is XML.</returns>
		/// <remarks>
		/// NOTE. 20091202. This was added because the standard call returns "utf-16", which sometimes does not load into VS.NET and IE8.
		/// </remarks>
		public static string SerializeEntity<T>(T entitySource, bool convertToUtf8)
		{
			string myString = "";

			myString = (LINQHelper.SerializeEntity(entitySource) + "").Trim();

			if (convertToUtf8)
			{
				myString = myString.Replace("utf-16", "utf-8");
			}

			return myString;
		}

		public static object DeserializeEntity(string entitySource, Type entityType)
		{
			object entityTarget;
			if (entityType == null) return null;
			DataContractSerializer dcs = new DataContractSerializer(entityType);
			StringReader sr = new StringReader(entitySource); XmlTextReader xmltr = new XmlTextReader(sr); entityTarget = (object)dcs.ReadObject(xmltr); xmltr.Close();
			return entityTarget;
		}
	}
}

HTH.

Thank you.

— Mark Kamoski

Advertisements

Author: mkamoski1

n/a