Click here to Skip to main content
15,867,568 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 66.9K   7.8K   89   18
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:

C#
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)


Written By
Engineer
Brazil Brazil
Computer engineer

Comments and Discussions

 
GeneralMy vote of 5 Pin
Member 1135957514-Jun-16 15:32
Member 1135957514-Jun-16 15:32 
GeneralMy vote of 5 Pin
manoj.jsm15-May-13 1:46
manoj.jsm15-May-13 1:46 
QuestionTest on Northwind database failed. Pin
Highflyer20-Apr-13 5:56
Highflyer20-Apr-13 5:56 
GeneralMy vote of 5 Pin
rajn_mca6-Feb-13 21:26
rajn_mca6-Feb-13 21:26 
great
GeneralMy vote of 5 Pin
Kanasz Robert5-Nov-12 2:42
professionalKanasz Robert5-Nov-12 2:42 
GeneralMy vote of 5 Pin
Dev S3-Sep-12 0:23
Dev S3-Sep-12 0:23 
Questionbinary and image data types Pin
Member 30205429-Jul-12 4:01
Member 30205429-Jul-12 4:01 
QuestionImprove support t4 template Pin
jun chan7-Jul-12 1:47
jun chan7-Jul-12 1:47 
AnswerRe: Improve support t4 template Pin
mauriciobarros8-Jul-12 11:24
mauriciobarros8-Jul-12 11:24 
Question5 is 5! Pin
Aamer Alduais6-Jul-12 19:22
Aamer Alduais6-Jul-12 19:22 
GeneralRe: 5 is 5! Pin
mauriciobarros8-Jul-12 11:22
mauriciobarros8-Jul-12 11:22 
GeneralMy vote of 5 Pin
Md. Marufuzzaman1-Jan-12 17:30
professionalMd. Marufuzzaman1-Jan-12 17:30 
QuestionError upon building solution Pin
Alino 34816-Dec-11 23:45
Alino 34816-Dec-11 23:45 
AnswerRe: Error upon building solution Pin
mauriciobarros7-Dec-11 1:00
mauriciobarros7-Dec-11 1:00 
GeneralRe: Error upon building solution Pin
Alino 34817-Dec-11 1:20
Alino 34817-Dec-11 1:20 
GeneralMy vote of 5 Pin
_Tushar Patil23-Nov-11 18:11
_Tushar Patil23-Nov-11 18:11 
QuestionGood Job Pin
Rafael_Teodoro23-Nov-11 8:01
Rafael_Teodoro23-Nov-11 8:01 
GeneralMy vote of 5 Pin
Pablo Aliskevicius23-Nov-11 4:39
Pablo Aliskevicius23-Nov-11 4:39 

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

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