Codify

A Powerful .NET
Code Generator

Getting Started in 5 Minutes or Less

Here's a five minute introduction to get you up and running with Codify. We'll show you how to generate code by adding CodeBuilders to a file. Then we'll show you how to alter the input to the template to get different output. Last we'll customize the template used to generate code to get specific output.

Projects and Files First

Let's start off by opening a C# or VB.NET project in Visual Studio .NET. I'm going to create a new C# project using the Class Library template. Codify can work with any existing project, but for the sake of this tutorial, let's create a new one.

I'm going to add a class to the project and call it Person. Here's what I have in Visual Studio right now:

Class Person
Figure 1: Class Person

Typically, a person will have a first and last names so we're going to need properties for each. Let's use Codify to help us out.

Adding a CodeBuilder

To add the properties to the class we're going to use a CodeBuilder. We add a CodeBuilder by first placing the cursor where we want the generated code to go. In this case, we'll add two carriage returns after the close of the Person constructor, and leave the cursor on the second.

Add-In Menu
Figure 2: Add-In Menu

Now that we've selected a spot for the generated code, we can add the CodeBuilder. We do this by clicking on the Add CodeBuilder item on the Codify menu. Clicking on Add CodeBuilder brings up a dialog box that lets us select a template for our CodeBuilder.

Since we're adding properties for first name and last name to our Person class, let's select Workstate Simple Property Builder. To add the CodeBuilder, we just double-click on the title of the template we want.

Changing Parameters

After selecting our template from the dialog box Codify shows us another dialog. This is the dialog where we can define our first and last name properties. You might be wondering why you already see properties called FirstName, LastName, PersonGuid, and InternalStaticProperty. These are the default parameters for this template. We only want FirstName and LastName, so we need to remove PersonGuid and InternalStaticProperty. Let's do this by right-clicking on them and selecting Remove this Property from the menu. You should now see the dialog box illustrated in Figure 3.

CodeBuilder Editor
Figure 3: CodeBuilder Editor

Now all we have to do is click on the Save and Run button to generate the code. This closes the dialog and inserts the generated code in the Person class. We have now added properties for first and last names.

Generated Properties
Figure 4: Generated Properties

Now that we have generated some code the first thing you're about to say is, "I don't write code that looks like that. Fields in my classes don't use a prefix and they are all lowercase. How can I change it?" This issue is something we considered when building Codify and we wanted to make a system for code generation that was flexible enough to answer those questions.

Let's address your issues one by one. The first thing we will do is get rid of the prefixes. This one is easy, all we have to do is edit the parameters to the template. To do this, we'll click somewhere inside the generated code (I'm clicking on the first line of the FirstName property). Next we'll click on Edit CodeBuilder on the Codify menu. This brings up the CodeBuilder Editor the same as we saw in Figure 3. There is a property designed just for us, Instance Field Prefix. Right now it is set to 'M_', let's remove the prefix by selecting this value and deleting it. Let's click on Save and Run to view our changes.

Hmm, we're getting some unfriendly squiggles in the generated code. If we tried to compile right now we would get this:

Person.cs(34): The class 'ClassLibrary1.Person' already contains a definition for 'FirstName'

To fix this problem, we'll have to get on to making the fields lowercase. This is a bit more complicated.

Customizing Generated Code

When we added the CodeBuilder to our file, Codify also added a file to our project. It is a local copy of the Workstate Simple Property Builder template. To make our fields lowercase, we're going to need to change the template used to generate our code.

Template Editor
Figure 5: Template Editor Nodes

To edit the template, we'll double-click on it in Visual Studio. This brings up the Codify Template Editor. Changing the generated code requires changes to the code that generates the code. Try saying that three times fast.

Let's just click on the C# item in the TreeView. It brings up three tabs in the right pane of the editor: Rendering Script, Class Include, Parameter Schema API. Right now lets just focus on the Rendering Script tab. Lets go to line 30, it's where we determine the field name for an instance property. (If you would like to read the rest of the Rendering Script to understand it better, do it later; right now let's get to work on solving our problem).

Line 30 in the Rendering Script looks like this:

fieldName = Parameters.RootItem.InstanceFieldPrefix + property.Name;

To make the fields lowercase all we have to do it call the ToLower method on property.Name, which is a string. So now line 30 looks like this:

fieldName = Parameters.RootItem.InstanceFieldPrefix + property.Name.ToLower();

Let's save our changes to the template by clicking on the File menu, then Save. We're done making changes so let's close the Template Editor with File -> Exit.

Now that we've changed our template, we can re-generate the code. Let's click on the Run CodeBuilders item on the Codify menu. Codify re-runs the CodeBuilder in the current file and uses our changes in the template. We now have code that compiles and looks similar to code you would write yourself.

Just a Start

This quick introduction should give you an idea of what programming with Codify is like. If generating code to implement properties doesn't seem revolutionary to you, then you're right - it isn't - but it will save you time, and that time adds up quickly into real savings.

Also, as you begin to identify similar patterns within in your own code, you can use Codify to quickly and easily turn those patterns into templates. The result is an ability to achieve significant productivity enhancements on nearly any development project.

That's it for our quick overview! If you have any questions about this tutorial or Codify in general then drop us a line at team.codify@workstate.com.

PS: If this took you more than five minutes then please start over and try to work faster this time. We don't want to be perceived to be liars.