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.
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
can you not import a dll in the template?
like
<%@ Import namespace="NewHelpers.Utilities"%>
and then call the function or create an instance of the object
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/configuration/config-options/
An example configuration (from Rob Conery's blog) would be:
<SubSonicService defaultProvider="Northwind" enableTrace="true"
templateDirectory="C:\Program Files\SubSonic\SubSonic 2.0.3\Templates\MVC">
<providers>
<clear/>
<add name="Northwind" type="SubSonic.SqlDataProvider, SubSonic"
connectionStringName="Northwind" generatedNamespace="Northwind"/>
</providers>
</SubSonicService>
You can get the current version of the built-in ActiveRecord templates from here.
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 :
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("_"))))) } }
}
Build the assembly and copy SubSonicHelper.dll to your subsonic project
- Setup your SubSonic project to use your own templates using the templateDirectory parameter.
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 :
- 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.
- Call the method you want to use with the object array as argument.
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.
Would someone mind updating this link: "See the templateDirectory parameter here: http://subsonicproject.com/configuration/config-options/"
Kinda need that, now I have to hunt for it. :) If I find it, I'll update this.