Click here to Skip to main content
Click here to Skip to main content

Millennials - A Custom Source Code Generator

, 6 Jul 2012
Rate this:
Please Sign up or sign in to vote.
Custom code generator for C# programming language. Supports MVC and three-tier architecture; ADO.NET, NHibernate and LINQ data access
Millennials sample image

Introduction

The request for software development has grown significantly in the last years. Complex projects with tight deadlines are increasingly present in the software factories reality. When the project is not structured in an organized way from the beginning, it may face considerable problems in the future, as the risk of compromising the final product and even the own company.

In this article, I will introduce Millennials. The objective of this software is create automatically a .NET solution with custom source code with your desired software architecture.

Background

Automating repetitive coding tasks is a good way to save time in the software development lifecycle. In addition to creating an organized code following a development pattern, the time saved with these tasks can be used in another phase of software development lifecycle, like tests and design.

Millennials is a custom source code generator which user, based on a SQL Server database, can generate a .NET solution with the data models, data access layer and business layer through a wizard. In this wizard, the user can choose between two software architectures (MVC and three-tier) and three data access technologies (ADO .NET, Nhibernate and LINQ).

I developed it by myself in the last year of my computer engineering graduation. I also attached the monograph of this project (unfortunately, only in Portuguese). The link is on the top of the page, below the source code download.

Using Millennials

Millennials is a wizard-based software. It's very easy to create a .NET solution with your own custom source code. I will explain step by step how to use it.

Creating a New Project

In the main menu, click on "Project". After this, click on "New". The first screen is a form to connect into the SQL Server. You can choose which authentication will be used and once you are logged in, you can choose a database.

Step 1

In the next step, you will be able to select which tables and fields will be included in your .NET solution.

Step 2

In the next step, you will be able to input the .NET solution name, which project architecture and data access type will be used.

Step 3

In this last step, you will be able to select the .NET solution's destination folder and configure each .NET solution's project.

Step 4

Click in the Finish button and your .NET solution will be created.

Creating a New Template

In the creation project's last step, Millennials allows you to link each .NET solution's project to a template. Millennials has a default template for each project coding lines but you can create your own template. In this session, I will explain how to do it.

In the main menu, Click on "Template". After this, click on "New". This form allows you to create your own template using special tags mixed with C# coding lines.

Creating a new template

Static codes can be included in the template as normal C# coding lines. Variable codes are represented by special tags which Millennials will replace by the corresponding values in the creation process. There is a legend available in this screen that explains each tag and how to use it correctly.

The Code Behind Millennials

Millennials was developed using Microsoft Visual Studio 2008 and C#. It was organized in 6 projects:

  • Millennials.Business - Contains the class responsible to retrieve information from SQL Server database.
  • Millennials.Creator - Contains the class responsible to create the solution, projects and classes.
  • Millennials.Data - Contains the classes with the coding lines to retrieve information from SQL Server database.
  • Millennials.Model - Contains the model classes to help in the .NET solution creation process.
  • Millennials.UI - Contains the user interface forms.
  • Millennials.Util - Contains common coding lines for all projects as Enums.

Millennials Class Diagram

Class Diagram

Class Diagram

Points of Interest

To allow the user to see the coding lines like a C# editor in the template creation/edition step, I use the great ICSharpCode.TextEditor.

Create a .NET solution with projects within is easier than it looks. Files with extensions sln and csproj are just XML files linked to each other by a GUID. These coding lines are in Millennials.Creator project.

To retrieve the database's table information from SQL Server, I use the information_schema schema. These coding lines are in Millennials.Data project. Here is a code snippet:

