Click here to Skip to main content
15,891,184 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 164.2K   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.Text;
using s = System;
using System.Linq;
using u = Util;
using gt=GlobalType;

namespace DomainUtil
{
	public interface INWAllocatedAccntDlt// : DTO.INWAccntDlt
	{
		int		NWAccntID	{get;}
		int		Amount		{get;}
		bool	Budgeted	{get;}
		int GetParticipantAmount(int iIndex);
		int ParticipantCount{get;}
	}
	public interface IDltAllocSum
	{
		int DltAllocAccntID { get; }
		bool Budgeted{get;}
		int GetParticipantAmount(int iIndex);
		int Amount { get; }
	}
	class NWAllocatedAccntDlt : INWAllocatedAccntDlt
	{
		int _iNWAccntID;
		bool _bBudgeted;
		int[] _arrParticipantAmounts;

		public int NWAccntID { get { return _iNWAccntID; } /*set { _iNWAccntID = value; } */}
		public bool Budgeted{get{return _bBudgeted;}}
		public int[] ParticipantAmounts { get { return _arrParticipantAmounts; } /*set { _arrParticipantAmounts = value; } */}
		public NWAllocatedAccntDlt(int iNWAccntID, bool bBudgeted, int[] arrParticipantAmounts)
		{
			_iNWAccntID = iNWAccntID;
			_bBudgeted = bBudgeted;
			_arrParticipantAmounts = arrParticipantAmounts;
		}
		public int GetParticipantAmount(int iIndex) 
		{ 
			if(_arrParticipantAmounts == null)
				return 0;
			return _arrParticipantAmounts[iIndex]; 
		}
		public int Amount { get { return _arrParticipantAmounts.Aggregate((total, i) => total + i); } }
		public int ParticipantCount{get{return _arrParticipantAmounts.Length;}}
	}
	public class TransactionNWAllocatedAccntDltUpdate
	{
		int[] _arrParticipantIDs;
		List<NWAllocatedAccntDlt> _lstNWAllocatedAccntDlts;
		public TransactionNWAllocatedAccntDltUpdate(int[] arrParticipantIDs)
		{
			_arrParticipantIDs = arrParticipantIDs;
			_lstNWAllocatedAccntDlts = new List<NWAllocatedAccntDlt>();
		}
		public void AddNWAllocatedAccntDlt(int iNWAccntID, bool bBudgeted, int iAmount, int[] arrParticipantShares)
		{
			if (arrParticipantShares == null)
				throw new s.ArgumentException("Participant shares must not be null.");
			if (arrParticipantShares.Length != _arrParticipantIDs.Length)
				throw new s.ArgumentException("Participant shares array must be the same length as the ParticipantID array.");

			float[] arrParticipantShares2 = new float[arrParticipantShares.Length];
			for (int i = 0; i < arrParticipantShares.Length; i++)
			{
				arrParticipantShares2[i] = arrParticipantShares[i];
			}
			NWAllocatedAccntDlt aNWAllocatedAccntDlt = new NWAllocatedAccntDlt(iNWAccntID, bBudgeted, Misc.DistributeValue(iAmount, arrParticipantShares2));
			_lstNWAllocatedAccntDlts.Add(aNWAllocatedAccntDlt);

		}
		internal List<NWAllocatedAccntDlt> Get()
		{
			return _lstNWAllocatedAccntDlts;
		}
		internal int[] ParticipantIDs { get { return _arrParticipantIDs; } }
		internal void Clear()
		{
			_lstNWAllocatedAccntDlts.Clear();
		}
		public int ParticipantCount{get{return _arrParticipantIDs.Length;}}
	}
	public class TransactionNWAccntDltUpdate
	{
		int[] _arrParticipantIDs;
		List<DTO.NWAccntDlt> _lstNWAccntDlts;
		public TransactionNWAccntDltUpdate(int[] arrParticipantIDs)
		{
			_arrParticipantIDs = arrParticipantIDs;
			_lstNWAccntDlts = new List<DTO.NWAccntDlt>();
		}
		public void AddNWAccntDlt(int iNWAccntID, bool bBudgeted, int iAmount)
		{
			DTO.NWAccntDlt aNWAccntDlt = new DTO.NWAccntDlt(iNWAccntID, bBudgeted, iAmount);
			_lstNWAccntDlts.Add(aNWAccntDlt);
		}
		internal List<DTO.NWAccntDlt> Get()
		{
			return _lstNWAccntDlts;
		}
		internal int[] ParticipantIDs { get { return _arrParticipantIDs; } }
		internal void Clear()
		{
			_lstNWAccntDlts.Clear();
		}
		public int ParticipantCount{get{return _arrParticipantIDs.Length;}}
	}
	public class TransactionItemUpdate
	{
		int[] _arrParticipantIDs;
		List<DTO.Item> _lstItems;
		public TransactionItemUpdate(int[] arrParticipantIDs)
		{
			_arrParticipantIDs = arrParticipantIDs;
			_lstItems = new List<DTO.Item>();
		}
		public void AddItem(string strDescrip, int iDltAllocAccntID, bool bDltAllocAccntIsBudgeted, int iTotalOrMultiplier, float[] arrParticipantShares)
		{
			if (arrParticipantShares == null)
				throw new s.ArgumentException("Participant shares must not be null.");
			if (arrParticipantShares.Length != _arrParticipantIDs.Length)
				throw new s.ArgumentException("Participant shares array must be the same length as the ParticipantID array.");

			DTO.Item aItem = new DTO.Item(0, strDescrip, iDltAllocAccntID, bDltAllocAccntIsBudgeted, new u.ROArray<int>(Misc.DistributeValue(iTotalOrMultiplier, arrParticipantShares)));
			_lstItems.Add(aItem);
		}
		public void AddItem(int? iID, string strDescrip, int iDltAllocAccntID, bool bDltAllocAccntIsBudgeted, u.ROArray<int> ParticipantAmounts)
		{
			if (ParticipantAmounts == null)
				throw new s.ArgumentException("Participant shares must not be null.");
			if (ParticipantAmounts.Count() != _arrParticipantIDs.Length)
				throw new s.ArgumentException("Participant shares array must be the same length as the ParticipantID array.");

			DTO.Item aItem = new DTO.Item(iID.GetValueOrDefault(), strDescrip, iDltAllocAccntID, bDltAllocAccntIsBudgeted, ParticipantAmounts);
			_lstItems.Add(aItem);
		}
		public int[] ParticipantIDs { get { return _arrParticipantIDs; } }
		public void ClearAllItems()
		{
			_lstItems.Clear();
		}
		internal List<DTO.Item> Get()
		{
			return _lstItems;
		}
		public int ParticipantCount{get{return _arrParticipantIDs.Length;}}
	}
	public interface ITransaction
	{
		int ParticipantCount { get; }
		int NWAccntDltCount { get; }
		int ItemCount { get; }
		int DltAllocSumCount { get; }
		IEnumerable<int> ParticipantIDs { get; }
		IEnumerable<DTO.NWAccntDlt> NWAccntDlts { get; }
		DTO.NWAccntDlt[] NWAccntDltsDeepCopy { get; }
		IEnumerable<DTO.Item> Items { get; }
		DTO.Item[] ItemsDeepCopy { get; }
		IEnumerable<IDltAllocSum> DltAllocSums { get; }
		IEnumerable<int> WashAmounts { get; }
		int GetParticipantID(int i);
		int GetWashAmount(int i);
		DTO.NWAccntDlt GetNWAccntDlt(int i);
		DTO.Item GetItem(int i);
		IDltAllocSum GetDltAllocSum(int i);
		int GetAmountNWDltsExceedAllocDltsBy();
		int? ID { get; }
		gt.RowVersion TheRowVersion { get; }
		int? PostID { get; }
		int? ParticipantID { get; set; }
		string Descrip { get; set; }
		s.DateTime Instant { get; set; }
		void ResetNWAccntDlts(TransactionNWAllocatedAccntDltUpdate update);
		void ResetItems(TransactionItemUpdate update);
		int GetTotalNWDltsAmount();
		int GetTotalDltAllocsAmount();
		int GetBudgetDelt();
		int GetTotalDltAllocAmountForParticipant(int iParticipantIndex);
	}
	class Transaction : ITransaction
	{
		internal class DltAllocSum : IDltAllocSum
		{
			int _iDltAllocAccntID;
			bool _bBudgeted;
			int[] _arrParticipantAmounts;

