tags:

views:

389

answers:

3

I would like to add basic logging and make some other minor changes to the classes generated by SubSonic 2.1 (I'm not using SubSonic 3.0 t4 templates).

Is there a way to do this without modifying the SubSonic source code?

+1  A: 

Yes you can modify the templates that version 2 uses, they're just aspx files. The templates are stored in src\SubSonic\CodeGeneration\Templates under your installation directory.

This blog post goes into more detail:

http://johnnycoder.com/blog/2008/06/09/custom-templates-with-subsonic/

Adam
@Adam - You beat me to it! I should have just summarized and linked to my blog rather than reformatted the answer for SO. See my answer below. :) Well done.
Ben Griswold
+4  A: 

You have two choices. You can modify the default templates or create your own. I suggest making your own templates which will lives side-by-side with the original and then generate your code via the following instructions.

Note that these steps assume you ran the default SubSonic installation. In other words, Sonic.exe and the default templates can be found under c://program files/. If not, you'll find your SubSonic files/templates in an alternative installation location, of course.

  1. Make a copy of the default templates folder as found in C:\Program Files\SubSonic\SubSonic 2.1 Final\src\SubSonic\CodeGeneration\Templates. I might recommend naming the copied folder "TemplatesWithLogging.”
  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. If you want to merely alter the default templates, you can but I suggest making a backup first.
  3. 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: TemplatesWithLogging

    • SubSonic Classes Command: C:\Program Files\SubSonic\SubSonic 2.1 Final\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 > TemplatesWithLogging.

You can find more here.

Ben Griswold
A: 

If you're using RepositoryRecord, you can add auditing/logging to a repository base class. I demonstrated this on my blog.

John Sheehan