Click here to Skip to main content
15,884,629 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 163.8K   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.Linq;
using System.Text;
//using sch = Schema;
using s = System;
using cub = ClientUtil;
//using ddal = DomainDAL;
using du = DomainUtil;
using u = Util;
using ns = Presenter;
using ui=UIInterface.Ownership;
using uib=UIInterface;
using si=ServiceInterface;
using uit=UIType.Ownership;
using gt=GlobalType;

namespace Presenter
{
	public class Ownership : C, ui.IPresenter
	{
		class DataForUI
		{
			public string[] Participants;
			public uit.SharesForAccount[] SharesForAccounts;
		}
		class DataFromDB
		{
			public u.ROArray<DTO.Cache.Ownership> Ownerships;
			public u.ROArray<DTO.Cache.NWAccnt> Accounts;
			public du.Participants Participants;
		}
		si.IService _Service;
		ui.IView _view;
		DataFromDB _DataFromDB;

		DataForUI _DataForUI;
		
		protected override UIInterface.IView GetView(){return _view;}
		static DataFromDB GetDataFromDB(si.IService ddal)
		{
			DataFromDB output = new DataFromDB();
			output.Participants = CreateDomainParticipants(ddal.GetParticipants());
			DTO.Cache aCache = ddal.GetCache();
			output.Ownerships = aCache.Ownerships;
			output.Accounts = aCache.NWAccnts;
			return output;
		}
		static DataForUI RepackageDataForUI(DataFromDB aDataFromDB)
		{
			DataForUI output = new DataForUI();
			cub.OwnershipDictionary aOwnershipDictionary = new ClientUtil.OwnershipDictionary(aDataFromDB.Ownerships, aDataFromDB.Participants);
			var lstSharesForAccounts = new List<uit.SharesForAccount>();
			foreach(DTO.Cache.NWAccnt aNWAccnt in aDataFromDB.Accounts)
			{
				uit.SharesForAccount o = new uit.SharesForAccount();
				o.Account = aNWAccnt.Name;
				o.Shares  = aOwnershipDictionary.GetParticipantShares(aNWAccnt.ID);
				lstSharesForAccounts.Add(o);
			}
			gt.IParticipant[] arrParticipantObjects = aDataFromDB.Participants.GetArryOfObjects();
			output.Participants	= arrParticipantObjects.Select(	p=>p.Name).ToArray();
			output.SharesForAccounts = lstSharesForAccounts.ToArray();
			return output;
		}
		static DTO.Ownership[] GetNewValues(DataFromDB aDataFromDB, ui.IView view)
		{
			List<DTO.Ownership> lstOwnerships = new List<DTO.Ownership>();
			ClientUtil.OwnershipDictionary aOwnershipDictionary = new ClientUtil.OwnershipDictionary(aDataFromDB.Ownerships, aDataFromDB.Participants);
			for (int iNWAccnt = 0; iNWAccnt < aDataFromDB.Accounts.Count; iNWAccnt++)
			{
				int iNWAccntID = aDataFromDB.Accounts[iNWAccnt].ID;
				int[] arrSharesLastRetreivedFromDB = aOwnershipDictionary.GetParticipantShares(iNWAccntID);
				for (int iPI = 0; iPI < aDataFromDB.Participants.Count; iPI++)
				{
					int iShareInView = view.GetShare(iPI, iNWAccnt);
					if (iShareInView == arrSharesLastRetreivedFromDB[iPI])
						continue;

					int iParticipantID = aDataFromDB.Participants.IDFromIndex(iPI);
					var dto = new DTO.Ownership(iNWAccntID, iParticipantID, iShareInView);
					lstOwnerships.Add(dto);
				}
			}
			return lstOwnerships.ToArray();
		}
		public Ownership(uib.IViewFactory theViewFactory, si.IService theService)
		{
			_Service = theService;
			_view = theViewFactory.CreateViewForOwnership();
			_view.SetPresenter(this);

			_DataFromDB = GetDataFromDB(_Service);
			_DataForUI = RepackageDataForUI(_DataFromDB);

		}
		public void Show()
		{
			_view.Show(_DataForUI.Participants, _DataForUI.SharesForAccounts);
		}
		
		public void LLSaveChangesDesired()
		{
			DTO.Ownership[] arr = GetNewValues(_DataFromDB, _view);
			if(arr==null)
				return;
			if(arr.Length==0)
				return;
			_Service.UpdateOwnerships(arr);

			DataFromDB aDataFromDB = GetDataFromDB(_Service);
			DataForUI aDataForUI = RepackageDataForUI(aDataFromDB);
			_view.ReLoad(aDataForUI.Participants, aDataForUI.SharesForAccounts);
			_DataFromDB = aDataFromDB;

		}
		public void SaveChangesDesired()
		{
			this.ExecuteWithErrorHandlingAndBusyIndicatorAndDoneMessage(LLSaveChangesDesired);
		}
	}
}

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