You are here
Home > Reviewed > CodeSmith Generator Projects And Visual Studio

CodeSmith Generator Projects And Visual Studio

CodeSmith Generator Projects

CodeSmith Generator Projects allow developers to manage a batch of CodeSmith Generator templates and outputs. All this is done in a single CodeSmith Generator Project file (.csp). This is great for when you need to generate several code templates that are logically grouped together by their implementation. Think of data layers that can be generated with a single click to create multiple classes driven from a specific database. The possibilities are endless. In this article I will show you a simple implementation of a CodeSmith Generator Project.

CodeSmith Generator Projects

Let’s jump straight in and fire up Visual Studio. I am using Visual Studio Ultimate 2013. Go ahead and right click ‘My Templates’. Create a new CodeSmith Generator Project.

CodeSmith Generator Projects

Rename the created file to a more suitable name. In the previous article I created a business object class from a single template. In this article I will create multiple classes from this single template file using the created CodeSmith Project.

CodeSmith Generator Projects

Right click the project file and click on ‘Manage Outputs’ from the context menu.

CodeSmith Generator Projects

From the Manage Outputs screen, click on the ‘Add Output’ menu button.

CodeSmith Generator Projects

The Add Output screen will open. This screen allows you to define the specific output which is based on one of your previously created templates. I will be using the previously created SYSPRO Business Object.cst template.

CodeSmith Generator Projects

I gave the output a specific name. In this output I want to create a Purchase Order class based on my template. I therefore called this output ‘Purchase Order’. Under the template section, I selected the template I previously created. For the file, I selected the Visual Studio project path and gave the file an appropriate name. If you use the default name or don’t change the file name, the output will be created in the default templates path as defined in the CodeSmith options. Lastly, the properties are then available for you to define  accordingly. You can see I called this class PORTOI. After you save this output, you can go back and edit it if needs be later on.

CodeSmith Generator Projects

When you click save, the output is added to the Manage Outputs screen. It will also show you the template that each specific output is based on.

CodeSmith Generator Projects

Next, I’ll copy the existing Purchase Order output by clicking on the Copy Output menu button.

CodeSmith Generator Projects

This creates a copy of the Purchase Order output and CodeSmith Generator appropriately renames the output.

CodeSmith Generator Projects

Next, click on Edit Output to change the copied Purchase Order output.

CodeSmith Generator Projects

First off, rename the output name. This time I am going to create a Sales Order class based on my template file. Therefore I called this output Sales Order. As before I used the same template but you could logically use a different template file if you need to generate a different class. For the file path, I again selected the output to be the project folder. As with purchase orders, I edited the properties appropriate to sales orders. The class name is also now called SORTOI.

CodeSmith Generator Projects

After saving the Sales Order output, you will see it added to the Manage Outputs screen. You can continue doing this for multiple different classes using the same or different templates.

CodeSmith Generator Projects

Just a quick note on the Manage Output menu. If you select a single or several output items in the list, you can generate just those output(s). If you click on the Generate Outputs button, CodeSmith Generator will execute all the outputs defined in the project file. You can also move the outputs around (up or down the list). Lastly you can change the project options.

CodeSmith Generator Projects