			public int DltAllocAccntID { get { return _iDltAllocAccntID; } /*set { _iDltAllocAccntID = value; }*/ }
			public bool Budgeted{get{return _bBudgeted;}}
			public int[] ParticipantAmounts { get { return _arrParticipantAmounts; } /*set { _arrParticipantAmounts = value; } */}

			public DltAllocSum(int iDltAllocAccntID, bool bBudgeted, int[] arrParticipantAmounts)
			{
				_iDltAllocAccntID = iDltAllocAccntID;
				_bBudgeted = bBudgeted;
				_arrParticipantAmounts = arrParticipantAmounts;
			}
			public int GetParticipantAmount(int iIndex) { return _arrParticipantAmounts[iIndex]; }
			public int Amount { get { return _arrParticipantAmounts.Aggregate((total, i) => total + i); } }
		}

		int? _iID;
		gt.RowVersion _RowVersion;
		int? _iPostID;
		int? _iParticipantID;
		int[] _arrParticipantIDs;
		IList<DTO.NWAccntDlt> _lstNWAccntDlts;
		IList<DTO.Item> _lstItems;
		int _iError;
		int[] _arrWashAmounts;
		string _strDescrip;
		s.DateTime _dtInstant;

		//computed
		IList<DltAllocSum> _lstDltAllocSums;
		static void ValidateNWAllocatedAccntDlts(IList<NWAllocatedAccntDlt> lstNWAllocatedAccntDlts, int? iParticipants)
		{
			if (lstNWAllocatedAccntDlts != null)
			{
				foreach (INWAllocatedAccntDlt aNWAllocatedAccntDlt in lstNWAllocatedAccntDlts)
				{
					if (aNWAllocatedAccntDlt == null)
						throw new s.ArgumentException("35");
					if (aNWAllocatedAccntDlt.ParticipantCount == 0)
						throw new s.ArgumentException("36");
					if (iParticipants.HasValue && aNWAllocatedAccntDlt.ParticipantCount != iParticipants.Value)
						throw new s.ArgumentException("37");
				}
			}
		}
		static void ValidateNWAccntDlts(IList<DTO.NWAccntDlt> lstNWAccntDlts)
		{
			if (lstNWAccntDlts != null)
			{
				foreach (DTO.NWAccntDlt aNWAccntDlt in lstNWAccntDlts)
				{
					if (aNWAccntDlt == null)
						throw new s.ArgumentException("35");
				}
			}
		}
		static void ValidateItems(IList<DTO.Item> lstItems, int? iParticipants)
		{
			if (lstItems != null)
			{
				foreach (DTO.Item aItem in lstItems)
				{
					if (aItem == null)
						throw new s.ArgumentException("38");
					if (aItem.ParticipantAmounts==null)
						throw new s.ArgumentException("39");
					if (iParticipants.HasValue && aItem.ParticipantAmounts.Count != iParticipants.Value)
						throw new s.ArgumentException("40");
				}
			}
		}
		static void Validate(IList<NWAllocatedAccntDlt> lstNWAllocatedAccntDlts, IList<DTO.Item> lstItems)
		{
			int iTotalDlts = 0;
			if (lstNWAllocatedAccntDlts != null)
				iTotalDlts += lstNWAllocatedAccntDlts.Count;
			if (lstItems != null)
				iTotalDlts += lstItems.Count;
			if (iTotalDlts < 1)
				throw new ArgumentException("At least one deltas must be supplied.");
		}
		static void Validate(IList<DTO.NWAccntDlt> lstNWAccntDlts, IList<DTO.Item> lstItems)
		{
			int iTotalDlts = 0;
			if (lstNWAccntDlts != null)
				iTotalDlts += lstNWAccntDlts.Count;
			if (lstItems != null)
				iTotalDlts += lstItems.Count;
		//	if (iTotalDlts < 1)
		//		throw new ArgumentException("At least one deltas must be supplied.");
		}
		static void ValidateWashAmounts(int[] arrWashAmounts, int? iParticipants)
		{
			if (arrWashAmounts != null)
			{
				if (iParticipants == null)
					throw new s.ArgumentException("41");
				else if (iParticipants != arrWashAmounts.Length)
					throw new s.ArgumentException("42");
				int iTotalWash = arrWashAmounts.Aggregate(0, (total, iAmount) => total + iAmount);
				if (iTotalWash != 0)
					throw new s.ArgumentException("Total Wash is not zero");
			}
		}
		static void ValidateParticipantIDs(int[] arrParticipantIDs, int[] arrParticpantIDsAccepted)
		{
			if (arrParticipantIDs == null)
				throw new s.ArgumentException("Participant IDs must not be null.");
			int iParticipants = arrParticipantIDs.Length;

			if (iParticipants < 1)
				throw new s.ArgumentException("At least one Participant ID is required.");

			if (arrParticpantIDsAccepted == null)
				return;
			if(!Util.C.AreAllItemsEqual<int>(arrParticipantIDs, arrParticpantIDsAccepted))
				throw new s.ArgumentException("ParticipantIDs doesn't match the one supplied earlier");

			
		}
//		static void ValidateParams(IList<NWAllocatedAccntDlt> lstNWAllocatedAccntDlts, IList<Item> lstItems, int? iParticipants)
//		{
//			Validate(lstNWAllocatedAccntDlts, lstItems);
//			ValidateNWAllocatedAccntDlts(lstNWAllocatedAccntDlts, iParticipants);
//			ValidateItems(lstItems, iParticipants);
//		}
		//static void ValidateParams(IList<NWAccntDlt> lstNWAccntDlts, IList<Item> lstItems, int[] arrWashAmounts, int? iParticipants)
		//{
		//    Validate(lstNWAccntDlts, lstItems);
		//    ValidateNWAccntDlts(lstNWAccntDlts);
		//    ValidateItems(lstItems, iParticipants);
		//    ValidateWashAmounts(arrWashAmounts, iParticipants);
		//}
		internal static int GetTotalNWDltsAmount(IList<NWAllocatedAccntDlt> lstNWAllocatedAccntDlts)
		{
			int iNWDlt = 0;
			if (lstNWAllocatedAccntDlts != null)
			{
				foreach (INWAllocatedAccntDlt aNWAllocatedAccntDlt in lstNWAllocatedAccntDlts)
				{
					if (aNWAllocatedAccntDlt != null/* && aNWAllocatedAccntDlt.ParticipantAmounts != null*/)
					{
					//	iNWDlt += aNWAllocatedAccntDlt.ParticipantAmounts.Aggregate(0, (total, iAmount) => total + iAmount);
						for(int i=0; i<aNWAllocatedAccntDlt.ParticipantCount; i++)
						{
							iNWDlt += aNWAllocatedAccntDlt.GetParticipantAmount(i);
						}
					}
				}
			}
			return iNWDlt;
		}
		internal static int GetTotalNWDltsAmount(IList<DTO.NWAccntDlt> lstNWAccntDlts)
		{
			int iNWDlt = 0;
			if (lstNWAccntDlts != null)
			{
				foreach (DTO.NWAccntDlt aNWAccntDlt in lstNWAccntDlts)
				{
					if (aNWAccntDlt != null)
						iNWDlt += aNWAccntDlt.Amount;
				}
			}
			return iNWDlt;
		}
		internal static int GetTotalDltAllocAmount(IList<DTO.Item> lstItems)
		{
			int iAllocDlt = 0;
			if (lstItems != null)
			{
				foreach (DTO.Item aItem in lstItems)
				{
//					if (aItem != null && aItem.ParticipantAmounts != null)
						iAllocDlt += aItem.ParticipantAmounts.Sum();
				}
			}
			return iAllocDlt;
		}
		internal static int GetTotalDltAllocAmountForParticipant(IList<DTO.Item> lstItems, int iParticipantIndex)
		{
			if (lstItems == null)
				return 0;
			return lstItems.Aggregate(0, (total, o) => total + o.ParticipantAmounts[iParticipantIndex]);
		}
		internal static int GetAmountNWDltsExceedAllocDltsBy(IList<NWAllocatedAccntDlt> lstNWAllocatedAccntDlts, IList<DTO.Item> lstItems)
		{
			int iNWDlt = GetTotalNWDltsAmount(lstNWAllocatedAccntDlts);
			int iAllocDlt = GetTotalDltAllocAmount(lstItems);
			return iNWDlt - iAllocDlt;
		}
		internal static int GetAmountNWDltsExceedAllocDltsBy(IList<DTO.NWAccntDlt> lstNWAccntDlts, IList<DTO.Item> lstItems)
		{
			int iNWDlt = GetTotalNWDltsAmount(lstNWAccntDlts);
			int iAllocDlt = GetTotalDltAllocAmount(lstItems);
			return iNWDlt - iAllocDlt;
		}
		static int[] GetWashAmounts(int[] arrParticipantIDs, IList<NWAllocatedAccntDlt> lstNWAllocatedAccntDlts, IList<DTO.Item> lstItems)
		{
			
			int[] arrWashDlt = new int[arrParticipantIDs.Length];
			for (int i = 0; i < arrParticipantIDs.Length; i++)
			{
				int iNWDlt = 0;
				int iAllocDlt = 0;
				if(lstNWAllocatedAccntDlts!=null)iNWDlt		= lstNWAllocatedAccntDlts.Aggregate(0, (accumulator, o) => accumulator + o.ParticipantAmounts[i]);
				if(lstItems				  !=null)iAllocDlt	= lstItems.Aggregate(				0, (accumulator, o) => accumulator + o.ParticipantAmounts[i]);
				arrWashDlt[i] = iAllocDlt - iNWDlt;
			}
			return arrWashDlt;
		}
		static int[] GetNewWashAmounts(int[] arrParticipantIDs, int[] arrCurrentWashAmounts, IList<DTO.Item> lstCurrentItems, IList<DTO.Item> lstNewItems)
		{
			//int iCurrentNWAccntDlt = lstCurrentNWAccntDlts.Aggregate(0, (accumulator, o) => accumulator + o.Amount);
			int[] arrNewItemDlts	 = new int[arrParticipantIDs.Length];
			int[] arrCurrentItemDlts = new int[arrParticipantIDs.Length];
			int[] arrCurrentNWDlts	 = new int[arrParticipantIDs.Length];
			int[] arrNewWash		 = new int[arrParticipantIDs.Length];

			if (lstNewItems == null) lstNewItems = new List<DTO.Item>();
			if (lstCurrentItems == null) lstCurrentItems = new List<DTO.Item>();
	
			for (int i = 0; i < arrParticipantIDs.Length; i++)
			{
				arrNewItemDlts[		i] = lstNewItems.Aggregate(		0, (accumulator, o) => accumulator + o.ParticipantAmounts[i]);
				arrCurrentItemDlts[	i] = lstCurrentItems.Aggregate(	0, (accumulator, o) => accumulator + o.ParticipantAmounts[i]);
				arrCurrentNWDlts[	i] = arrCurrentItemDlts[i] - arrCurrentWashAmounts[i];
				arrNewWash[			i] = arrNewItemDlts[i] - arrCurrentNWDlts[i];
			}
			return arrNewWash;
		}
		Dictionary<int, DltAllocSum> CreateDictionaryOfDltAllocSums(IList<DTO.Item> lstItems, int iParticipants)
		{
			Dictionary<int, DltAllocSum> dictDltAllocSums = new Dictionary<int, DltAllocSum>();
			foreach (DTO.Item aItem in lstItems)
			{
				DltAllocSum aDltAllocSum = null;
				dictDltAllocSums.TryGetValue(aItem.DltAllocAccntID, out aDltAllocSum);
				if (aDltAllocSum == null)
				{
					aDltAllocSum = new DltAllocSum(aItem.DltAllocAccntID, aItem.DltAllocAccntIsBudgeted, new int[iParticipants]);
					dictDltAllocSums[aItem.DltAllocAccntID] = aDltAllocSum;
				}
				for (int i = 0; i < iParticipants; i++)
				{
					aDltAllocSum.ParticipantAmounts[i] += aItem.ParticipantAmounts[i];
				}

			}
			return dictDltAllocSums;
		}
		void Init(int? iID, gt.RowVersion aRowVersion, int? iPostID, int? iParticipantID, string strDescrip, s.DateTime instant, int[] arrParticipantIDs, IList<DTO.NWAccntDlt> lstNWAccntDlts, IList<DTO.Item> lstItems, int[] arrWashAmounts, int iError)
		{
			int iParticipants = arrParticipantIDs.Length;
			if (lstItems != null)
			{
				Dictionary<int, DltAllocSum> dictDltAllocSums = CreateDictionaryOfDltAllocSums(lstItems, iParticipants);
				_lstDltAllocSums = dictDltAllocSums.Values.ToList();
			}
			_arrWashAmounts = arrWashAmounts;
			_arrParticipantIDs = arrParticipantIDs;
			_lstNWAccntDlts = lstNWAccntDlts;
			_lstItems = lstItems;
			_iID = iID;
			_RowVersion = aRowVersion;
			_iPostID = iPostID;
			_iParticipantID = iParticipantID;
			_strDescrip = strDescrip;
			_dtInstant = instant;
			_iError = iError;
		}
		internal Transaction(int? iID, gt.RowVersion aRowVersion, int? iPostID, int? iParticipantID, string strDescrip, s.DateTime instant, int[] arrParticipantIDs, IList<NWAllocatedAccntDlt> lstNWAllocatedAccntDlts, IList<DTO.Item> lstItems)
		{
			#region basic validation
			Transaction.ValidateParticipantIDs(arrParticipantIDs, null);
			Transaction.Validate(lstNWAllocatedAccntDlts, lstItems);
			Transaction.ValidateNWAllocatedAccntDlts(lstNWAllocatedAccntDlts, arrParticipantIDs.Length);
			Transaction.ValidateItems(lstItems, arrParticipantIDs.Length);

			#endregion

			//	Func<NWAllocatedAccntDlt, NWAccntDlt> f = delegate(NWAllocatedAccntDlt o)
			//	{
			//		return new NWAccntDlt(o.NWAccntID, o.Amount);
			//	};
			IList<DTO.NWAccntDlt> lstNWAccntDlts = null;
			if(lstNWAllocatedAccntDlts!=null)
				lstNWAccntDlts = lstNWAllocatedAccntDlts.Select((o) => new DTO.NWAccntDlt(o.NWAccntID, o.Budgeted, o.Amount)).ToList();
			int iError = GetAmountNWDltsExceedAllocDltsBy(lstNWAccntDlts, lstItems);
			int[] arrWashAmounts = GetWashAmounts(arrParticipantIDs, lstNWAllocatedAccntDlts, lstItems);

			Init(iID, aRowVersion, iPostID, iParticipantID, strDescrip, instant, arrParticipantIDs, lstNWAccntDlts, lstItems, arrWashAmounts, iError);
		}
		internal Transaction(int? iID, gt.RowVersion aRowVersion, int? iPostID, int? iParticipantID, string strDescrip, s.DateTime instant, int[] arrParticipantIDs, IList<DTO.NWAccntDlt> lstNWAccntDlts, IList<DTO.Item> lstItems, int[] arrWashAmounts, bool bValidate)
		{
			#region basic validation
			if(bValidate)
			{
				Transaction.ValidateParticipantIDs(arrParticipantIDs, null);
				Transaction.Validate(lstNWAccntDlts, lstItems);
				Transaction.ValidateNWAccntDlts(lstNWAccntDlts);
				Transaction.ValidateItems(lstItems, arrParticipantIDs.Length);
				Transaction.ValidateWashAmounts(arrWashAmounts, arrParticipantIDs.Length);
			}

			#endregion

			int iError = 0;
			if(bValidate)
			{
				iError = GetAmountNWDltsExceedAllocDltsBy(lstNWAccntDlts, lstItems);
				if (iError != 0)
					throw new s.ArgumentException("Not balanced.");
			}

			if (arrWashAmounts == null || arrWashAmounts.Length == 0)
				arrWashAmounts = new int[arrParticipantIDs.Length];

			Init(iID, aRowVersion, iPostID, iParticipantID, strDescrip, instant, arrParticipantIDs, lstNWAccntDlts, lstItems, arrWashAmounts, iError);
		}

