Frustrations with CodeSmith

Many many developers have had the pleasure of playing around with a tool called CodeSmith. I certainly love what it does and have been using it since my university days. But when I went to the tool's website yesterday, I was deeply saddened to see that as from version 3.0, CodeSmith is not freeware anymore. If you look up 'sellout' in the dictionary, you might find a description that resembles the sequence of events here described.

Now the freeware version of CodeSmith is quite useful for simple tasks, but when advanced tasks are attempted, one has to resort to hacks. Let me illustrate with the all time classic example - overriding the default editor for a property. Presently, you would have to do something like the following in the script section of your template:

[Editor(typeof(MyEditor), typeof(UITypeEditor))]
public MyType MyProperty
{
    
get
    
{
        
return pMyProperty;
    }
    
set
    
{
        pMyProperty = 
value;
    }
}

Wouldn't it be much easier to have something like what follows in the prologue if the template?

<%@ Property Name="MyProperty" Type="MyType" Editor="MyEditor" %>

Another problem that I'm having is activating a drop-down editor in a property. Presently, I have to make an assembly that has a TypeConverter that will provide CodeSmith's PropertyGrid with values. The following code illustrates what I currently have to do:

    public class MyConverter : StringConverter
    {
        
public override bool GetStandardValuesSupported(ITypeDescriptorContext context)
        {
            
return true;
        }

        
public override bool GetStandardValuesExclusive(ITypeDescriptorContext context)
        {
            
return false;
        }

        
public override System.ComponentModel.TypeConverter.StandardValuesCollection GetStandardValues(
            ITypeDescriptorContext context)
        {
            
return new StandardValuesCollection(new string[] {"String 1", "String 2"});
        }
    }

And in the script section of the template:

[TypeConverter(typeof(MyConverter))]
public string MyProperty
{
    
get
    
{
        
return pMyProperty;
    }
    
set
    
{
        pMyProperty = 
value;
    }
}

Contrast that with something like this:

<%@ Property Name="MyProperty" Type="System.String" DropDownValues="GetMyPropertyValues" %>

...and in the script section of the template:

private void GetMyPropertyValues(object sender, DropDownValuesEventArgs e)
{
    e.Values = 
new StandardValuesCollection(new string[] {"String 1", "String 2"});
}

Much easier - and you now don't need an extra assembly.

Finally, a pet peeve, is the way Xml Serialization is handled in CodeSmith. I think that this might be an issue with the way CodeSmith interacts with the .NET Serializer, but I still don't know what to do. Here is a snippet of code in my template:

    public VeloTestFieldCollection ReceiveFields
    {
        
get
        
{
            
return pReceiveFields;
        }
        
set
        
{
            pReceiveFields = 
value;
        }
    }
    

When I try and save the properties to xml, it appears as "(collection)" and all the information in the collection is lost. What to do? Help!?!?! And as an aside, I don't like the fact that CodeSmith generates a set accessor for collections. Bad CodeSmith.

If you use CodeSmith, I'm sure that you've gained much benefit from it. But if you've also experienced frustration in creating templates, why not share it with the rest of us and comment about it.

Published Tuesday, June 07, 2005 1:53 PM by trumpi
Filed under:

Comments

# Re: Frustrations with CodeSmith

Oh - there's one more. Lack of #line pragmas. When your template does not compile, you want the compiler to report the line numbers according to where it is located in your template and not according to where it is located in the C# that gets generated behind the scenes.

Tuesday, June 07, 2005 2:26 PM by Trevor Green

# re: Frustrations with CodeSmith

Hmm, it's a pity it's not free anymore.

The attributes used to define custom ui editors for properties are not dictated by codesmith, but by the .net PropertyGrid control that it uses to display the properties. There is an article in the msdn about it here:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dndotnet/html/usingpropgrid.asp

I had a similar problem with collections which I noted here :

http://forum.codesmithtools.com/default.aspx?f=5&m=3949&g=3961#m3961

and you can download the template and code which contains the collection here:

http://forum.codesmithtools.com/default.aspx?f=9&m=3898&g=3968#m3968

Tuesday, June 07, 2005 3:36 PM by Colin

# Re: Frustrations with CodeSmith

Thanks - that was helpful.

I'm not sure that I understand your first point, though. Could you give some clarification?

Tuesday, June 07, 2005 3:42 PM by Trevor Green

# re: Frustrations with CodeSmith

Codesmith uses the .net PropertyGrid control to display the list of properties on the class that gets generated from your template. i.e. it generates a code file from your template, compiles the code into a class, and then creates an instance of the class which is used as a source for the propertygrid. The person executing the template fills in the values in the propertygrid before hitting the generate button.

In order to provide the metadata for the propertygrid to know how to display the properties, the propertygrid uses the attributes defined on the property to extract the metadata.

[Editor(typeof(MyEditor), typeof(UITypeEditor))] is an attribute.

They went for a fairly abstract model in the System.ComponentModel namespace to make it flexible. The msdn article explains how to use the TypeConverters etc to extend the PropertyGrid.

So yes, it would be simpler to just provide an array of strings for a dropdown, but that is only one scenario the design caters for.

Tuesday, June 07, 2005 3:53 PM by Colin

# Re: Frustrations with CodeSmith

Thanks Colin. Maybe the point I was trying to make in my post didn't come out clearly. If specifying an editor for a property is as simple as placing an EditorAttribute on it, why doesn't CodeSmith do it? It has been an issue that has been raised and so much so that there was a place on the website demonstrating the hack that I showed in my post.

The Editor feature won't be as difficult to implement in a CodeSmith-like tool as the DropDownList feature. It is still possible, though, using a generic subclass of a StringConverter that calls the delegate specified in the prologue. Issues such as multiple drop down lists will have to be dealt with, but it is still a possibility.

Tuesday, June 07, 2005 4:05 PM by Trevor Green

# re: Frustrations with CodeSmith

Codesmith version 2.6 is still freeware. Scroll to the bottom of the codesmithtools.com page and you will see a link to download the freeware version.

Wednesday, June 15, 2005 1:42 PM by Kelly Sumrall

# re: Frustrations with CodeSmith

Yeah it's freeware, but the free version is practically abandonware.

Wednesday, June 15, 2005 3:16 PM by Trevor Green