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
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 System.Text.RegularExpressions;
using SmartCode.Model;

namespace WebTemplates
{
    public class Helper
    {
        private static Regex cleanRegEx;
        static Helper()
        {
            cleanRegEx = new Regex(@"\s+|_|-|\.", RegexOptions.Compiled);
        }


        public static string ClassName(string name)
        {
            return MakeSingle(name);
        }

        public static string CleanName(string name)
        {
            return cleanRegEx.Replace(name, "");
        }

        public static string CamelCase(string name)
        {
            string output = CleanName(name);
            return char.ToLower(output[0]) + output.Substring(1);
        }

        public static string PascalCase(string name)
        {
            string output = CleanName(name);
            return char.ToUpper(output[0]) + output.Substring(1);
        }

        public static string MakePlural(string name)
        {
            Regex plural1 = new Regex("(?<keep>[^aeiou])y$");
            Regex plural2 = new Regex("(?<keep>[aeiou]y)$");
            Regex plural3 = new Regex("(?<keep>[sxzh])$");
            Regex plural4 = new Regex("(?<keep>[^sxzhy])$");

            if (plural1.IsMatch(name))
                return plural1.Replace(name, "${keep}ies");
            else if (plural2.IsMatch(name))
                return plural2.Replace(name, "${keep}s");
            else if (plural3.IsMatch(name))
                return plural3.Replace(name, "${keep}es");
            else if (plural4.IsMatch(name))
                return plural4.Replace(name, "${keep}s");

            return name;
        }

        public static string MakeSingle(string name)
        {
            Regex plural1 = new Regex("(?<keep>[^aeiou])ies$");
            Regex plural2 = new Regex("(?<keep>[aeiou]y)s$");
            Regex plural3 = new Regex("(?<keep>[sxzh])es$");
            Regex plural4 = new Regex("(?<keep>[^sxzhyu])s$");

            if (plural1.IsMatch(name))
                return plural1.Replace(name, "${keep}y");
            else if (plural2.IsMatch(name))
                return plural2.Replace(name, "${keep}");
            else if (plural3.IsMatch(name))
                return plural3.Replace(name, "${keep}");
            else if (plural4.IsMatch(name))
                return plural4.Replace(name, "${keep}");

            return name;
        }

        public static bool IsManyToManyTable(TableSchema table)
        {
            return (table.Columns().Count == 2 && table.HasPrimaryKey() && table.PrimaryKeyColumns().Count == 2 && table.InReferences.Count == 2);
        }

        public static bool IsChildFKColumn(ColumnSchema column, TableSchema table)
        {
            foreach (ReferenceSchema inReference in table.InReferences)
            {
                foreach (ReferenceJoin join in inReference.Joins)
                {
                    //Found the child Column...
                    if (join.ChildColumn.ObjectID == column.ObjectID)
                    {
                        return true;
                    }
                }
            }
            return false;
        }

        public static bool IsNullableType(ColumnSchema column)
        {
            return !column.IsRequired && column.NetDataType != "System.String";
        }

        public static string GetConvertToNETType(ColumnSchema  column, string strExpression)
        {
            switch (column.NetDataType)
            {
                case "System.Int32":
                    return "Convert.ToInt32(" + strExpression + ")";
                case "System.Byte":
                    return "Convert.ToByte(" + strExpression + ")";
                case "System.Boolean":
                    return "Convert.ToBoolean(" + strExpression + ")";
                case "System.Char":
                    return "Convert.ToChar(" + strExpression + ")";
                case "System.DateTime":
                    return "Convert.ToDateTime(" + strExpression + ")";
                case "System.Decimal":
                    return "Convert.ToDecimal(" + strExpression + ")";
                case "System.Double":
                    return "Convert.ToDouble(" + strExpression + ")";
                case "System.Int16":
                    return "Convert.ToInt16(" + strExpression + ")";
                case "System.Int64":
                    return "Convert.ToInt64(" + strExpression + ")";
                case "System.SByte":
                    return "Convert.ToSByte(" + strExpression + ")";
                case "System.Single":
                    return "Convert.ToSingle(" + strExpression + ")";
                case "System.TimeSpan":
                    return "Convert.ToDateTime(" + strExpression + ")";
                case "System.UInt16":
                    return "Convert.ToUInt16(" + strExpression + ")";
                case "System.UInt32":
                    return "Convert.ToUInt32(" + strExpression + ")";
                case "System.UInt64":
                    return "Convert.ToUInt64 (" + strExpression + ")";
                case "System.Guid":
                    return "Convert.ToString(" + strExpression + ")";
                case "System.Byte[]":
                    return "(Byte[])(" + strExpression + ")";
                default:
                    return "Convert.ToString(" + strExpression + ")";
            }

        }

        internal static string GetValueOrText(ColumnSchema column)
        {
            if (column.IsIdentity)
            {
                return "Value";
            }
            else
            {
                if (column.Control is SmartCode.Model.Profile.ComboBox)
                {
                    return "SelectedValue";
                }
                else if (column.Control is SmartCode.Model.Profile.CheckBox )
                {
                    return "Checked";
                }
                else
                {
                    return "Text";
                }
            }
        }

    }
}

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)

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 | Mobile
Web01 | 2.8.140721.1 | Last Updated 11 Dec 2007
Article Copyright 2007 by Danilo Mendez
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid