Click here to Skip to main content
15,897,518 members
Articles / Desktop Programming / Windows Forms

Three-tier .NET Application Utilizing Three ORM Technologies

Rate me:
Please Sign up or sign in to vote.
4.95/5 (118 votes)
30 Jan 2010CPOL109 min read 166.1K   4.4K   437  
LINQ to SQL, Entity Framework, and NHibernate used in a parallel fashion in a three-tier WinForms application.
using System;
using System.Collections.Generic;
using System.Collections;
using System.Text;
using System.Linq;
using s = System;
using ser=System.Runtime.Serialization;

namespace Util
{
	public interface ICommand
	{
		void Execute();	
	}
	public class ActionCommand<T> : ICommand
	{
		T _param;
		s.Action<T> _Action;
		public ActionCommand(s.Action<T> aAction, T param)
		{
			_param = param;
			_Action = aAction;
		}
		public void Execute()
		{
			_Action(_param);
		}
	}
	public class ActionCommand<T1, T2> : ICommand
	{
		T1 _param1;
		T2 _param2;
		s.Action<T1, T2> _Action;
		public ActionCommand(s.Action<T1, T2> aAction, T1 param1, T2 param2)
		{
			_param1 = param1;
			_param2 = param2;
			_Action = aAction;
		}
		public void Execute()
		{
			_Action(_param1, _param2);
		}
	}
	public class ActionCommand<T1, T2, T3> : ICommand
	{
		T1 _param1;
		T2 _param2;
		T3 _param3;
		s.Action<T1, T2, T3> _Action;
		public ActionCommand(s.Action<T1, T2, T3> aAction, T1 param1, T2 param2, T3 param3)
		{
			_param1 = param1;
			_param2 = param2;
			_param3 = param3;
			_Action = aAction;
		}
		public void Execute()
		{
			_Action(_param1, _param2, _param3);
		}
	}
	public class FuncCommand<TResult> : ICommand
	{
		TResult _result;
		s.Func<TResult> _Func;
		public FuncCommand(s.Func<TResult> aFunc, TResult error_result)
		{
			_Func = aFunc;
			_result = error_result;
		}
		public void Execute()
		{
			_result = _Func();
		}
		public TResult Result{get{return _result;}}
	}
	public class FuncCommand<T, TResult> : ICommand
	{
		T _param;
		TResult _result;
		s.Func<T, TResult> _Func;
		public FuncCommand(s.Func<T, TResult> aFunc, T param, TResult error_result)
		{
			_param = param;
			_Func = aFunc;
			_result = error_result;
		}
		public void Execute()
		{
			_result = _Func(_param);
		}
		public TResult Result{get{return _result;}}
	}
	public class FuncCommand<T1, T2, TResult> : ICommand
	{
		T1 _param1;
		T2 _param2;
		TResult _result;
		s.Func<T1, T2, TResult> _Func;
		public FuncCommand(s.Func<T1, T2, TResult> aFunc, T1 param1, T2 param2, TResult error_result)
		{
			_param1 = param1;
			_param2 = param2;
			_Func = aFunc;
			_result = error_result;
		}
		public void Execute()
		{
			_result = _Func(_param1, _param2);
		}
		public TResult Result{get{return _result;}}
	}
	public interface IROList<T> : IEnumerable<T>
	{
		T this[int index] { get;}
		int IndexOf(T item);
		int Count { get; }
		bool Contains(T item);
		void CopyTo(T[] array, int arrayIndex);
	}
	[ser.DataContract]
	public class ROList<T> : IROList<T>
	{
		[ser.DataMember] s.Collections.ObjectModel.ReadOnlyCollection<T> _d;

		public ROList(s.Collections.ObjectModel.ReadOnlyCollection<T> d){_d = d;}

		#region IROList<T> Members

		public T				this[int index]						{get { return _d[index];}}
		public int				Count								{get { return _d.Count;}}
		public int				IndexOf(T item)						{return _d.IndexOf(item);}
		public bool				Contains(T item)			 		{return _d.Contains(item);}
		public IEnumerator<T>	GetEnumerator()						{return _d.GetEnumerator();}
			   IEnumerator		IEnumerable.GetEnumerator()			{return _d.GetEnumerator();}
		public void				CopyTo(T[] array, int arrayIndex)	{_d.CopyTo(array, arrayIndex);}

