Custom Templates with SubSonic

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/.)

  1. 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.”
  2. 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.
  3. I am going to assume you are already familiar with code generation with SubSonic.  I perimagesonally 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
  4. Select the project which will contain the “Generated” folder and auto-generated files.  Select Tool > SubSonic Classes.
  5. Enjoy. 

Comments

  1. @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.

  2. 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.

  3. @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.

  4. Hi are using WordPress for your blog platform? I’m new to the blog world but I’m trying to get started and create my own. Do you require any html coding knowledge to make your own blog? Any help would be greatly appreciated!

  5. I was more than happy to find this web-site.I needed to thanks for your time for this excellent read!! I positively enjoying each little bit of it and I’ve you bookmarked to check out new stuff you weblog post.

  6. My husband and i got very delighted when Emmanuel managed to do his preliminary research using the precious recommendations he obtained when using the web pages. It is now and again perplexing to just happen to be giving for free ideas which often many others might have been trying to sell. And now we do understand we’ve got you to appreciate for this. The explanations you’ve made, the easy blog menu, the friendships you can make it possible to create – it’s got many overwhelming, and it’s really letting our son in addition to us imagine that this article is cool, and that is extraordinarily fundamental. Thanks for all the pieces!

closed