Click here to Skip to main content
15,886,362 members
Articles / Programming Languages / C#

Attribute-flavored Domain Driven Design

Rate me:
Please Sign up or sign in to vote.
4.00/5 (2 votes)
6 Apr 2008CPOL8 min read 39.1K   231   25  
A centralized business domain with a common base
using System;
using System.Collections;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Runtime.Serialization;
using System.Xml.Serialization;
using Codebasement.DomainDrivenDesign.Basement.Attributes;
using Codebasement.DomainDrivenDesign.Basement.Basement;
using Codebasement.DomainDrivenDesign.Basement.Enums;
using Codebasement.DomainDrivenDesign.Model.ModelClasses;

namespace Codebasement.DomainDrivenDesign.Model.ModelClasses
{
    /// <summary>
    /// Contains a collection of <see cref="User"/> objects.
    /// </summary>
    [Serializable]
    [Description("Defines a collection of Users")]
    [XmlInclude(typeof (User))]
    [Relatable(typeof (User), 1, CardinalityType.ExactlyAsCardinality)]
    public sealed class Users : BasementObject
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="Users"/> class.
        /// </summary>
        public Users()
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="Users"/> class.
        /// </summary>
        /// <param name="info">The info.</param>
        /// <param name="context">The context.</param>
        private Users(SerializationInfo info, StreamingContext context) : this()
        {
            ConstructParameters(info);
        }

        /// <summary>
        /// Overridable core validation of a <see cref="BasementObject"/>. Usage intended for specific business model validations.
        /// </summary>
        protected override void ValidateCore()
        {
            /* BUSINESS RULE : User names must be unique */

            ArrayList listUsers = new ArrayList();
            Collection<BasementObject> collection = GetSubItems(typeof (User), true);

            if (collection.Count > 0)
            {
                foreach (User item in collection)
                {
                    if (!listUsers.Contains(item.Name))
                        listUsers.Add(item.Name);
                    else
                    {
                        // already present
                        string message = string.Format("Non-unique User name {0} in Users group.",
                                                       item.Name);
                        string solution = string.Format("Change the Name property of the User item.");
                        ValidationResult result = new ValidationResult(this, ValidationResultType.Error, message,
                                                                       solution);

                        AddValidationResult(result);
                    }
                }
            }
        }
    }
}

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 Code Project Open License (CPOL)


Written By
Software Developer (Senior)
Netherlands Netherlands
Software engineer & architect.

Comments and Discussions