Search code examples
subsonic

Extending SubSonic Generator


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.


Solution

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