Click here to Skip to main content
15,897,371 members
Articles / Programming Languages / C#

Millennials - A Custom Source Code Generator

Rate me:
Please Sign up or sign in to vote.
4.90/5 (33 votes)
6 Jul 2012GPL34 min read 68K   7.8K   89  
Custom code generator for C# programming language. Supports MVC and three-tier architecture; ADO.NET, NHibernate and LINQ data access
using System;

namespace Millennials.Creator.BaseClass
{
    public static class DAOBaseClass
    {
        public const string baseClassTemplate =

        @"using System;
using System.Collections.Generic;
{0}

namespace {1}
{{
    /// <summary>
    /// {4}'s data access class
    /// </summary>
    public class {2} {5}
    {{
        /// <summary>
        /// Constructor
        /// </summary>
        public {2}(): base()
        {{
        }}

        {3}
    }}
}}";

        public const string usingTemplate = @"using {0};
";

        public const string paramCmdTemplate = @"cmd.Parameters.AddWithValue(""{0}"", {1});
                ";

        // 0 - return, 1 - method name, 2 - params, 
        // 3 - content, 4 - description, 5 - param
        public const string methodTemplate = @"
         /// <summary>
        /// Method responsible to {4}
        /// </summary>
        /// <param name=""{5}""></param>
        /// <returns></returns>
        public {0} {1}({2})
        {{
            {3}
        }}
";
        // 0 - table, 1 - condition, 2 - params
        public const string deleteCmdTemplate = @"
            conn = new SqlConnection(connectionString);
	        try
	        {{
	            conn.Open();
	            cmd = new SqlCommand(""delete from {0} where {1}"", conn);
				{2}
				
				cmd.ExecuteNonQuery();
	        }}
	        finally
	        {{
	            conn.Close();
	        }}
";
        // 0-table, 1-fields, 2-values, 3-params, 4-return var
        public const string insertCmdTemplate = @"conn = new SqlConnection(connectionString);
            SqlDataReader dr = null;
	        try 
			{{
				conn.Open();
				cmd = new SqlCommand(""insert into {0}({1}) values({2})"", conn);
	            {3}
				dr = cmd.ExecuteReader();
                var t = new CastDbType<{0}>();
                if (dr.HasRows)
                {{
                    {4} = t.Fill(dr)[0];
                }}
	        }}
	        finally 
			{{
                if (dr != null && !dr.IsClosed)
                {{
                    dr.Close();
                }}
	            conn.Close();
	        }}
			
			return {4};
";

        // 0-table, 1-fields, 2-condition, 3-params, 4- return var
        public const string editCmdTemplate = @"conn = new SqlConnection(connectionString);
            SqlDataReader dr = null;
	        try 
			{{
				conn.Open();
				cmd = new SqlCommand(""update {0} set {1} where {2}"", conn);
	            {3}
				dr = cmd.ExecuteReader();
                var t = new CastDbType<{0}>();
                if (dr.HasRows)
                {{
                    {4} = t.Fill(dr)[0];
                }}
	        }}
	        finally 
			{{
                if (dr != null && !dr.IsClosed)
                {{
                    dr.Close();
                }}
	            conn.Close();
	        }}
			
			return {4};
";


        // 0-table, 1-condition, 2-params, 3-type
        public const string listCmdTemplate = @"conn = new SqlConnection(connectionString);
            SqlDataReader dr = null;
            List<{3}> lstReturn = new List<{3}>();

	        try 
			{{
				conn.Open();
				cmd = new SqlCommand(""select * from {0} where {1}"", conn);
	            {2}
				dr = cmd.ExecuteReader();
                var t = new CastDbType<{0}>();
                if (dr.HasRows)
                {{
                    lstReturn = t.Fill(dr).ToList<{0}>();
                }}
	        }}
	        finally 
			{{
                if (dr != null && !dr.IsClosed)
                {{
                    dr.Close();
                }}
	            conn.Close();
	        }}
			
			return lstReturn;
";
        public const string nhibernateInsertTemplate = @"
            ISession session = NHibernateHelper.GetSession();
	        session.{0}({1});
	        session.Flush();
	        session.Close();
            {2}
";
        public const string nhibernateListCmdTemplate = @"
            ISession session = NHibernateHelper.GetSession();
            return session.CreateCriteria(typeof({0})).List<{0}>();
";

    }
}

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 GNU General Public License (GPLv3)


Written By
Engineer
Brazil Brazil
Computer engineer

Comments and Discussions