Generate all classes automatically?

Generate all classes automatically?

Old forum URL: forums.lhotka.net/forums/t/1227.aspx


davem1958 posted on Friday, September 15, 2006

This might be wishful thinking, but it seems like one really nice feature of a code generator would be to automatically generate all classes and stored procedures at once, based on the current data schema.  Primary keys and foreign keys would drive the logic of which classes are child, root, etc. 

Is that possible with CodeSmith or similar tool?

If not, how close can I come to automatic generation?

Thanks...

Brian Criswell replied on Friday, September 15, 2006

There are at least two very simple ways of doing this with CodeSmith.  You can either set up a batch job or create master template that runs your regular templates against your metadata.  Both methods are described in the help file.

davem1958 replied on Monday, September 18, 2006

I'm sure it's practical, but I don't buy the "simple."  :)

I'm a little spoiled, because in my last project on .NET 1.1 / CSLA 1.5 one of the programmers had written a code-generator that created all 6 CSLA objects and related stored procs. 

Dave

Brian Criswell replied on Monday, September 18, 2006

Well, my code generation system uses the sub-templates method.  I found it very simple to set up, and the resulting process generated hundreds of objects with hierarchy, namespaces, saving objects in folders based on assembly and namespace, a base class regeneration scheme, all the stored procedures including loading grandchildren etc. in a single fetch, and applying the stored procedures to the database in a couple of minutes.  The master template that orchestrated this is only 160 lines of code with a lot of white space.

I really do not think it would take you more than a day to understand sub-templates and create a master template that walked through your metadata and executed the sub-templates using the RenderToFile method.

David replied on Thursday, September 21, 2006

Dave,

I only just bought CodeSmith today and have spent a couple of hours looking into this. I found it a little difficult to follow the help on sub-templates, but using the console application with a single XML input file is really easy to setup and might be the method you're looking for.

All you have to do is create a single XML file that lists the property sets for each object you want to generate, then running one command can generate all the files. See the help topic titled 'Using the CodeSmith Console Application".

davem1958 replied on Friday, September 22, 2006

David, I looked at the help file, and it's straightforward to create one file from one template using one XML property set.

There's also a section on Batch generation mode.  That looks like what you're describing, but there's only one page of help?  (That's a question)

Dave

Brian Criswell replied on Friday, September 22, 2006

To give you an idea of what a master template might look like:

<%@ CodeTemplate Language="C#" TargetLanguage="C#" Src="Utilities.cs" Inherits="Utilities" Debug="False" Description="Generates a project." %>
<%@ Register Name="SubTemplate" Template="SubTemplate.cst" MergeProperties="False" %>
<%
//Load metadata
MetaData data = MetaData.Load(fileName);

//Loop through metadata
foreach(Item item in data.Items)
{
    SubTemplate template = new SubTemplate();
    template.Item = item;
    template.RenderToFile(item.Name + ".cs", true);
}
%>

Copyright (c) Marimer LLC