Click here to Skip to main content
15,894,720 members
Articles / Hosted Services / Azure

Kerosene ORM: a dynamic, configuration-less and self-adaptive ORM for POCO objects supporting a SQL-like syntax from C#

Rate me:
Please Sign up or sign in to vote.
4.96/5 (71 votes)
1 Mar 2015CPOL35 min read 547.8K   4.6K   212  
The seventh version of the dynamic, configuration-less and self-adaptive Kerosene ORM library, that provides full real support for POCO objects, natural SQL-like syntax from C#, and advanced capabilities while being extremely easy to use.
// ======================================================== 
namespace Kerosene.Tools.Test
{
	using System;
	using System.Collections.Generic;
	using System.Runtime.Serialization;

	// ==================================================== 
	public class Test_DeepObject
	{
		public static DeepObject Index_And_Dynamic_Syntax(bool pressEnter = true)
		{
			ConsoleHelper.Header(pressEnter, "\n\n=== {0} ...", MethodHelper.ThisMethod());

			DeepObject obj = new DeepObject(); Console.WriteLine("\n> Object = {0}", obj);
			dynamic d = obj;

			d["Country"] = "UK"; Console.WriteLine("\n> Object = {0}", obj);
			d["Owner", "FirstName"] = "James"; Console.WriteLine("\n> Object = {0}", obj);
			d["Owner", "LastName"] = "Bond";
			d["Wierd Name for a Property"] = new CalendarDate(DateTime.Now);
			d.Address.Street[0] = "Big Avenue, 1";
			d.Address.Street[1] = "Secret Building";
			Console.WriteLine("\n> Object = {0}", obj);

			return obj;
		}

		public static void Dispose_Twice(bool pressEnter = true)
		{
			ConsoleHelper.Header(pressEnter, "\n\n=== {0} ...", MethodHelper.ThisMethod());

			var obj = Index_And_Dynamic_Syntax(pressEnter: false);

			obj.Dispose(); Console.WriteLine("\n> Disposed = {0}", obj);
			obj.Dispose(); Console.WriteLine("\n> Disposed = {0}", obj);
		}

		public static DeepObject Complex_Members(bool pressEnter = true)
		{
			ConsoleHelper.Header(pressEnter, "\n\n=== {0} ...", MethodHelper.ThisMethod());

			dynamic d = Index_And_Dynamic_Syntax(pressEnter: false);
			d.ItemType = typeof(Dictionary<ClockTime, CalendarDate>);
			//d.ItemTypeGeneric = typeof(Dictionary<,>);
			d.MyDict = new Dictionary<int, CalendarDate>();
			d.MyDict.Add(1, new CalendarDate(2001, 1, 1));
			d.MyDict.Add(2, new CalendarDate(2002, 2, 2));
			d.MyDict.Add(3, new CalendarDate(2003, 3, 3));
			Console.WriteLine("\n> Object = {0}", d);

			return d;
		}

		public static void Easy_Cloning(bool pressEnter = true)
		{
			ConsoleHelper.Header(pressEnter, "\n\n=== {0} ...", MethodHelper.ThisMethod());

			var obj = Index_And_Dynamic_Syntax(pressEnter: false); Console.WriteLine("\n> Source = {0}", obj);
			var temp = obj.Clone(); Console.WriteLine("\n> Cloned = {0}", temp);
			if (obj != temp) Console.WriteLine("\n> {0}",
				(new Exception(string.Format("Source '{0}' and cloned '{1}' are not equivalent.", obj, temp)))
				.ExtendedString());
		}