For my project, I want to create a separate file for each template. I have therefore selected multiple files as an option. You could combine the various outputs into a single file if you wanted to. You can then define header detail (for example namespace declaration and using statements) and footer detail (closing C# braces for example). You can also define default properties and variables, but I will not be going into this in this review. You can refer to the detailed CodeSmith Generator documentation here.

CodeSmith Generator Projects

Once you have completed editing and configuring the project, go ahead and click on ‘Generate Outputs’.

CodeSmith Generator Projects

CodeSmith starts to render the outputs based upon your project file. When it has completed you can view the details if you wish.

CodeSmith Generator Projects

Next I clicked on my Visual Studio project and added the created files as existing items to my visual studio project.

CodeSmith Generator Projects

You can see the SORTOI and PORTOI classes added to the project. Go ahead and build the solution and see if it compiles.

CodeSmith Generator Projects

Having a look at the purchase order and sales order code files side by side, you can see that they are similar in structure but totally different in implementation.

CodeSmith Generator Projects

Adding CodeSmith Generator Projects Directly To Visual Studio

Lastly, there is another way to use CodeSmith Generator Projects. Right click on the Visual Studio project and add a new item. From this screen you will be able to add a CodeSmith Generator Project directly to your Visual Studio project.

1 add new item

You will see the CodeSmith Project added to your Visual Studio Project.

2 csp project added

The code view for the CodeSmith Project is now opened in Visual Studio.

2a csp xml

As described above, right click the csp file and manage the outputs for the CodeSmith Project. I just used the same template file and added two new classes to the outputs.

3 Manage Outputs

After saving the project outputs you will see the code view of the CodeSmith project has been modified and contains the output settings. You can actually just go ahead and close this file in Visual Studio because you aren’t going to be modifying this file directly. So whenever you change your outputs, Visual Studio might pester you to reload the file.

4 csp xml

Next you right click the CodeSmith Generator Project and select the ‘Generate On Build’ from the Output Options context menu. This will create the output files and add them automatically to your Visual Studio solution when you build your project.

5 csp Output Options

Go ahead and build your Visual Studio project. Visual Studio will notify you that there has been a file modification and allow you to reload the project.

6 after build

Once you reload the project you will see the created files added to the Visual Studio solution.

7 project rebuilt

The CodeSmith Generator Project made it possible for me to generate multiple class files in seconds. You can imagine how this will benefit you when the classes you need to generate become more complicated. Download a trial version and have fun with CodeSmith Generator. See for yourself how easy it is to generate code.

Dirk Strauss
Dirk is a Software Developer and Microsoft MVP from South Africa. He loves all things Technology and is slightly addicted to Twitter and Jimi Hendrix. Apart from writing code, he also enjoys writing human readable articles. "I love sharing knowledge and connecting with people from around the world. It's the diversity that makes life so beautiful." Dirk feels very strongly that pizza is simply not complete without Tabasco, that you can never have too much garlic, and that cooking the perfect steak is an art he has yet to master.

Similar Articles

  • rahul jain

    The following error when i build the codesmith project .

    Error 1 The “CodeSmith.MSBuild.CodeSmith” task could not be loaded from the assembly CodeSmith.MSBuild.v4.0, Version=6.0.4.0, Culture=neutral, PublicKeyToken=9fd61cdb91609a7a. Could not load file or assembly ‘CodeSmith.MSBuild.v4.0, Version=6.0.4.0, Culture=neutral, PublicKeyToken=9fd61cdb91609a7a’ or one of its dependencies. The system cannot find the file specified. Confirm that the declaration is correct, that the assembly and all its dependencies are available, and that the task contains a public class that implements Microsoft.Build.Framework.ITask. GalaxyERP.Data

  • rahul jain

    Hi,

    Is there any way in CodeSmith where automatically generate the Insert, Update and Delete code for the SQL Table. ?

    • Yes, you can do that. Run CodeSmith Generator Studio. Then in the Template Explorer, look under CodeSmith Generator Samples > Database > StoredProcedures and select one of the templates there. Then modify the selected template’s properties in the properties window.

      • rahul jain

        Thanks. But is there any Business layer automatically created for Insert,Update and delete instead of stored procedures. Passing DTO to the method. That dll can used for both Windows and web platform.

        Public Shared Function AddOrUpdateComboMaster(ByVal ParamConnStr As String, ByRef ParamDTO As ComboMasterDTO, ByVal ParamLocationCompanyID As Integer, ByVal ParamUserID As Integer) As Long
        Using DC As GalaxyERPDataContext = New GalaxyERPDataContext(ParamConnStr)
        Dim CM As ComboMaster
        If ParamDTO.ComboID > 0 Then
        CM = DC.ComboMaster.GetByKey(ParamDTO.ComboID)
        End If
        If CM Is Nothing Then
        CM = New ComboMaster
        DC.ComboMaster.InsertOnSubmit(CM)
        If ParamDTO.ComboID = 0 Then
        CM.ComboID = GeneratorDAO.GenerateID(ParamConnStr, “ComboMaster”, ParamLocationCompanyID, ParamUserID)
        Else
        CM.ComboID = ParamDTO.ComboID
        End If
        End If
        CM.ComboTypeID = ParamDTO.ComboTypeID
        CM.ComboName = ParamDTO.ComboName
        CM.PrintName = ParamDTO.PrintName
        CM.MatchText = ParamDTO.MatchText
        CM.AliasName = ParamDTO.AliasName
        CM.IsInactive = ParamDTO.IsInactive
        If EmptyIfNull(ParamDTO.Remarks) = “” Then CM.Remarks = Nothing Else CM.Remarks = ParamDTO.Remarks
        CM.ParentComboID = IIf(ZeroIfNull(ParamDTO.ParentComboID) = 0, Nothing, ParamDTO.ParentComboID)
        DC.SubmitChanges()
        Return CM.ComboID
        End Using
        End Function

        • I don’t think that it would create the business layer or data access code for you. The code sample you gave seems rather specific. Just an observation…. be sure to use @@IDENTITY to return the value for CM.ComboID if you want to set it to the inserted ID of the record (you can’t just do a select on the last record inserted).

  • Pingback: Programming Tools For Developers – #6 ⋆ Dirk Strauss' Tech Blog()

Top