Over the weekend, I found myself doing a lot of grunt work creating more than my fair share of data transfer objects which, for me, are nothing more than custom collections which contain very simple objects exposing public properties. Simple, but cumbersome enough that I wanted to generate the classes.
I have been working with SubSonic for a few weeks now but I didn’t realize the tool could generate files based on custom/personalized templates. It’s a good thing Jon Galloway still tolerates my stupid questions or I might still not know…
Here’s how it is done using the base SubSonic install (assumes Sonic.exe and the default templates can be found under c://program files/.)
- Make a copy of the default templates folder as found in C:\Program Files\SubSonic\SubSonic 2.0.3\src\SubSonic\CodeGeneration\Templates. I named my copied folder “SimpleClassTemplates.”
- Open the aspx files in Visual Studio and modify to your heart’s content. For example, I wanted an alternate C# class template so I modified CS_ClassTemplate.aspx.
- I am going to assume you are already familiar with code generation with SubSonic. I personally like to setup a Visual Studio External Tool to allow for quick, pre-configured regeneration. Otherwise, the following can be ported over to the command line. Here’s the External Tool setup instructions:
- Tools > External Tools > Add
- Title: SubSonic Classes
- Command: C:\Program Files\SubSonic\SubSonic 2.0.3\SubCommander\sonic.exe
- Arguments: generate /out Generated /namespace NAMESPACE /server SERVER/db DATABASE where NAMESPACE, SERVER and DATABASE are replaced accordingly.
- Initial Directory: $(ProjectDir)
- Check “Use Output window” and “Prompt for arguments.”
- Select Apply or OK
- Select the project which will contain the “Generated” folder and auto-generated files. Select Tool > SubSonic Classes.