		#endregion
	}
	[ser.DataContract]
	public class ROArray<T> : IROList<T>
	{
		[ser.DataMember] T[] _d;

		public ROArray(T[] d){_d = d;}
		public ROArray(IList<T> d){_d = d.ToArray();}

		#region IROList<T> Members

		public T				this[int index]						{get { return _d[index];}}
		public int				Count								{get { return _d.Length;}}
		public int				IndexOf(T item)						{return Array.IndexOf(_d, item);}
		public bool				Contains(T item)			 		{return _d.Contains(item);}
		public IEnumerator<T>	GetEnumerator()						
		{
			foreach(T t in _d)
			{
				yield return t;
			}
		}
			   IEnumerator		IEnumerable.GetEnumerator()			{return _d.GetEnumerator();}
		public void				CopyTo(T[] array, int arrayIndex)	{_d.CopyTo(array, arrayIndex);}

		#endregion
	}
	public static class C
	{
		public static bool AreAllItemsEqual<T>(IEnumerable<T> stream1, IEnumerable<T> stream2) where T : s.IEquatable<T>
		{
			if (stream1 == null || stream2 == null)
				throw new System.ArgumentException("agrs must be non-null");

			IEnumerator<T> e1 = stream1.GetEnumerator();
			IEnumerator<T> e2 = stream2.GetEnumerator();
			bool b1, b2;
			b1 = e1.MoveNext();
			b2 = e2.MoveNext();
			if (!b1 || !b2)
				throw new System.ArgumentException("args must have values");

			while (b1 && b2)
			{
				if (!(e1.Current.Equals(e2.Current)))
					return false;

				b1 = e1.MoveNext();
				b2 = e2.MoveNext();
				if (b1 != b2)
					return false;
			}
			return true;
		}
		public static bool DoItemStreamsMatch<T>(IEnumerable<T> stream1, IEnumerable<T> stream2, Func<T, T, bool> DoesMatchDel)
		{
			if(stream1==null && stream2==null)
				return true;
			if(stream1==null || stream2==null)
				return false;

			IEnumerator<T> e1 = stream1.GetEnumerator();
			IEnumerator<T> e2 = stream2.GetEnumerator();
			bool b1, b2;
			b1 = e1.MoveNext();
			b2 = e2.MoveNext();
			if (!b1 && !b2)
				return true; //both streams empty
			if (!b1 || !b2)
				return false; //one of the streams is empty

			while (b1 && b2)
			{
				if(!DoesMatchDel(e1.Current, e2.Current))
					return false;

				b1 = e1.MoveNext();
				b2 = e2.MoveNext();
				if (b1 != b2)
					return false;
			}
			return true;
		}
		public static s.DateTime FromStandardTime(string str)
		{
			if(str == null)
				throw new s.Exception();
			str = str.Replace(" ", "");
			str = str.Replace(":", "");
			str = str.Replace("-", "");
			if(str.Length == 0)
				throw new s.Exception();
			try
			{
				int year	=s.Int32.Parse(str.Substring(0, 4));
				int month	=s.Int32.Parse(str.Substring(4, 2));
				int day		=s.Int32.Parse(str.Substring(6, 2));
				int hour	=0;
				int minute	=0;
				int second	=0;
				if(str.Length >= 10) hour	=s.Int32.Parse(str.Substring( 8, 2));
				if(str.Length >= 12) minute =s.Int32.Parse(str.Substring(10, 2));
				if(str.Length >= 14) second =s.Int32.Parse(str.Substring(12, 2));

				return new DateTime(year, month, day, hour, minute, second);
			}
			catch(s.Exception ex)
			{
				s.Exception ex2 = new Exception("Unable to translate date-time value from standard format.", ex);
				throw ex2;
			}
		}
		public static string ToStandardTimeToTheDay		(s.DateTime datetime){if(datetime==s.DateTime.MinValue)return null; return datetime.ToString("yyyy-MM-dd"		);}
		public static string ToStandardTimeToTheMinute	(s.DateTime datetime){if(datetime==s.DateTime.MinValue)return null; return datetime.ToString("yyyy-MM-dd HH:mm"	);}
		public static string ToStandardTimeToTheSecond	(s.DateTime datetime){if(datetime==s.DateTime.MinValue)return null; return datetime.ToString("yyyy-MM-dd HH:mm:ss"	);}
	}
}

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
Software Developer (Senior) Austin Regional Clinic
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions