Click here to Skip to main content
13,511,994 members
Click here to Skip to main content
Add your own
alternative version


28 bookmarked
Posted 6 Dec 2008


, 6 Dec 2008
Rate this:
Please Sign up or sign in to vote.
Class diagram and code generation tool.

Sample screens


Class diagrams are important in developing a model for a software system before you actually start programming your software. They provide designers, architects, and developers a graphical interface to manipulate methods, properties, fields, events for data structures like classes, interfaces, structs, and more. In this article, we will use what I call ModelStudio as a designer to help you create your model structures before directly diving into the code, and while we are interacting with this designer, it should be noted that the code will be updated in the background and later auto-generated using the System.CodeDom namespace.


I think that modeling your code first is a good first step in OOP and designing a great software application architecture. If you are looking for a Reflective solution, then you may want to take a look at something like Sacha Barber's 100% Reflective Class Diagram Creation Tool. For the diagrammer, which I won't discuss in this article, I modified and used the ClassDrawerContainerPanel and dependent classes from Barber's tool. I also want to acknowledge the use of Chagosoft's cDOMx Class Builder for ideas as well.

Using ModelStudio

The ModelStudio application uses the ModelStudio Framework as we refer to create Visual Basic and C# code documents. To use ModelStudio is fairly straightforward.

  1. When you start the program, you will be presented with a blank canvas.
  2. To start creating your first class, simply click the "New Namespace" button, give it a name, and a new Namespace object is created.
  3. Then, simply select the namespace from the left to add a "New Import" or "New Class".
  4. To add properties and methods to a class, simply select the required class and use the "New Property" or "New Method" buttons. To add parameters to methods is very simple as well; simply select the method from the left and click "New Parameter".
  5. When you are happy with your project, you have these options for getting the code:
    1. Use the "Generate Code" button to retrieve the code of the project in your desired language.
    2. Use the "Generate Files" button to generate the individual class documents in the selected folder in the desired language.
    3. You also have the option to save the project (Repository) as a file for future editing or transport using the "Save" button.
  6. To open a project file, use the "Open" button, browse to the file, and the project will be loaded.


There is one note that I need to add about my purpose for putting this resource tool together. Starting with Visual Studio 2005, Microsoft has included what they call the Class Designer. The Class Designer tool may work for your organization, especially if you have the super duper Visual Studio Team System. However, what I dislike about the Class Designer tool are the 'little things'. For instance, I like for all generated classes to initialize any custom types during class construction.

Books = New List(of Book)

Class Designer neglects to do that. Secondly, wouldn't it be nice to have completed setters and getters for all your properties along with the associated fields auto declared? Class Designer neglects to do this as well. Instead, what Class Designer provides you with is only a prototyped class property by default.

Public Overridable Property Name() As String

    End Get
    Set(ByVal value As String)

    End Set
End Property
Private m_Name As String

Public Overridable Property Name() As String
        Return Me.m_Name
    End Get
    Set(ByVal value As String)
        Me.m_Name = value
    End Set
End Property

The Framework

The ModelStudio Framework implements a simple design that first encapsulates the class information into a 'facade' of simple classes. Here we use the term facade in its most liberal sense. Definition: "a superficial appearance or illusion of something. (E.g.: They managed somehow to maintain a facade of wealth)". These facade classes and the data within each is then "translated", which we will discuss later, to create the actual CodeDom classes for code generation.

ModelStudio Framework



Description: This is an example facade class that will be used to create a new Imports (i.e., using) statement for your generated classes.

