Click here to Skip to main content
15,885,278 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 541.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.DataServices.Concrete
{
	using Kerosene.DataServices.Agnostic;
	using Kerosene.ORM.Maps;
	using Kerosene.Tools;
	using System;
	using System.Collections;
	using System.Text;

	// ==================================================== 
	/// <summary>
	/// Represents an object able to execute a query command and to return afterwards the entities produced as the
	/// result of that execution.
	/// </summary>
	/// <typeparam name="T">The type of the entities to be produced when the command is executed and enumerated.</typeparam>
	public class DataQueryEnumerator<T> : IDataQueryEnumerator<T> where T : class
	{
		bool _IsDisposed = false;
		DataQuery<T> _DataQuery = null;
		KMetaQueryEnumerator<T> _Enumerator = null;

		/// <summary>
		/// Creates a new enumerator for the given data query.
		/// </summary>
		/// <param name="dataQuery">The data query this enumerator is created for.</param>
		public DataQueryEnumerator(DataQuery<T> dataQuery)
		{
			if ((_DataQuery = dataQuery) == null) throw new ArgumentNullException("dataQuery", "DataQuery cannot be null.");
			if ((_Enumerator = _DataQuery.MetaCommand.GetEnumerator()) == null) throw new CannotCreateException(string.Format("Cannot create enumerator for command '{0}'.", dataQuery));
		}

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

		/// <summary>
		/// Disposes this instance.
		/// </summary>
		public void Dispose()
		{
			if (!IsDisposed) { OnDispose(true); GC.SuppressFinalize(this); }
		}

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

		protected virtual void OnDispose(bool disposing)
		{
			if (!IsDisposed && disposing)
			{
				if (_Enumerator != null) _Enumerator.Dispose(); _Enumerator = null;
				_DataQuery = null;
			}
			_IsDisposed = true;
		}

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

			sb.Append(_Enumerator == null ? GetType().EasyName() : _Enumerator.ToString());

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

		/// <summary>
		/// Gets the query command this enumerator is associated with.
		/// </summary>
		public DataQuery<T> Command
		{
			get { return _DataQuery; }
		}
		IDataQuery<T> IDataQueryEnumerator<T>.Command
		{
			get { return this.Command; }
		}

		/// <summary>
		/// Gets the current object being enumerated, or null if this command has not been executed yet or if there
		/// are no more records available.
		/// </summary>
		public T Current
		{
			get { return _Enumerator == null ? null : _Enumerator.Current; }
		}
		object IEnumerator.Current
		{
			get { return this.Current; }
		}

		/// <summary>
		/// Executes the command if it has not been executed yet, and tries to retrieve the next available record from
		/// the database. Returns true if a new record is available and can be obtained using the Current property, or
		/// false if there are no more records available.
		/// </summary>
		public bool MoveNext()
		{
			if (IsDisposed) throw new ObjectDisposedException(this.ToString());
			return _Enumerator.MoveNext();
		}

		/// <summary>
		/// Resets this enumerator.
		/// </summary>
		public void Reset()
		{
			if (_Enumerator != null && !_Enumerator.IsDisposed) _Enumerator.Reset();
		}
	}
}
// ======================================================== 

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