Click here to Skip to main content
15,884,537 members
Articles / Programming Languages / C#

ObjectLounge - An Object-Oriented Database Framework

Rate me:
Please Sign up or sign in to vote.
4.07/5 (8 votes)
30 Jul 2009LGPL310 min read 43.7K   425   45  
Host your Domain Models "Out Of The Box" + No storage or schema needed + Transactions on Domain Models + Validation
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using Technewlogic.ObjectLounge.SyncProvider;
using Technewlogic.ObjectLounge.Framework.Proxy;
using Technewlogic.ObjectLounge.Framework.BaseConcerns;
using Technewlogic.ObjectLounge.SyncProvider.SqlCE;

namespace Technewlogic.ObjectLounge.Test.Model
{
	public class DataContextMockSyncProvider : ISyncProvider
	{
		private DataContextMock _dataContextMock;

		public DataContextMockSyncProvider(DataContextMock dataContextMock)
		{
			_dataContextMock = dataContextMock;

			Inserted = new List<IUnitOfWorkConcern>();
			Updated = new List<IUnitOfWorkConcern>();
			Deleted = new List<IUnitOfWorkConcern>();
		}

		public List<IUnitOfWorkConcern> Inserted { get; private set; }
		public List<IUnitOfWorkConcern> Updated { get; private set; }
		public List<IUnitOfWorkConcern> Deleted { get; private set; }

		#region ISyncProvider Members

		public void BeginRegisterChanges() { }

		public void Initialize(IInstanceFactory factory, Linker linker)
		{
			var flatList = _dataContextMock
				.Customers.Cast<object>()
				.Concat(_dataContextMock
					.Materials.Cast<object>())
				.Concat(_dataContextMock
					.Orders.Cast<object>())
				.Concat(_dataContextMock
					.OrderTypeCompositors.Cast<object>())
				.Concat(_dataContextMock
					.OrderTypes.Cast<object>());

			Dictionary<object, IProxy> originalToProxyLinks = new Dictionary<object, IProxy>();
			foreach (var it in flatList)
			{
				var proxy = factory.CreateInstance(it.GetType());
				CopyValues(proxy, it);
				originalToProxyLinks.Add(it, proxy);
			}

			// Build the entity relationships
			foreach (var it in _dataContextMock.Connections)
			{
				var sourceProxy = originalToProxyLinks[it.Key];
				var targetProxies = it.Value
					.Select(it2 => originalToProxyLinks[it2]);
				var targetProxiesGrouped = targetProxies
					.GroupBy(it2 => it2.GetType());

				List<RelationshipHolder> rhs = new List<RelationshipHolder>();
				foreach (var it2 in targetProxiesGrouped)
				{
					var targets = it2.Select(it3 => it3.KeyManagementConcern.EntityID);
					rhs.Add(new RelationshipHolder(
						it2.Key.BaseType, targets));
				}
				linker.RegisterRelationships(new EntityRelationship(sourceProxy, rhs));
			}
		}

		public void PersistChanges(
			IEnumerable<IUnitOfWorkConcern> insertedEntities,
			IEnumerable<IUnitOfWorkConcern> updatedEntities,
			IEnumerable<IUnitOfWorkConcern> deletedEntities,
			Linker linker)
		{
			Insert(insertedEntities);
			Update(updatedEntities);
			Delete(deletedEntities);
		}

		public void Close()
		{
		}

		#endregion

		private void Insert(IEnumerable<IUnitOfWorkConcern> entities)
		{
			foreach (var item in entities)
			{
				Type t = item.GetType();

				if (IsType(t, typeof(Customer)))
					_dataContextMock.Customers.Add((Customer)item);
				else if (IsType(t, typeof(Order)))
					_dataContextMock.Orders.Add((Order)item);
				else if (IsType(t, typeof(OrderType)))
					_dataContextMock.OrderTypes.Add((OrderType)item);
				else if (IsType(t, typeof(Material)))
					_dataContextMock.Materials.Add((Material)item);
				else if (IsType(t, typeof(OrderTypeCompositor)))
					_dataContextMock.OrderTypeCompositors.Add((OrderTypeCompositor)item);
				else
					throw new ApplicationException("The type is unknown");
			}

			Inserted.AddRange(entities);
		}

		private void Update(IEnumerable<IUnitOfWorkConcern> entities)
		{
			Updated.AddRange(entities);
		}

		private void Delete(IEnumerable<IUnitOfWorkConcern> entities)
		{
			foreach (var item in entities)
			{
				Type t = item.GetType();

				if (IsType(t, typeof(Customer)))
					_dataContextMock.Customers.Remove((Customer)item);
				else if (IsType(t, typeof(Order)))
					_dataContextMock.Orders.Remove((Order)item);
				else if (IsType(t, typeof(OrderType)))
					_dataContextMock.OrderTypes.Remove((OrderType)item);
				else if (IsType(t, typeof(Material)))
					_dataContextMock.Materials.Remove((Material)item);
				else if (IsType(t, typeof(OrderTypeCompositor)))
					_dataContextMock.OrderTypeCompositors.Remove((OrderTypeCompositor)item);
				else
					throw new ApplicationException("The type is unknown");
			}

			Deleted.AddRange(entities);
		}

		private void CopyValues(IProxy instance, object originalInstance)
		{
			// Copy the original fields to the proxy object
			var helper = new XmlFieldSerializerHelper();
			var dataFields = helper.GetSerializationFields(instance.GetType()); // Extension-method des SqlCE Providers
			foreach (var field in dataFields)
			{
				var originalValue = field.GetValue(originalInstance);
				field.SetValue(instance, originalValue);
			}
		}

		private bool IsType(Type me, Type t)
		{
			return me == t || me.IsSubclassOf(t);
		}
	}
}

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 GNU Lesser General Public License (LGPLv3)


Written By
Software Developer (Senior) www.technewlogic.de
Germany Germany
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions