Click here to Skip to main content
15,884,472 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.Drawing;
using System.Windows.Forms;
using Codebasement.DomainDrivenDesign.Basement.Basement;
using Codebasement.DomainDrivenDesign.Basement.EventArguments;
using Codebasement.DomainDrivenDesign.Model.ModelClasses;
using Codebasement.DomainDrivenDesign.Model.Utilities;

namespace Codebasement.DomainDrivenDesign.Model.Factory
{
    /// <summary>
    /// 
    /// </summary>
    public static class ModelFactory
    {
        /// <summary>
        /// Creates the object.
        /// </summary>
        /// <param name="type">The type.</param>
        /// <param name="parentObject">The parent object.</param>
        /// <returns></returns>
        public static BasementObject CreateObject(
            Type type,
            BasementObject parentObject)
        {
            BasementObject newObject =
                Activator.CreateInstance(type) as BasementObject;

            if (newObject != null)
            {
                newObject.ObjectIdentifier = Guid.NewGuid();
                newObject.OnBasementObjectCreated(new BasementObjectEventArgs(newObject));
            }

            if (parentObject != null)
            {
                if (parentObject.Relatables.Contains(type))
                    parentObject.Relations.Add(newObject);
                else
                    throw new InvalidOperationException(
                        String.Format("Type {0} not relatable to object {1}",
                                      type, parentObject));
            }
            return newObject;
        }

        /// <summary>
        /// Removes the object.
        /// </summary>
        /// <param name="basementObject">The basement object.</param>
        /// <param name="parentObject">The parent object.</param>
        public static void RemoveObject(BasementObject basementObject, BasementObject parentObject)
        {
            if (!(parentObject != null &
                  basementObject != null)) return;
            parentObject.Relations.Remove(basementObject);
            basementObject.OnBasementObjectRemoved(new BasementObjectEventArgs(basementObject));
        }

        /// <summary>
        /// New container.
        /// </summary>
        /// <returns></returns>
        public static BasementObject NewContainer()
        {
            BasementObject basementObject =
                CreateObject(typeof (InformationContainer), null);

            InformationContainer container =
                basementObject as InformationContainer;
            if (container != null)
                container.Name = "Root";

            Customers customers =
                CreateObject(typeof(Customers), container) as Customers;

            Servers servers =
                CreateObject(typeof (Servers), container) as Servers;
            Server server =
                CreateObject(typeof (Server), servers) as Server;

            Users users =
                CreateObject(typeof(Users), container) as Users;

            return basementObject;
        }

        /// <summary>
        /// Gets the index of the image.
        /// </summary>
        /// <param name="type">The type.</param>
        /// <returns></returns>
        public static int GetImageIndex(Type type)
        {
            return ModelImages.GetImageIndex(type);
        }

        /// <summary>
        /// Gets the image.
        /// </summary>
        /// <param name="type">The type.</param>
        /// <returns></returns>
        public static Image GetImage(Type type)
        {
            return ModelImages.GetImage(type);
        }

        /// <summary>
        /// Gets the image list.
        /// </summary>
        /// <returns></returns>
        public static ImageList GetImageList()
        {
            return ModelImages.GetImageList();
        }

    }
}

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