Click here to Skip to main content
15,881,455 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.6K   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.Linq.Expressions;
using System.Collections;
using System.Reflection;
using Technewlogic.ObjectLounge.Helper;
using Technewlogic.ObjectLounge.Framework.Proxy;
using Technewlogic.ObjectLounge.Framework.Transactions;

namespace Technewlogic.ObjectLounge.Framework.BaseConcerns
{
	internal abstract class OneBase : EntitySync
	{
		public OneBase(IUnitOfWorkConcern master)
			: base(master)
		{
		}

		#region EntitySync

		public override bool HasValue(object value)
		{
			if (value == null)
				throw new ArgumentNullException("Value");

			var currentValue = GetValue();
			if (currentValue == null)
				return false;
			else
				return currentValue.Equals(value);
		}

		#endregion

		protected override void OnStateChange()
		{
			base.OnStateChange();

			if (GetValue() != null)
				_strategy.StateChange(GetValue(), WillBeInDB);
		}

		protected abstract Type GetTargetType();

		private bool _isInitialized = false;

		private IUnitOfWorkConcern _innerValue;
		private IUnitOfWorkConcern InnerValue
		{
			get
			{
				//// Lazy initializing
				//if (!_isInitialized)
				//    Initialize();

				return _innerValue;
			}
			set { _innerValue = value; }
		}

		internal IUnitOfWorkConcern GetValue()
		{
			return InnerValue;
		}

		internal void SetValue(IUnitOfWorkConcern value)
		{
			if (InnerValue == value)
				return;

			OnBeforeValueSet(value);

			if (InnerValue != null)
				_strategy.Remove(InnerValue, WillBeInDB);
			if (value != null)
				_strategy.Add(value, WillBeInDB);

			InnerValue = value;
		}

		//private void Initialize()
		//{
		//    EntityCollection ec = _entityContext.GetEntityCollection(GetTargetType());
		//    var value = MasterIsChild()
		//        ? ec.GetEntityByForeignKeyFieldOfEntity(_master)
		//        : ec.GetEntityByPrimaryKeyPropertyOfEntity(_master);

		//    //_innerValue = new ShadowStorageRef<IUnitOfWorkConcern>(value, _master);
		//    _innerValue = value;

		//    _isInitialized = true;
		//}

		protected abstract void OnBeforeValueSet(IUnitOfWorkConcern value);

		internal override void AddTarget(IEnumerable<IUnitOfWorkConcern> children)
		{
			if (_isInitialized)
				throw new ApplicationException("The entity is already initialized."); // IMP: besser machen
			if (children.Count() > 1)
				throw new ApplicationException("In a OneBase, only 1 child can be added at initialization.");

			_innerValue = children.FirstOrDefault();

			if (_innerValue != null && GetTargetType() != _innerValue.GetType().BaseType)
				throw new ApplicationException("The expected type is not the initialization type.");

			_isInitialized = true;
		}

		internal override IEnumerable<IUnitOfWorkConcern> GetTargets()
		{
			if (InnerValue == null)
				return new IUnitOfWorkConcern[] { };
			else
				return new IUnitOfWorkConcern[] { (IUnitOfWorkConcern)InnerValue };
		}
	}
}

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