Click here to Skip to main content
15,895,256 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 548.1K   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.ORM.Core.Concrete
{
	using Kerosene.Tools;
	using System;
	using System.Runtime.Serialization;
	using System.Text;

	// ==================================================== 
	/// <summary>
	/// Represents the alias of a given element in a command.
	/// </summary>
	[Serializable]
	public class KAliasBase : IKAlias
	{
		bool _IsDisposed = false;
		IKAliasCollection _Owner = null;
		string _Alias = null;
		string _Table = null;

		/// <summary>
		/// Creates a new empty and orphan instance.
		/// </summary>
		public KAliasBase() { }

		/// <summary>
		/// Gets whether this instance has been disposed or not.
		/// </summary>
		public bool IsDisposed
		{
			get { return _IsDisposed; }
		}

		/// <summary>
		/// Disposes this instance, and removes it from its owner it any.
		/// </summary>
		public void Dispose()
		{
			if (!IsDisposed) { OnDispose(true); GC.SuppressFinalize(this); }
		}

		~KAliasBase()
		{
			if (!IsDisposed) OnDispose(false);
		}

		protected virtual void OnDispose(bool disposing)
		{
			if (!IsDisposed && disposing)
			{
				if (_Owner != null)
				{
					var temp = _Owner; _Owner = null;
					if (temp != null && !temp.IsDisposed) temp.Remove(this);
				}
			}
			_IsDisposed = true;
		}

		public virtual void GetObjectData(SerializationInfo info, StreamingContext context)
		{
			if (IsDisposed) throw new ObjectDisposedException(this.ToString());

			info.AddValue("Alias", _Alias);
			info.AddValue("Table", _Table);
		}

		protected KAliasBase(SerializationInfo info, StreamingContext context)
		{
			_Alias = info.GetString("Alias");
			_Table = info.GetString("Table");
		}

		/// <summary>
		/// Returns a new orphan instance that otherwise is a copy of the current one.
		/// </summary>
		public KAliasBase Clone()
		{
			if (IsDisposed) throw new ObjectDisposedException(this.ToString());
			var cloned = new KAliasBase();
			OnClone(cloned); return cloned;
		}
		IKAlias IKAlias.Clone()
		{
			return this.Clone();
		}
		object ICloneable.Clone()
		{
			return this.Clone();
		}

		protected virtual void OnClone(object cloned)
		{
			var temp = (KAliasBase)cloned;

			temp._Alias = _Alias;
			temp._Table = _Table;
		}

		/// <summary>
		/// Returns whether this instance can be considered equivalent to the other one given.
		/// </summary>
		/// <param name="other">The other one to compare this one with.</param>
		public bool Equals(KAliasBase other)
		{
			if (object.ReferenceEquals(this, other)) return true;
			if (object.ReferenceEquals(null, other)) return false;

			if (this.IsDisposed) return false;
			if (other.IsDisposed) return false;

			var sensitive = _Owner == null ? IKAliasCollectionHelper.DefaultCaseSensitive : _Owner.CaseSensitive;
			if (string.Compare(_Alias, other._Alias, ignoreCase: !sensitive) != 0) return false;
			if (string.Compare(_Table, other._Table, ignoreCase: !sensitive) != 0) return false;

			return true;
		}
		bool IEquatable<IKAlias>.Equals(IKAlias other)
		{
			if (Object.ReferenceEquals(this, other)) return true;
			if (Object.ReferenceEquals(null, other)) return false;

			var temp = other as KAliasBase; if (Object.ReferenceEquals(null, temp)) return false;
			return Equals(temp);
		}

		public override bool Equals(object other)
		{
			if (Object.ReferenceEquals(this, other)) return true;
			if (Object.ReferenceEquals(null, other)) return false;

			var temp = other as KAliasBase; if (Object.ReferenceEquals(null, temp)) return false;
			return Equals(temp);
		}

		public override int GetHashCode()
		{
			return ToString().GetHashCode();
		}

		public static bool operator ==(KAliasBase left, KAliasBase right)
		{
			if (object.ReferenceEquals(left, null) && object.ReferenceEquals(right, null)) return true;
			if (object.ReferenceEquals(left, null) && !object.ReferenceEquals(right, null)) return false;
			if (!object.ReferenceEquals(left, null) && object.ReferenceEquals(right, null)) return false;
			if (object.ReferenceEquals(left, right)) return true;

			return left.Equals(right);
		}
		public static bool operator !=(KAliasBase left, KAliasBase right)
		{
			return !(left == right);
		}

		/// <summary>
		/// Returns the string representation of this instance.
		/// </summary>
		public override string ToString()
		{
			StringBuilder sb = new StringBuilder();
			if (IsDisposed) sb.Append("disposed::[");

			sb.AppendFormat("{0} => {1}",
				_Alias ?? "-",
				_Table ?? "-");

			if (IsDisposed) sb.Append("]");
			return sb.ToString();
		}

		/// <summary>
		/// Gets the collection this parameter belongs to, or null if it is an orphan instance.
		/// </summary>
		public IKAliasCollection Owner
		{
			get { return _Owner; }
			set
			{
				if (value == null)
				{
					var temp = _Owner; _Owner = null;
					if (temp != null && !temp.IsDisposed) temp.Remove(this);
				}
				else
				{
					if (IsDisposed) throw new ObjectDisposedException(this.ToString());
					if (object.ReferenceEquals(_Owner, value)) return;
					if (value.IndexOf(this) < 0) value.Add(this);
				}
				_Owner = value;
			}
		}

		/// <summary>
		/// Gets the actual alias maintained by this instance.
		/// <para>The setter fails if this instance is not an orphan one.</para>
		/// </summary>
		public string Alias
		{
			get { return _Alias; }
			set
			{
				if (IsDisposed) throw new ObjectDisposedException(this.ToString());
				if (Owner != null) throw new NotOrphanException(string.Format("This '{0}' is not orphan.", this));
				_Alias = IKAliasHelper.ValidateAlias(value);
			}
		}

		/// <summary>
		/// Gets the table name (or element) this alias refer to, or null if it refers to the default one.
		/// <para>The setter fails if this instance is not an orphan one.</para>
		/// </summary>
		public string Table
		{
			get { return _Table; }
			set
			{
				if (IsDisposed) throw new ObjectDisposedException(this.ToString());
				if (Owner != null) throw new NotOrphanException(string.Format("This '{0}' is not orphan.", this));
				_Table = IKAliasHelper.ValidateTable(value);
			}
		}
	}
}
// ======================================================== 

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