Public Class CodeDomImport
    Implements ICodeMember

    Private m_Import As String = "System.Text"
    Private m_Parent As Object

    ''' Initialize a new instance of the CodeDomImport object
    ''' The fully qualified Namespace that will be imported ie: System.Text
    Sub New(ByVal ImportName As String)
        m_Import = ImportName
    End Sub

    ''' The fully qualified Namespace that represents the import ie: System.text
    Public Property Name() As String Implements ICodeMember.Name
            Return m_Import
        End Get
        Set(ByVal value As String)
            m_Import = value
        End Set
    End Property

    Public Property Parent() As Object
            Return m_Parent
        End Get
        Set(ByVal value As Object)
            m_Parent= value
        End Set
    End Property

End Class

CodeGenerator Class

This is the business end of the whole thing. Here is where the actual code gets translated, prepared, and generated to code files.

Basically, the structure of this class is not ideal. However, for the purposes of this article, everything gets done inside this one class. Translation takes place when we return a regular CodeDOM CodeNamespace.


Public Function GetNamespace(ByVal _Namespace As CodeDomNamespace) As CodeNamespace
    Dim _space As CodeDomNamespace = _Namespace
    Dim _ns As CodeNamespace = _Namespace.DOMNameSpace

    If _space.Imports.Count > 0 Then
        For Each import As CodeDomImport In _space.Imports
            Dim im As New CodeNamespaceImport(import.Name)
    End If
End Function


Now we need to prepare the code for output the way we want it. During what we call the preparation stage, you will be using the following CodeDom namespaces:

Imports System.CodeDom
Imports System.CodeDom.Compiler

To prepare classes to initialize all custom types during class construction, we use the following code:

Dim mcdClass As CodeTypeDeclaration = _Class.DOMClass
For Each meth As CodeDomMethod In _Class.Constructors
    If meth.Parameters.Count > 0 Or _Class.CustomTypes.Count > 0 Then
        Dim cnstr As New CodeDom.CodeConstructor

        For Each param As CodeDomParameter In meth.Parameters
            cnstr.Parameters.Add(New _
                  CodeParameterDeclarationExpression(param.Type, param.Name))
        ' Here is where custom initialization begins
        For Each p As CodeDomProperty In _Class.CustomTypes
            Dim this As New CodeThisReferenceExpression()
            Dim leftExpression As New CodePropertyReferenceExpression(this, p.Name)
            Dim rightExpression As New _
                CodeObjectCreateExpression(New CodeTypeReference(p.Type))

            Dim assign As New CodeAssignStatement(leftExpression, rightExpression)

        Next p

    End If
Next meth

Now, you can change your constructor to initialize any type that you want by simply adjusting the above commands to meet your own individual preferences.


Here is where we actually output what we've translated and prepared. Obviously, we are going to generate VB code here, but there is also a similar C# generation method as well.

Private Sub GenerateVBFile(ByVal n As CodeNamespace, ByVal OutputDirectory As String)
        m_Unit = New CodeCompileUnit
    ' Generate the code with the VB code provider.
    Dim provider As Microsoft.VisualBasic.VBCodeProvider = _
        New Microsoft.VisualBasic.VBCodeProvider
    ' Build the output file name.

    For Each c As CodeDom.CodeTypeDeclaration In n.Types
        Dim tempUnit As New CodeCompileUnit
        tempUnit.Namespaces.Add(New CodeDom.CodeNamespace(n.Name))

        ' Build the output file name.
        Dim sourceFile As String = OutputDirectory + _
         Path.DirectorySeparatorChar + c.Name + "." + provider.FileExtension

        ' Create a TextWriter to a StreamWriter to an output file.
        Using tw As New IndentedTextWriter(New StreamWriter(sourceFile, False), "    ")

            ' Generate source code using the code provider.
            provider.GenerateCodeFromCompileUnit(tempUnit, tw, New CodeGeneratorOptions())

            ' Close the output file.
        End Using
    Next c

End Sub

Pretty simple, just sets up the private members and calls GenerateCodeFromCompileUnit from the VBCodeProvider and spits out a class, which should look like:

Namespace Classroom
Public Class Student

    Private m_Name As String
    Private m_StudentID As Integer
    Private m_MyTeacher As Teacher
    Private m_MyChair As Chair
    Private m_IsSpecial As Boolean
    Private m_Books As List(Of Book)

    Private Sub New()
        Me.MyTeacher = New Teacher
        Me.MyChair = New Chair
        Me.Books = New List(Of Book)
    End Sub

    Public Overridable Property Name() As [String]
            Return Me.m_Name
        End Get
            Me.m_Name = value
        End Set
    End Property
End Class
End Namespace

Wrap Up

The last part we have to do is import all of the generated classes into our project solution. Build it and run it. Congratulations, now the real work begins.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Terence Wallace
Founder Arkitech EBC Corporation
United States United States
MS, BBA, software developer, consultant, and trainer. Specializing in building data-centric applications designed for business, university, community & faith based organizations. Started developing Excel VBA macros and never looked back. Freelance developer utilizing VB.Net, SQL Server, Microsoft Access, and ASP.Net.

You may also be interested in...

Comments and Discussions

GeneralSub Classes Pin
aaroncampf28-Jan-11 13:55
memberaaroncampf28-Jan-11 13:55 
Generalseems good Pin
konikula20-Dec-09 12:30
memberkonikula20-Dec-09 12:30 
GeneralRe: seems good Pin
TL Wallace20-Dec-09 14:45
memberTL Wallace20-Dec-09 14:45 
GeneralNice post Pin
Sean Botha8-Dec-08 21:09
memberSean Botha8-Dec-08 21:09 
GeneralRe: Nice post Pin
TL Wallace9-Dec-08 2:16
memberTL Wallace9-Dec-08 2:16 
GeneralMy vote of 1 Pin
g0got27-Dec-08 0:38
memberg0got27-Dec-08 0:38 
GeneralRe: My vote of 1 Pin
Jerry Evans7-Dec-08 2:30
memberJerry Evans7-Dec-08 2:30 
Rant[Message Deleted] Pin
TL Wallace7-Dec-08 5:06
memberTL Wallace7-Dec-08 5:06 
GeneralRe: My vote of 1 Pin
Jerry Evans8-Dec-08 10:35
memberJerry Evans8-Dec-08 10:35 
GeneralRe: My vote of 1 Pin
TL Wallace9-Dec-08 2:21
memberTL Wallace9-Dec-08 2:21 
GeneralRe: My vote of 1 Pin
sngbrdb1-Jul-10 9:58
membersngbrdb1-Jul-10 9:58 

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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.180417.1 | Last Updated 6 Dec 2008
Article Copyright 2008 by Terence Wallace
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid