Click here to Skip to main content
15,885,278 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 Technewlogic.ObjectLounge;
using System.Data;
using System.Data.Linq;
using System.Data.Linq.Mapping;
using Technewlogic.ObjectLounge.Framework.Proxy;
using Technewlogic.ObjectLounge.Framework.Validation;

namespace Technewlogic.ObjectLounge.Test.Model
{
	[Identity("_key")]
	public class Customer : BOBase
	{
		#region Constants

		public const string NewName = "New Customer";
		public const string SKName = "S&K Anlagentechnik";
		public const string PizzaInderName = "Pizza Express";

		#endregion

		public Customer()
		{
		}

		public Guid _key = Guid.NewGuid();

		public int _accountNumber;
		public virtual int AccountNumber
		{
			get
			{
				return _accountNumber;
			}
			set
			{
				_accountNumber = value;
				SendPropertyChanged("AccountNumber");
			}
		}

		public string _name;
		public virtual string Name
		{
			get
			{
				return _name;
			}
			set
			{
				_name = value;
				SendPropertyChanged("Name");
			}
		}

		[ValidationRule]
		protected ValidationResult ValidateAccountNumber()
		{
			if (Context.EnableValidation)
			{
				if (_accountNumber > 0 && _accountNumber < 1000)
					return ValidationResult.Success();
				else
					return ValidationResult.Failure("Invalid account number.");
			}

			return ValidationResult.Success();
		}

		public bool ContextAccessRuleTriggered;

		[ValidationRule]
		protected ValidationResult ValidateContextAccess()
		{
			ContextAccessRuleTriggered = true;

			if (Context.EnableValidation)
			{
				var unique = !Context.Customers
					.Where(it => it != this)
					.Any(it => it.AccountNumber == this.AccountNumber);

				if (unique)
					return ValidationResult.Success();
				else
					return ValidationResult.Failure("Account number is not unique.");
			}

			return ValidationResult.Success();
		}

		[ValidationRule]
		protected ValidationResult ValidateName()
		{
			if (Context.EnableValidation)
			{
				// Macht Probleme bei alles Tests, da das Framework nun automatisch validiert
				if (string.IsNullOrEmpty(_name))
					return ValidationResult.Failure("Customer has no name.");
				else
					return ValidationResult.Success();
			}

			return ValidationResult.Success();
		}

		[Composition]
		public virtual IList<Order> Orders { get; protected set; }

		public override string ToString()
		{
			return this.GetType().Name + ": " + Name;
		}
	}
}

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