Click here to Skip to main content
15,884,388 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.CodeDom;
using System.CodeDom.Compiler;
using System.IO;
using System.Reflection;
using Technewlogic.ObjectLounge.Framework.Transactions;
using Technewlogic.ObjectLounge.Framework.MetaModel;
using Technewlogic.ObjectLounge.Framework.BaseConcerns;
using Technewlogic.ObjectLounge.Framework.Validation;

namespace Technewlogic.ObjectLounge.Framework.Proxy
{
	public partial class ProxyGenerator
	{
		public ProxyGenerator(
			ModelSpec modelSpec,
			TransactionManager transactionManager)
		{
            _modelSpec = modelSpec;
			_transactionManager = transactionManager;

			DynamicProxyAssembly = GenerateAssembly();

			foreach (var it in _modelSpec.EntitySpecs.Values)
			{
				Type proxyType = DynamicProxyAssembly.GetTypes()
					.Single(it2 => it2.BaseType == it.ClassType);

				it.UpdateTriggerPropertyNames = proxyType
					.GetProperties(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance)
					.Where(it2 => it2.PropertyType != typeof(EntitySync))
					.Where(it2 => !it2.PropertyType.IsSubclassOf(typeof(EntitySync)))
					.Select(it2 => it2.Name)
					.ToArray();
			}
		}

		private readonly ModelSpec _modelSpec;
		private readonly TransactionManager _transactionManager;

		public Assembly DynamicProxyAssembly { get; private set; }

		public T CreateProxy<T>(
			SynchronizationOperation initialSynchronizationOperation)
			where T : class, new()
		{
			return (T)CreateProxy(typeof(T), initialSynchronizationOperation);
		}

		public IProxy CreateProxy(
			Type type,
			//EntityCollection entityCollection,
			SynchronizationOperation initialSynchronizationOperation)
		{
			// TODO: Assert, dass die Typen gleich sind von type und origi.

			Type proxyType = DynamicProxyAssembly.GetTypes()
				.FirstOrDefault(it => it.BaseType == type);

			// Zwar dürfte das nicht passieren, wenn der generator von dem ObjectLounge-Framework aus bedient wird. Wird er aber einzeln benutzt, so muss das hier abgefangen werden.
			if (proxyType == null)
				throw Error.EntityTypeNotRegistered(type);

			var transactionDispatcher = new TransactionDispatcher(
				_modelSpec,
				initialSynchronizationOperation,
				_transactionManager,
				type);

			// TODO: Abfangen, wenn kein parameterloser ctor vorhanden ist
			var proxy = (IProxy)Activator.CreateInstance(
				proxyType,
				new object[] { transactionDispatcher });
			transactionDispatcher.Parent = proxy;

			return proxy;
		}
	}
}

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