Click here to Skip to main content
15,892,199 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 546.2K   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.
// ======================================================== Repo.cs
namespace Kerosene.ORM.Maps.Lazy.Test
{
	using Kerosene.ORM.ExampleDB;
	using Kerosene.ORM.Maps;
	using Kerosene.Tools;
	using System;
	using System.Linq;
	using System.Text;

	// ==================================================== 
	/// <summary>
	/// Factory class to manage repositories for testing purposes.
	/// </summary>
	public static class Repo
	{
		/// <summary>
		/// Creates a new repository associated with the test database connection.
		/// </summary>
		public static IDataRepository Create()
		{
			var repo = RepositoryFactory.Create(DB.Link);

			new RegionMap((Concrete.DataRepository)repo);
			new CountryMap((Concrete.DataRepository)repo);
			new EmployeeMap((Concrete.DataRepository)repo);
			new TalentMap((Concrete.DataRepository)repo);
			new EmployeeTalentMap((Concrete.DataRepository)repo);

			return repo;
		}

		/// <summary>
		/// Gets a shared repository instance fort testing purposes.
		/// </summary>
		public static IDataRepository Instance
		{
			get
			{
				if (_Instance == null || _Instance.IsDisposed) _Instance = Create();
				return _Instance;
			}
		}
		static IDataRepository _Instance = null;

		/// <summary>
		/// Generates a string valid to be printed in the console.
		/// </summary>
		public static string ToConsoleString(this IDataMap map, int tabs = 0)
		{
			var header = tabs <= 0 ? string.Empty : new string('\t', tabs);
			var sb = new StringBuilder();

			sb.AppendFormat("{0}", map == null ? "<null>" : map.ToString());
			if (map != null && !map.IsDisposed)
			{
				if (map.IsValidated)
					sb.AppendFormat("\n{0}- Validated: true", header);

				if (map.VersionColumn.Name != null)
					sb.AppendFormat("\n{0}- Version Column: {1}", header, map.VersionColumn);

				if (map.Columns.Count != 0)
					sb.AppendFormat("\n{0}- Columns: {1}", header, map.Columns);

				foreach (var member in map.Members)
				{
					sb.AppendFormat("\n{0}- Member: {1}", header, member);
				}
			}

			return sb.ToString();
		}

		/// <summary>
		/// Generates a string valid to be printed in the console.
		/// </summary>
		public static string ToConsoleString(this IDataRepository repo)
		{
			var sb = new StringBuilder();

			sb.AppendFormat("{0}", repo == null ? "<null>" : repo.ToString());
			if (repo != null && !repo.IsDisposed)
			{
				foreach (var map in repo.Maps)
					sb.AppendFormat("\n\n\t- Map: {0}", map.ToConsoleString(2));
			}

			return sb.ToString();
		}
	}
}
// ======================================================== 

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