Click here to Skip to main content
15,897,371 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 549.5K   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.Maps
{
	using System;
	using System.Collections.Generic;

	// ==================================================== 
	/// <summary>
	/// Represents a column in the database that will be transfered back and forth the record associated with the
	/// entities even if there is no matching member in the type for the column name. This class is typically used to
	/// retrieve and persist the columns used by the extended members collection of the map.
	/// </summary>
	public interface IKMapForcedColumn
	{
		/// <summary>
		/// The map this instance belongs to.
		/// </summary>
		IKMetaMap MetaMap { get; }

		/// <summary>
		/// The name of the forced column in the database.
		/// </summary>
		string Name { get; }

		/// <summary>
		/// If not null the delegate to invoke with (entity, value) arguments, typically to load into the entity the
		/// value obtained from the record associated with this column. If this delegate is null, the value is loaded
		/// into the record only if there is a matching member in the type, otherwise no transfer is performed.
		/// </summary>
		Action<object, object> LoadEntityDelegate { get; }

		/// <summary>
		/// Sets or clears the delegate to invoke with (entity, value) arguments, typically to load into the entity
		/// the value obtained from the record associated with this column. If this delegate is null, the value is
		/// loaded into the record only if there is a matching member in the type, otherwise no transfer is performed.
		/// <para>This method fails if the map is already validated.</para>
		/// <para>Returns this instance to permit a fluent syntax chaining.</para>
		/// </summary>
		/// <param name="onLoad">The delegate to set or null to clear it.</param>
		IKMapForcedColumn OnLoadEntity(Action<object, object> onLoad);

		/// <summary>
		/// If not null the delegate to invoke with (entity) argument to obtain the value associated with this column
		/// that will be written into the record. If this delegate is null then a value is obtained from the entity
		/// only if there is a matching member in the type, otherwise no transfer is performed.
		/// </summary>
		Func<object, object> WriteRecordDelegate { get; }

		/// <summary>
		/// Sets or clears the delegate to invoke with (entity) argument to obtain the value associated with this
		/// column that will be written into the record. If this delegate is null then a value is obtained from the
		/// entity only if there is a matching member in the type, otherwise no transfer is performed.
		/// <para>This method fails if the map is already validated.</para>
		/// <para>Returns this instance to permit a fluent syntax chaining.</para>
		/// </summary>
		/// <param name="onWrite">The delegate to set or null to clear it.</param>
		IKMapForcedColumn OnWriteRecord(Func<object, object> onWrite);
	}

	// ==================================================== 
	/// <summary>
	/// Represents the collection of columns in the database that will be transfered back and forth the record
	/// associated with the entities even if there are no matching member in the type for the column names. This
	/// class is typically used to retrieve and persist the columns used by the extended members collection of the
	/// map.
	/// </summary>
	public interface IKMapForcedColumnCollection : IEnumerable<IKMapForcedColumn>
	{
		/// <summary>
		/// The map this instance belongs to.
		/// </summary>
		IKMetaMap MetaMap { get; }

		/// <summary>
		/// Adds a new entry into this collection.
		/// <para>Returns the new entry that has been created to permit a fluent syntax chaining.</para>
		/// <para>This method fails if the map is already validated.</para>
		/// </summary>
		/// <param name="name">A dynamic lambda expression resolving into the name of the forced column.</param>
		IKMapForcedColumn Add(Func<dynamic, object> name);

		/// <summary>
		/// Removes the given entry from this collection. Returns true if the entry has been removed sucessfully or
		/// false otherwise.
		/// </summary>
		/// <param name="entry">The entry to remove from this collection.</param>
		bool Remove(IKMapForcedColumn entry);

		/// <summary>
		/// Gets the number of entries in this collection.
		/// </summary>
		int Count { get; }

		/// <summary>
		/// Returns the first entry in this collection that matches the conditions given in the predicate, or null
		/// if no such entry can be found.
		/// </summary>
		/// <param name="match">The predicate that defines the conditions of the entry to look for.</param>
		IKMapForcedColumn Find(Predicate<IKMapForcedColumn> match);
	}
}
// ======================================================== 

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