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 per
sonally 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.
- Enjoy.
July 28th, 2009 at 11:05 pm
There is some tool like this for the subsonic 3?
August 15th, 2009 at 10:37 am
@San Miguel de Allende - SubSonic 3 provides the same functionality however file paths may differ from those in the provided instructions since install locations (based on the SubSonic version number) has changed. Thanks for the question.
September 9th, 2009 at 10:04 pm
I know the above comments are a bit old, but just wanted to pitch in: SubSonic 3.0 uses T4 templates (baked in to VS2008) for code generation. The “old” aspx method from SubSonic 2.0 isn’t used and sonic.exe to generate templates is no longer necessary. Check out Rob’s SubSonic docs section for good info on SubSonic 3.0.
September 11th, 2009 at 2:28 pm
@Tim - Thanks for chimming in. One could still create custom templates using the old approach (templates are installed with 2.1 and not even with 2.2), but you’re right, the approach has definitely changed with SubSonic 3.0. The bottomline is my reply to @San Miguel de Allende is dead wrong. Thanks for providing information.