Click here to Skip to main content
15,896,269 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 44K   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.Proxy;
using Technewlogic.ObjectLounge.Framework.MetaModel;
using Technewlogic.ObjectLounge.Framework.BaseConcerns;
using Technewlogic.ObjectLounge.Framework.Transactions;
using System.Collections.ObjectModel;
using System.ComponentModel;

namespace Technewlogic.ObjectLounge.Framework.Validation
{
    public class ValidationConcern 
		: ConcernBase<IValidationConcern, IUnitOfWorkConcern>, IValidated, INotifyPropertyChanged
	{
		internal ValidationConcern(
			Type classType,
			TransactionManager transactionManager)
		{
			_transactionManager = transactionManager;
			_classType = classType;
		}

		private readonly TransactionManager _transactionManager;
		private readonly Type _classType;
		private readonly List<Rule> _rules = new List<Rule>();
		private readonly ObservableCollection<BrokenRule> _brokenRules = new ObservableCollection<BrokenRule>();

		#region IValidated Member

		public IEnumerable<BrokenRule> BrokenRules
		{
			get { return _brokenRules; }
		}

		public bool IsValid
		{
			get { return _brokenRules.Count == 0; }
		}

		#endregion

		// wird vom Proxy aufgerufen
		internal void RegisterValidationMethod(
			string validationMethodName,
			Func<ValidationResult> validationMethod)
		{
			var ruleID = _classType.Name + "." + validationMethodName;
			//var rule = Rule.TriggersAlways(ruleID, validationMethod);
			var rule = new Rule(ruleID, validationMethod);
			_rules.Add(rule);
		}

		private void TriggerRules(string propertyName) // Parameter for further use
		{
			TriggerRules();
		}

		internal void TriggerRules()
		{
			_brokenRules.Clear();
			var brokenRules = _transactionManager
				.ValidationTransactionService.Trigger(this.Parent, _rules);
			foreach (var it2 in brokenRules)
				_brokenRules.Add(it2);

			OnPropertyChanged("IsValid");
		}

		internal void OnPropertyChanging(string propertyName, object newValue, object oldVvalue)
		{
			// nothing
		}

		internal void OnPropertyChanged(string propertyName, object newValue, object oldVvalue)
		{
			// IMP: Transaction Lock
			TriggerRules(propertyName);
		}

		#region INotifyPropertyChanged Members

		public event PropertyChangedEventHandler PropertyChanged;
		private void OnPropertyChanged(string propertyName)
		{
			if (PropertyChanged != null)
				PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
		}

		#endregion
	}
}

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