public List<Column> ListColumnsByTable(Table table, SqlConnectionStringBuilder conn)
{
	List<Column> lstColumns = new List<Column>();
	using (SqlConnection dbConnection = new SqlConnection(conn.ConnectionString))
	{
		dbConnection.Open();
		StringBuilder strBuilder = new StringBuilder();
		SqlCommand cmd = new SqlCommand();

		strBuilder.Append("select ");
		strBuilder.Append("a.column_name as column_name, ");
		strBuilder.Append("CASE a.is_nullable ");
		strBuilder.Append("WHEN 'YES' THEN 1 ");
		strBuilder.Append("ELSE 0 ");
		strBuilder.Append("END as nullable, ");
		strBuilder.Append("a.data_type as type, ");
		strBuilder.Append("a.character_maximum_length as 
				character_maximum_length, ");
		strBuilder.Append("(SELECT count([sc].[name]) as possue ");
		strBuilder.Append("FROM sys.indexes [si] ");
		strBuilder.Append("JOIN ");
		strBuilder.Append("sys.index_columns [sic] ");
		strBuilder.Append("ON ");
		strBuilder.Append("[si].[object_id] = [sic].[object_id] AND ");
		strBuilder.Append("[si].[index_id] = [sic].[index_id] ");
		strBuilder.Append("JOIN ");
		strBuilder.Append("sys.columns [sc] ");
		strBuilder.Append("ON ");
		strBuilder.Append("[sic].[object_id] = [sc].[object_id] AND ");
		strBuilder.Append("[sic].[column_id] = [sc].[column_id] "); 
		strBuilder.Append("WHERE [si].[is_primary_Key] = 1 ");
		strBuilder.Append("and [sc].[name] = a.column_name ");
		strBuilder.Append("and OBJECT_NAME([si].[object_id]) = a.table_name ");
		strBuilder.Append(") as primary_key, ");
		strBuilder.Append("( ");
		strBuilder.Append("select ");
		strBuilder.Append("count(ccu.column_name) as possue ");
		strBuilder.Append
			("from information_schema.constraint_column_usage ccu ");
		strBuilder.Append
			("inner join information_schema.table_constraints tc "); 
		strBuilder.Append("on (ccu.constraint_name = tc.constraint_name) ");
		strBuilder.Append("where tc.Constraint_Type = 'FOREIGN KEY' ");
		strBuilder.Append("and ccu.table_name = a.table_name ");
		strBuilder.Append("and ccu.column_name = a.column_name ");
		strBuilder.Append(") as foreign_key ");
		strBuilder.Append("from information_schema.columns a ");
		strBuilder.Append("inner join  information_schema.tables b "); 
		strBuilder.Append("on (a.table_name = b.table_name) ");
		strBuilder.Append("where a.table_name = @tableName ");

		SqlParameter data = new SqlParameter("@tableName", SqlDbType.NChar);
		data.Value = table.Name;

		cmd.CommandText = strBuilder.ToString();
		cmd.Parameters.Add(data);
		cmd.CommandType = CommandType.Text;
		cmd.Connection = dbConnection;

		SqlDataReader reader = cmd.ExecuteReader();
		while (reader.Read())
		{
			Column column = new Column();
			column.ForeignKey = Convert.ToBoolean(reader["foreign_key"]);
			column.PrimaryKey = Convert.ToBoolean(reader["primary_key"]);
			if (reader["character_maximum_length"] != DBNull.Value)
			{
				column.CharacterMaximumLength = 
				Convert.ToInt32(reader["character_maximum_length"]);
			}
			column.Name = reader["column_name"].ToString();
			column.Nullable = Convert.ToBoolean(reader["nullable"]);
			column.Type = reader["type"].ToString();
			column.Table = table;
			lstColumns.Add(column);
		}
		dbConnection.Close();
	}

	return lstColumns;
}

Conclusion

I hope this software helps many developers around the world not just in the software development lifecycle but to start a new way of thinking about software development. We can save time with automatic coding lines and get more time to spend on creativity and innovation. I have some ideas for future release versions like expand it to other programming languages, help with layout creation, basic business flows... anyway, there are a lot of things to do and all of you are able to modify this software to help our developer's world wide community.

History

  • Version 1.0 released in November, 2010 (only in Portuguese)
  • Version 2.0 released in November, 2011 (English)

License

This article, along with any associated source code and files, is licensed under The GNU General Public License (GPLv3)

Share

About the Author

mauriciobarros
Engineer
Brazil Brazil
Computer engineer
Follow on   Twitter

Comments and Discussions

 
QuestionError upon building solution PinmemberAlino 34816-Dec-11 23:45 
AnswerRe: Error upon building solution Pinmembermauriciobarros7-Dec-11 1:00 
GeneralRe: Error upon building solution PinmemberAlino 34817-Dec-11 1:20 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web03 | 2.8.140827.1 | Last Updated 6 Jul 2012
Article Copyright 2011 by mauriciobarros
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid