Take the 2-minute tour ×

Is it possible to extend the SubSonic generator without modifying it's code? I would like to add my own custom methods that i can use inside the templates. Somthing similair like the Utility.GetVariableType method.

share|improve this question
add comment

4 Answers

You can't extend the built in templates, but you can replace them with your own templates without changing SubSonic.dll. See the templateDirectory parameter here: http://subsonicproject.com/docs/Generated_Classes/#Customizing_Active_Record

An example configuration would be:

<SubSonicService defaultProvider="Northwind" enableTrace="true"
    templateDirectory="C:\Program Files\SubSonic\SubSonic 2.0.3\Templates\MVC">
      <add name="Northwind" type="SubSonic.SqlDataProvider, SubSonic"
       connectionStringName="Northwind" generatedNamespace="Northwind"/>

You can get the current version of the built-in ActiveRecord templates from here.

share|improve this answer
add comment
up vote 1 down vote accepted

I've found the solution for my own problem :).
I can now extend SubSonic with functionality i need in the templates without needing to rebuild or change any of the SubSonic code itself.
It works for what i wanted to do and i think it can be usefull for others as well so here it is:

  1. Create a new class library SubSonicHelper. Mine has a class looking like this:

    using System;
    using System.Collections.Generic;
    using System.Text;
    namespace Helpers.SubSonic
        public class GeneratorHelper
            public bool IsColumnAllowed(string columnName)
                return columnName.Length == 1 ||
                       (columnName.Length > 1 && 
                       (!(columnName[0].ToString().Equals("_") && 
                       columnName[columnName.Length - 1].ToString().Equals("_"))))
  2. Build the assembly and copy SubSonicHelper.dll to your subsonic project.
  3. Setup your SubSonic project to use your own templates using the templateDirectory parameter.
  4. Edit your own templates and at the following after the const bool showGenerationInfo = false;

    System.Reflection.Assembly a = System.Reflection.Assembly.LoadFile(
        System.IO.Path.Combine(System.IO.Directory.GetCurrentDirectory(), "SubSonicHelper.dll"));
    object instance = a.CreateInstance("Helpers.SubSonic.GeneratorHelper");
    Type type = instance.GetType();

After this you have an instance of the GeneratorHelper that you can use inside the template. For accessing the methods you need to do the following:

  1. Create an array of objects for the parameters of the method you want to use. I have the columnName parameter which i set to col.propertyName. This is inside the foreach (TableSchema.TableColumn col in cols) loop in the Update method.
  2. Call the method you want to use with the object array as argument.
  3. Check the result object to see the result of the method.

    object[] arg = new object[]{col.PropertyName};
    object isColumnAllowedResult = type.InvokeMember("IsColumnAllowed", System.Reflection.BindingFlags.Default | System.Reflection.BindingFlags.InvokeMethod, null, instance, arg);
    if (Convert.ToBoolean(isColumnAllowedResult))

That's it! Now i can extend the SubSonicHelper class with other methods i want to use inside the template.

share|improve this answer
add comment

The short answer is no. If you come up with something useful, submit a patch and it will likely be integrated into the core. You can submit patches here: http://code.google.com/p/subsonicproject/issues/list

share|improve this answer
add comment

can you not import a dll in the template?
<%@ Import namespace="NewHelpers.Utilities"%>
and then call the function or create an instance of the object

share|improve this answer
I have a dll with the NewHelpers.Utilities namespace in the GAC and added it to the references in the project where i need to create the DAL. Added the import line to the template, but as soon as i call the static method SubSonic crashes with ERROR: Trying to execute generate. The helper class and method are both statics and i can call them from another project so they do work. I should say that i generate my DAL using sonic.exe generate /out Generated. Perhaps that is a problem? –  Marco Apr 27 '09 at 10:38
add comment

Your Answer