		//public void ResetNWAccntDlts(TransactionNWAccntDltUpdate update)
		//{
		//    ValidateParticipantIDs(update.ParticipantIDs, _arrParticipantIDs);
		//    IList<NWAccntDlt> lstNWAccntDlts = update.Get();
		//    ValidateNWAccntDlts(lstNWAccntDlts);
		//    int iError = GetAmountNWDltsExceedAllocDltsBy(lstNWAccntDlts, this._lstItems);
		//    int[] arrWashAmounts = GetNewWashAmounts(_arrParticipantIDs, _arrWashAmounts, _lstItems, lstItems);
		//    _lstNWAccntDlts = lstNWAccntDlts;
		//    _iError = iError;
		//    _arrWashAmounts = arrWashAmounts;
		//}
		public void ResetNWAccntDlts(TransactionNWAllocatedAccntDltUpdate update)
		{
			ValidateParticipantIDs(update.ParticipantIDs, _arrParticipantIDs);
			IList<NWAllocatedAccntDlt> lstNWAllocatedAccntDlts = update.Get();
			ValidateNWAllocatedAccntDlts(lstNWAllocatedAccntDlts, update.ParticipantIDs.Length);
			IList<DTO.NWAccntDlt> lstNWAccntDlts = lstNWAllocatedAccntDlts.Select((o) => new DTO.NWAccntDlt(o.NWAccntID, o.Budgeted, o.Amount)).ToList();
			int iError = GetAmountNWDltsExceedAllocDltsBy(lstNWAccntDlts, this._lstItems);
			int[] arrWashAmounts = GetWashAmounts(_arrParticipantIDs, lstNWAllocatedAccntDlts, _lstItems);
			_lstNWAccntDlts = lstNWAccntDlts;
			_iError = iError;
			_arrWashAmounts = arrWashAmounts;
		}
		public void ResetItems(TransactionItemUpdate update)
		{
			ValidateParticipantIDs(update.ParticipantIDs, _arrParticipantIDs);
			IList<DTO.Item> lstItems = update.Get();
			ValidateItems(lstItems, _arrParticipantIDs.Length);
			int iError = GetAmountNWDltsExceedAllocDltsBy(_lstNWAccntDlts, lstItems);
			int[] arrWashAmounts = GetNewWashAmounts(_arrParticipantIDs, _arrWashAmounts, _lstItems, lstItems);
			IList<DltAllocSum> lstDltAllocSums = null;
			if (lstItems != null)
			{
				Dictionary<int, DltAllocSum> dictDltAllocSums = CreateDictionaryOfDltAllocSums(lstItems, _arrParticipantIDs.Length);
				lstDltAllocSums = dictDltAllocSums.Values.ToList();
			}
			_lstItems = lstItems;
			_iError = iError;
			_arrWashAmounts = arrWashAmounts;
			_lstDltAllocSums = lstDltAllocSums;
		}
		public int ParticipantCount {	get { return _arrParticipantIDs.Length; } }
		public int NWAccntDltCount {	get { return _lstNWAccntDlts	==null ? 0 : _lstNWAccntDlts.Count;		} }
		public int ItemCount {			get { return _lstItems			==null ? 0 : _lstItems.Count;			} }
		public int DltAllocSumCount {	get { return _lstDltAllocSums	==null ? 0 : _lstDltAllocSums.Count;	} }
		public IEnumerable<int> ParticipantIDs { get { return _arrParticipantIDs; } }
		public IEnumerable<DTO.NWAccntDlt> NWAccntDlts
		{
			get
			{
				if (_lstNWAccntDlts == null)
					yield break;
				foreach (DTO.NWAccntDlt o in _lstNWAccntDlts)
				{
					yield return o;
				}
			}
		}
		public DTO.NWAccntDlt[] NWAccntDltsDeepCopy
		{
			get
			{
				return NWAccntDlts.Select(o=>new DTO.NWAccntDlt(o.NWAccntID, o.Budgeted, o.Amount)).ToArray();
			}
		}
		public IEnumerable<DTO.Item> Items
		{
			get
			{
				if (_lstItems == null)
					yield break;
				foreach (DTO.Item o in _lstItems)
				{
					yield return o;
				}
			}
		}
		public DTO.Item[] ItemsDeepCopy
		{
			get
			{
				return Items.Select(o=>new DTO.Item(o.ID, o.Descrip, o.DltAllocAccntID, o.DltAllocAccntIsBudgeted, o.ParticipantAmounts)).ToArray();
			}
		}
		public IEnumerable<IDltAllocSum> DltAllocSums
		{
			get
			{
				if (_lstDltAllocSums == null)
					yield break;
				foreach (DltAllocSum o in _lstDltAllocSums)
				{
					yield return o;
				}
			}
		}
		public IEnumerable<int> WashAmounts { get { return _arrWashAmounts; } }
		public int GetParticipantID(int i) { return _arrParticipantIDs[i]; }
		public int GetWashAmount(int i) { return _arrWashAmounts[i]; }
		public DTO.NWAccntDlt GetNWAccntDlt(int i) { return _lstNWAccntDlts[i]; }
		public DTO.Item GetItem(int i) { return _lstItems[i]; }
		public IDltAllocSum GetDltAllocSum(int i) { return _lstDltAllocSums[i]; }
		public int GetAmountNWDltsExceedAllocDltsBy() { return _iError; }
		public int? ID { get { return _iID; } }
		public gt.RowVersion TheRowVersion { get { return _RowVersion; } }
		public int? PostID { get { return _iPostID; } }
		public int? ParticipantID { get { return _iParticipantID; } set { _iParticipantID = value;} }
		public string Descrip
		{
			get { return _strDescrip; }
			set { _strDescrip = value; }
		}
		public s.DateTime Instant
		{
			get { return _dtInstant; }
			set { _dtInstant = value; }
		}
		public int GetTotalNWDltsAmount()
		{
			if(_lstNWAccntDlts ==null)
				return 0;
			return this._lstNWAccntDlts.Aggregate(0, (accumulator, o) => accumulator + o.Amount);
		}
		public int GetTotalDltAllocsAmount()
		{
			return Transaction.GetTotalDltAllocAmount(this._lstItems);
		}
		public int GetTotalDltAllocAmountForParticipant(int iParticipantIndex)
		{
			return GetTotalDltAllocAmountForParticipant(_lstItems, iParticipantIndex);
		}
		public int GetBudgetDelt()
		{
			int iNWAccntDltBudget = 0;
			int iDltAllocBudget = 0;
			foreach (DTO.NWAccntDlt aNWAccntDlt in NWAccntDlts)
			{
				if(aNWAccntDlt.Budgeted)
					iNWAccntDltBudget += aNWAccntDlt.Amount;
			}
			foreach (IDltAllocSum aDltAllocSum in DltAllocSums)
			{
				if(aDltAllocSum.Budgeted)
					iDltAllocBudget += aDltAllocSum.Amount;
			}
			return iDltAllocBudget - iNWAccntDltBudget;
		}

	}
	public static class TransactionFactory
	{
		public static ITransaction Create(
			int? iID,
			gt.RowVersion aRowVersion,
			int? iPostID,
			int? iParticipantID,
			string strDescrip, 
			s.DateTime instant,
			int[] arrParticipantIDs,
			TransactionNWAllocatedAccntDltUpdate aTransactionNWAllocatedAccntDltUpdate,
			TransactionItemUpdate aTransactionItemUpdate
			)
		{
			IList<DTO.Item>					lstItems				= null;
			IList<NWAllocatedAccntDlt>	lstNWAllocatedAccntDlts = null;
			if (aTransactionNWAllocatedAccntDltUpdate	!= null) lstNWAllocatedAccntDlts = aTransactionNWAllocatedAccntDltUpdate.Get();
			if (aTransactionItemUpdate					!= null) lstItems				 = aTransactionItemUpdate.Get();
			return new Transaction(iID, aRowVersion, iPostID, iParticipantID, strDescrip, instant, arrParticipantIDs, lstNWAllocatedAccntDlts, lstItems);
		}
		public static ITransaction Create(
			int iID,
			gt.RowVersion aRowVersion,
			int? iPostID,
			int? iParticipantID,
			string strDescrip, 
			s.DateTime instant,
			int[] arrParticipantIDs,
			TransactionNWAccntDltUpdate aTransactionNWAccntDltUpdate,
			TransactionItemUpdate aTransactionItemUpdate,
			int[] arrWashAmounts
			)
		{
			IList<DTO.Item> lstItems = null;
			IList<DTO.NWAccntDlt> lstNWAccntDlts = null;
			if (aTransactionNWAccntDltUpdate	!= null) lstNWAccntDlts = aTransactionNWAccntDltUpdate.Get();
			if (aTransactionItemUpdate			!= null) lstItems		= aTransactionItemUpdate.Get();
			return new Transaction(iID, aRowVersion, iPostID, iParticipantID, strDescrip, instant, arrParticipantIDs, lstNWAccntDlts, lstItems, arrWashAmounts, true);
		}
		public static ITransaction Create(
			int? iID,
			gt.RowVersion aRowVersion,
			int? iPostID,
			int? iParticipantID,
			string strDescrip, 
			s.DateTime instant,
			int[] arrParticipantIDs,
			DTO.NWAccntDlt[] arrNWAccntDlts,
			DTO.Item[] arrItems,
			int[] arrWashAmounts,
			bool bValidate
			)
		{
			IList<DTO.NWAccntDlt>	lstNWAccntDlts	= new List<DTO.NWAccntDlt	>(arrNWAccntDlts);
			IList<DTO.Item>			lstItems		= new List<DTO.Item			>(arrItems		);
			return new Transaction(iID, aRowVersion, iPostID, iParticipantID, strDescrip, instant, arrParticipantIDs, lstNWAccntDlts, lstItems, arrWashAmounts, bValidate);
		}
		public static int GetAmountNWDltsExceedAllocDltsBy(
			TransactionNWAllocatedAccntDltUpdate aTransactionNWAllocatedAccntDltUpdate,
			TransactionItemUpdate aTransactionItemUpdate
		)
		{
			return Transaction.GetAmountNWDltsExceedAllocDltsBy(aTransactionNWAllocatedAccntDltUpdate.Get(), aTransactionItemUpdate.Get());
		}
		public static DTO.Transaction CreateSerializableTransaction(ITransaction dmnT)
		{
			if(dmnT==null)
				return null;
			return new DTO.Transaction(
			dmnT.ID,
			dmnT.TheRowVersion,
			dmnT.PostID,
			dmnT.ParticipantID,
			dmnT.Descrip,
			dmnT.Instant,
			new u.ROArray<int>(				dmnT.ParticipantIDs.ToArray()	),
			new u.ROArray<DTO.NWAccntDlt>(	dmnT.NWAccntDltsDeepCopy		),
			new u.ROArray<DTO.Item>(		dmnT.ItemsDeepCopy				),
			new u.ROArray<int>(				dmnT.WashAmounts.ToArray()		));
		}
		public static ITransaction CreateDomainTransation(DTO.Transaction siT)
		{
			if(siT==null)
				return null;
			return Create(
				siT.ID, 
				siT.TheRowVersion,
				siT.PostID, 
				siT.ParticipantID, 
				siT.Descrip, 
				siT.Instant, 
				siT.ParticipantIDs	.ToArray(), 
				siT.NWAccntDlts		.ToArray(), 
				siT.Items			.ToArray(), 
				siT.WashAmounts		.ToArray(), 
				true);
		}
	}
}

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