Click here to Skip to main content
15,891,844 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.9K   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 Technewlogic.ObjectLounge.Framework.Transactions;
using Technewlogic.ObjectLounge.Framework.Proxy;
using Technewlogic.ObjectLounge.Framework.BaseConcerns;
using System.Reflection;

namespace Technewlogic.ObjectLounge.Framework.Transactions
{
	/// <summary>
	/// Die ITransactionMapper Implementierung für den ShadowStorage-Concern.
	/// </summary>
	public class ShadowStorageTransactionMapper
	{
		internal ShadowStorageTransactionMapper(
			TransactionManager transactionManager)
		{
			_transactionManager = transactionManager;

			_transactionalStorage = new TransactionalStorage<IShadowStorageBehavior>(
				new ShadowStorageBehavior(),
				_transactionManager);

			_transactionManager.RollingBack
				+= new EventHandler<TransactionEventArgs>(_transactionManager_RollingBack);
			_transactionManager.Committing
				+= new EventHandler<TransactionEventArgs>(_transactionManager_Committing);
		}

		private readonly TransactionManager _transactionManager;
		private readonly TransactionalStorage<IShadowStorageBehavior> _transactionalStorage;

		internal void RegisterRollbackAction(string propertyName, Action<object> restoreValueMethod)
		{
			_transactionalStorage.Value
				.RegisterRollbackAction(propertyName, restoreValueMethod);
		}

		private void _transactionManager_RollingBack(object sender, TransactionEventArgs e)
		{
			if (_transactionalStorage.HasCurrentTransactionValue)
				_transactionalStorage.Value.RestorePropertyValues();
		}

		private void _transactionManager_Committing(object sender, TransactionEventArgs e)
		{
			if (_transactionalStorage.HasCurrentTransactionValue)
				_transactionalStorage.ClearOwnerScope();
		}

		/// <param name="value">Der Gespeicherte Propertywert (wenn True zurückgegeben wird).</param>
		/// <returns>
		/// True, wenn das angeforderte Property von einer anderen (nicht der aktuellen)
		/// noch laufenden Transaktion geschrieben wurde.
		/// </returns>
		internal bool TryGetCommittedValue<T>(string propertyName, out T value)
		{
			if (_transactionalStorage.HasDifferentTransactionValue)
			{
				if (_transactionalStorage.Value.BackupProperties.ContainsKey(propertyName))
				{
					value = (T)_transactionalStorage.Value.BackupProperties[propertyName];
					return true;
				}
			}

			value = default(T);
			return false;
		}

		internal void OnPropertyChanging(string propertyName, object newValue, object oldVvalue)
		{
			// Wird jetzt im TransactionManager global gemacht
			//if (_transactionalStorage.HasDifferentTransactionValue)
			//    Error.EntityInTransaction(this, _transactionalStorage.OwnerTransaction); // TODO: this ist nicht ganz korrekt, eigentlich müsste es die entity sein
		}

		internal void OnPropertyChanged(string propertyName, object newValue, object oldVvalue)
		{
			// Prüfung auf Korrektheit ist oben schon geschehen

			if (!_transactionalStorage.HasCurrentTransactionValue)
			{
				_transactionalStorage.SetOwnerScope();
				// IMP: An Transaction-Events anhängen für commit / rollback und den Concern löschen (evtl. im Storage machen)
			}

			_transactionalStorage.Value.HandlePropertySet(propertyName, oldVvalue);
		}
	}
}

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