How-to:CodeSmith Generator SDK Integration Instructions

Overview

After reading this document you will know how-to download, install and use the CodeSmith SDK in your applications.

Download

After logging into your account at http://www.codesmithtools.com click on the download button located near the top of the page. You will be presented with the CodeSmith Generator product downloads that are associated with your account. You can use any downloadable version but it is recommended that you the download CodeSmith Generator zip.

Note: It is recommended that you download the zipped version of CodeSmith Generator if you will not be using CodeSmith Generator Studio.

Installing the license

The next step is to install CodeSmith Generator Professional by clicking on the file you just downloaded (If you downloaded the zipped version there is no installing just unzip the files to a folder of your choice). Now run CodeSmith Generator Studio and enter in your Professional License key. In order to get your SDK License, you will need to contact sales and we will send you a .lic (License) file. The license for the SDK versions are handled a little bit differently than the other versions of CodeSmith Generator. You have three choices:

  1. Embed the license file into your application as an embedded resource in the assembly that calls the CodeSmith.Engine (Entry Assembly).
  2. Embed the license into your application and make a call to CodeSmith.Engine.Configuration.SetLicenseAssembly(Assembly) to register the assembly that contains the license file.
  3. Place the license file into the same directory as your application.

Creating a new project

In order to use CodeSmith Generator SDK you will need to create a new .Net 3.5 or newer .Net project. I recommend starting out with a console application. After you have started up Visual Studio you will need to add references to the required CodeSmith Generator assemblies.

Note: We already have a C# and VB.Net sample SDK project all setup for you located in your my documents folder (My DocumentsCodeSmithSamplesv5.3ProjectsCSharpAPISample). You may need to do some minor changes like changing the connection string and adding the project to an existing or new Visual Studio solution to get it running.

Adding project references

Next you will need to reference these three assemblies that are located in the GAC andor on disk. If you installed CodeSmith Generator Professional you will need to add three references to

  • C:Program FilesCodeSmithv5.2binCodeSmith.Engine.dll,
  • C:Program FilesCodeSmithv5.2binCodeSmith.Insight.Client.dll and
  • C:Program FilesCodeSmithv5.2AddInsSchemaExplorer.dll

Note: It is required that you add references to CodeSmith.Engine.dll and CodeSmith.Insight.Client.dll

Writing the Code

Now it’s time to dive in and write some generation code! We will add the following code to our console application:

public static void Main(string[] args)
{
    CodeTemplateCompiler compiler = new CodeTemplateCompiler("..\..\StoredProcedures.cst");
    compiler.Compile();

    if (compiler.Errors.Count == 0)
    {
        CodeTemplate template = compiler.CreateInstance();
        DatabaseSchema database = new DatabaseSchema(new SqlSchemaProvider(), @"Data Source=.SQLEXPRESS;AttachDbFilename=PetShop.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True;");
        TableSchema table = database.Tables["Inventory"];

        template.SetProperty("SourceTable", table);
        template.SetProperty("IncludeDrop", false);
        template.SetProperty("InsertPrefix", "Insert");

        template.Render(Console.Out);
    }
    else
    {
        for (int i = 0; i < compiler.Errors.Count; i++)
        {
            Console.Error.WriteLine(compiler.Errors[i].ToString());
        }
    }
}

The above code will compile and run but requires the Stored Procedures template that can be found in the APISample project that was mentioned above. If you have any SDK API questions feel free to ask on the community site or contact support.