Click here to Skip to main content
15,885,216 members
Articles / Web Development / ASP.NET

NHibernate Templates for Smart Code Generator

Rate me:
Please Sign up or sign in to vote.
4.77/5 (20 votes)
11 Dec 2007CPOL5 min read 203K   1.9K   130  
Describes how to generate NHibernate objects and ASPX pages using Smart Code
using System;
using System.Collections.Generic;
using System.Text;
using SmartCode.Template;
using SmartCode.Model;

namespace NHibernateTemplates
{
    public class DomainObject : TemplateBase
    {
        public DomainObject()
        {
            CreateOutputFile = true;
            IsProjectTemplate = true;
            Description = "Generates the DomainObject Base Class";
            Name = "DomainObject";
            OutputFolder = "NHibernate/Domain";
        }

        public override string OutputFileName()
        {
            return "DomainObject.cs";
        }

        public override void ProduceCode()
        {
            WriteLine(@"using System;");
            WriteLine(@"using System.Collections.Generic;");
            W();
            WriteLine(@"namespace {0}.Core.DataInterfaces", Helper.PascalCase(Domain.Code));

            WriteLine(@"{");
            WriteLine(@"  /// <summary>");
            WriteLine(@"/// For a discussion of this object, see ");
            WriteLine(@"/// http://devlicio.us/blogs/billy_mccafferty/archive/2007/04/25/using-equals-gethashcode-effectively.aspx");
            WriteLine(@"/// </summary>");
            WriteLine(@"public abstract class DomainObject<IdT>");
            WriteLine(@"{");
            WriteLine(@"    private IdT id = default(IdT);");
            W();
            WriteLine(@"    /// <summary>");
            WriteLine(@"    /// ID may be of type string, int, custom type, etc.");
            WriteLine(@"    /// Setter is protected to allow unit tests to set this property via reflection and to allow ");
            WriteLine(@"    /// domain objects more flexibility in setting this for those objects with assigned IDs.");
            WriteLine(@"    /// </summary>");
            WriteLine(@"    public IdT ID {");
            WriteLine(@"        get { return id; }");
            WriteLine(@"        protected set { id = value; }");
            WriteLine(@"    }");
            W();
            WriteLine(@"    public override sealed bool Equals(object obj) {");
            WriteLine(@"        DomainObject<IdT> compareTo = obj as DomainObject<IdT>;");
            W();
            WriteLine(@"        return (compareTo != null) &&");
            WriteLine(@"               (HasSameNonDefaultIdAs(compareTo) ||");
            WriteLine(@"                // Since the IDs aren't the same, either of them must be transient to ");
            WriteLine(@"                // compare business value signatures");
            WriteLine(@"                (((IsTransient()) || compareTo.IsTransient()) &&");
            WriteLine(@"                 HasSameBusinessSignatureAs(compareTo)));");
            WriteLine(@"    }");
            W();
            WriteLine(@"    /// <summary>");
            WriteLine(@"    /// Transient objects are not associated with an item already in storage.  For instance,");
            WriteLine(@"    /// a <see cref=""Customer"" /> is transient if its ID is 0.");
            WriteLine(@"    /// </summary>");
            WriteLine(@"    public bool IsTransient() {");
            WriteLine(@"        return ID == null || ID.Equals(default(IdT));");
            WriteLine(@"    }");
            W();
            WriteLine(@"    /// <summary>");
            WriteLine(@"    /// Must be provided to properly compare two objects");
            WriteLine(@"    /// </summary>");
            WriteLine(@"    public abstract override int GetHashCode();");
            W();
            WriteLine(@"    private bool HasSameBusinessSignatureAs(DomainObject<IdT> compareTo) {");
            WriteLine(@"        Check.Require(compareTo != null, ""compareTo may not be null"");");
            W();
            WriteLine(@"        return GetHashCode().Equals(compareTo.GetHashCode());");
            WriteLine(@"    }");
            W();
            WriteLine(@"    /// <summary>");
            WriteLine(@"    /// Returns true if self and the provided persistent object have the same ID values ");
            WriteLine(@"    /// and the IDs are not of the default ID value");
            WriteLine(@"    /// </summary>");
            WriteLine(@"    private bool HasSameNonDefaultIdAs(DomainObject<IdT> compareTo) {");
            WriteLine(@"        Check.Require(compareTo != null, ""compareTo may not be null"");");
            W();
            WriteLine(@"        return (ID != null && ! ID.Equals(default(IdT))) &&");
            WriteLine(@"               (compareTo.ID != null && ! compareTo.ID.Equals(default(IdT))) &&");
            WriteLine(@"               ID.Equals(compareTo.ID);");
            WriteLine(@"    }");
            W();
            WriteLine(@"}");
            WriteLine(@"}");

        }
    }
}

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
Web Developer
United States United States
Danilo is the creator of SmartRules, a Business Rules Engine. He is an industry consultant working primarily with companies interested in implementing dynamic rules programming concepts to add flexibility to their architectures on web, CE, and desktop platforms. He operates his own website, Kontac, where you will find more information.

To contact Danilo, email him at danilo.mendez@gmail.com.

Comments and Discussions