Skip Navigation Links.

Northwind\Data\EntityBase.cs

D:\Temp3\TestProjects\CSADO.Net\Northwind\Data\EntityBase.cs
/*
 * This file was generated by ProCG version 2.0
 *
 * File name:	Northwind\Data\EntityBase.cs
 * Language:	C# - ADO.Net 
 * Database:	My Sql
 *
 * Copyright (c) 2002-2019 iGenXSoft.
 * For more information visit http://www.igenxsoft.com
 */

using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using Northwind.General;
using System.Runtime.Serialization;
using System.Xml.Serialization;
using System.Web.Script.Serialization;

namespace Northwind.Data
{
	[DataContract]
	[Serializable]
	public class CEntityBase
	{
		static List<Type> m_KnownTypes = null;
		static Type[] m_KnownTypesArray = null;
		protected DataManager m_DataManager;
		protected bool m_DataManagerWasCreatedByMe = false;
		public static List<Type> KnownTypes
		{
			get 
			{
				if (m_KnownTypes == null)
					LoadKnownTypes();

				return CEntityBase.m_KnownTypes; 
			}
			set { CEntityBase.m_KnownTypes = value; }
		}

		public static Type[] KnownTypesArray
		{
			get
			{
				if (m_KnownTypesArray == null)
					m_KnownTypesArray = KnownTypes.ToArray();

				return m_KnownTypesArray;
			}
		}

		//static System.Web.Script.Serialization.JavaScriptSerializer Serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
		//static object serializationLock = new object();

		private static void LoadKnownTypes()
		{
			m_KnownTypes = new List<Type>();
			// use reflection to get all derived types
			foreach (Type t in Assembly.GetAssembly(typeof(CEntityBase)).GetTypes())
			{
				if (typeof(CEntityBase).IsAssignableFrom(t))
				{
					if (!t.Name.EndsWith("Base"))
						m_KnownTypes.Add(t);
				}
			}
			foreach (Type t in Assembly.GetAssembly(typeof(NorthwindCodesManager)).GetTypes())
			{
				if (typeof(CStructData).IsAssignableFrom(t))
				{
					if (!t.Name.EndsWith("Base"))
						m_KnownTypes.Add(t);
				}
			}
			foreach (Type t in Assembly.GetAssembly(typeof(CStructData)).GetTypes())
			{
				if (typeof(CStructData).IsAssignableFrom(t))
				{
					if (!t.Name.EndsWith("Base"))
						m_KnownTypes.Add(t);
				}
			}
		}

		public static string SerializeObject(Object objectToSerialize, SerializationTypeEnum serializationType = SerializationTypeEnum.JSON)
		{
			if (serializationType == SerializationTypeEnum.FORMATTED_STRING)
			{
				throw new Exception(string.Format("SerializationType {0} not supported", serializationType.ToString()));
			}
			return SerializeObjectToJson(objectToSerialize);
		}

		public static object DeserializeObject(string text, Type type, SerializationTypeEnum serializationType = SerializationTypeEnum.JSON)
		{
			if (serializationType == SerializationTypeEnum.FORMATTED_STRING)
			{
				throw new Exception(string.Format("SerializationType {0} not supported", serializationType.ToString()));
			}
			return DeserializeObjectFromJson(text, type);
		}

		// Json serializing and deserializing functions
		protected static string SerializeObjectToJson(Object objectToSerialize)
		{
			System.Runtime.Serialization.Json.DataContractJsonSerializer jSer;
			//lock(m_KnownTypes)
				jSer = new System.Runtime.Serialization.Json.DataContractJsonSerializer(objectToSerialize.GetType(), KnownTypesArray);
			using (MemoryStream ms = new MemoryStream())
			{
				jSer.WriteObject(ms, objectToSerialize);
				byte[] buff = ms.ToArray();
				return Encoding.UTF8.GetString(buff, 0, buff.Length);
			}
		}

		protected static object DeserializeObjectFromJson(string text, Type type)
		{
			System.Runtime.Serialization.Json.DataContractJsonSerializer jSer;
			//lock(m_KnownTypes)
			jSer = new System.Runtime.Serialization.Json.DataContractJsonSerializer(type, KnownTypesArray);
			using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(text)))
			{
				return jSer.ReadObject(ms);
			}
		}

		public void SaveToFile(string fileName, SerializationTypeEnum serializationType = SerializationTypeEnum.JSON)
		{
			if (serializationType == SerializationTypeEnum.FORMATTED_STRING)
			{
				throw new Exception(string.Format("SerializationType {0} not supported", serializationType.ToString()));
			}
			SaveToJsonFile(fileName);
		}

		public static object DeserializeObjectFromFile(string fileName, Type type, SerializationTypeEnum serializationType = SerializationTypeEnum.JSON)
		{
			if (serializationType == SerializationTypeEnum.FORMATTED_STRING)
			{
				throw new Exception(string.Format("SerializationType {0} not supported", serializationType.ToString()));
			}

			return DeserializeObjectFromJsonFile(fileName, type);
		}

		protected void SaveToJsonFile(string fileName)
		{
			string jsonText = SerializeObjectToJson(this);
			System.IO.File.WriteAllText(fileName+".Json", jsonText);
		}

		protected static object DeserializeObjectFromJsonFile(string fileName, Type type)
		{
			string jsonText = System.IO.File.ReadAllText(fileName+".Json");

			return DeserializeObjectFromJson(jsonText, type);
		}

		// need to delete this function but first change all entity
		public DataManager GetDataManager()
		{
			if (m_DataManager == null)
			{
				m_DataManager = new DataManager();
				m_DataManagerWasCreatedByMe = true;
			}
			return m_DataManager;
		}

		[IgnoreDataMember]
		[XmlIgnore]
		[ScriptIgnore]
		public DataManager DataManager
		{
			get
			{
				if (m_DataManager == null)
				{
					m_DataManager = new DataManager();
					m_DataManagerWasCreatedByMe = true;
				}
				return m_DataManager;
			}
			set
			{
				m_DataManager = value;
			}
		}

		public void DisposeDataManager()
		{
			if (m_DataManagerWasCreatedByMe && m_DataManager != null)
			{
				m_DataManager.Dispose();
				m_DataManager = null;
			}
		}

		protected virtual void BeforeSave()
		{
		}

		protected virtual void AfterSave()
		{
		}

	}
}


  //      6024 ProCG uses this line - don't edit it