		public static void Easy_Serialization(bool binary, bool pressEnter = true)
		{
			ConsoleHelper.Header(pressEnter, "\n\n=== {0} ({1} Mode) ...", MethodHelper.ThisMethod(), binary ? "Binary" : "Text");

			var obj = Index_And_Dynamic_Syntax(pressEnter: false); Console.WriteLine("\n> Source = {0}", obj);
			var path = "c:\\temp\\data.xml";

			try { SerializationHelper.Serialize(path, obj, binary); }
			catch (SerializationException e)
			{
				if (!binary) { Console.WriteLine("\n- Expected exception: {0}", e.ExtendedString()); return; }
				throw e;
			}

			DeepObject temp = null;
			try { temp = (DeepObject)SerializationHelper.Deserialize(path, binary); Console.WriteLine("\n> Deserialized = {0}", temp); }
			catch (SerializationException e)
			{
				if (!binary) { Console.WriteLine("\n- Expected exception: {0}", e.ExtendedString()); return; }
				throw e;
			}
			if (obj != temp) Console.WriteLine("\n> {0}",
				(new Exception(string.Format("Source '{0}' and deserialized '{1}' are not equivalent.", obj, temp)))
				.ExtendedString());
		}

		public static void Complex_Cloning(bool pressEnter = true)
		{
			ConsoleHelper.Header(pressEnter, "\n\n=== {0} ...", MethodHelper.ThisMethod());

			var obj = Complex_Members(pressEnter: false); Console.WriteLine("\n> Source = {0}", obj);
			var temp = obj.Clone(); Console.WriteLine("\n> Cloned = {0}", temp);
			if (obj != temp) Console.WriteLine("\n> {0}",
				(new Exception(string.Format("Source '{0}' and cloned '{1}' are not equivalent.", obj, temp)))
				.ExtendedString());
		}

		public static void Complex_Serialization(bool binary, bool pressEnter = true)
		{
			ConsoleHelper.Header(pressEnter, "\n\n=== {0} ({1} Mode) ...", MethodHelper.ThisMethod(), binary ? "Binary" : "Text");

			var obj = Complex_Members(pressEnter: false); Console.WriteLine("\n> Source = {0}", obj);
			var path = "c:\\temp\\data.xml";

			try { SerializationHelper.Serialize(path, obj, binary); }
			catch (SerializationException e)
			{
				if (!binary) { Console.WriteLine("\n- Expected exception: {0}", e.ExtendedString()); return; }
				throw e;
			}

			DeepObject temp = null;
			try { temp = (DeepObject)SerializationHelper.Deserialize(path, binary); Console.WriteLine("\n> Deserialized = {0}", temp); }
			catch (SerializationException e)
			{
				if (!binary) { Console.WriteLine("\n- Expected exception: {0}", e.ExtendedString()); return; }
				throw e;
			}
			if (obj != temp) Console.WriteLine("\n> {0}",
				(new Exception(string.Format("Source '{0}' and deserialized '{1}' are not equivalent.", obj, temp)))
				.ExtendedString());
		}

		public static void Execute(bool pressEnter = true)
		{
			ConsoleHelper.Header(pressEnter, "\n\n=== {0} ...", MethodHelper.ThisMethod());

			Index_And_Dynamic_Syntax(pressEnter);
			Dispose_Twice(pressEnter);
			Complex_Members(pressEnter);

			Easy_Cloning(pressEnter);
			Easy_Serialization(true, pressEnter); Easy_Serialization(false, pressEnter);

			Complex_Cloning(pressEnter);
			Complex_Serialization(true, pressEnter); Complex_Serialization(false, pressEnter);
		}
	}
}
// ======================================================== 

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
Spain Spain
mbarbac has worked in start-ups, multinational tech companies, and consulting ones, serving as CIO, CTO, SW Development Director, and Consulting Director, among many other roles.

Solving complex puzzles and getting out of them business value has ever been among his main interests - and that's why he has spent his latest 25 years trying to combine his degree in Theoretical Physics with his MBA... and he is still trying to figure out how all these things can fit together.

Even if flying a lot across many countries, along with the long working days that are customary in IT management and Consultancy, he can say that, after all, he lives in Spain (at least the weekends).

Comments and Discussions