Click here to Skip to main content
Click here to Skip to main content
Articles » Languages » C# » General » Downloads
 
Add your own
alternative version

NHibernate Templates for Smart Code Generator

, 11 Dec 2007 CPOL
Describes how to generate NHibernate objects and ASPX pages using Smart Code
nhibernate.zip
NHibernate
Everything-1.0.suo
Newin.scp
NHibernate
bin
Debug
NHibernateTemplates.dll
NHibernateTemplates.pdb
SmartCode.Model.DLL
SmartCode.Template.dll
mssccprj.scc
NHibernate.csproj.vspscc
NHibernateTemplates.csproj.user
Properties
vssver2.scc
vssver2.scc
Northwind
Northwind.Core
bin
Debug
Northwind.Core.dll
NorthwindDomain.dll
ProjectBase.Utils.dll
DataInterfaces
vssver2.scc
Domain
vssver2.scc
Hbm
vssver2.scc
mssccprj.scc
nhibernate-mapping.xsx
Northwind.Core.csproj.vspscc
Properties
vssver2.scc
vssver2.scc
Northwind.Data
bin
Debug
Northwind.Core.dll
Northwind.Data.dll
ProjectBase.Data.dll
ProjectBase.Utils.dll
Manager
vssver2.scc
mssccprj.scc
Northwind.Data.csproj.vspscc
Properties
vssver2.scc
vssver2.scc
Northwind.Web
bin
Castle.DynamicProxy.dll
Iesi.Collections.dll
Northwind.Core.dll
Northwind.Data.dll
Northwind.Web.dll
ProjectBase.Utils.dll
mssccprj.scc
Northwind.Web.csproj.user
Northwind.Web.csproj.vspscc
Properties
vssver2.scc
Utils
vssver2.scc
vssver2.scc
ProjectBase.Utils
bin
Debug
log4net.dll
ProjectBase.Utils.dll
ProjectBase.Utils.pdb
mssccprj.scc
ProjectBase.Utils.csproj.vspscc
Properties
vssver2.scc
vssver2.scc
Web
vssver2.scc
WebTemplates
bin
Debug
SmartCode.Model.DLL
SmartCode.Template.dll
WebTemplates.dll
WebTemplates.pdb
Edit
vssver2.scc
List
vssver2.scc
mssccprj.scc
obj
Debug
Refactor
ResolveAssemblyReference.cache
TempPE
WebTemplates.dll
WebTemplates.pdb
Properties
vssver2.scc
vssver2.scc
WebTemplates.csproj.user
WebTemplates.csproj.vspscc
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)

Share

About the Author

Danilo Mendez
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.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.1411028.1 | Last Updated 11 Dec 2007
Article Copyright 2007 by Danilo Mendez
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid