SharpDevelop 4.2 now supports SubSonic T4 templates.
Subsonic is an open
object-relational mapper and makes use of T4 templates in all
but one of its supported methods to provide access to your data.
Before we take a look at what new T4 features were added to
SharpDevelop to support SubSonic let us see what you need to do to
get everything working.
Using SubSonic T4 Templates with SharpDevelop
In order to use the SubSonic T4 templates with SharpDevelop you
need to make two modifications to the Settings.ttinclude file.
Replace the EnvDTE assembly reference:
<#@ assembly name="EnvDTE" #>
With the following:
<#@ assembly name="$(addinpath:ICSharpCode.PackageManagement)PackageManagement.dll" #>
This references SharpDevelop's NuGet addin assembly which
implements its own version of the Visual Studio object model.
In SharpDevelop 4.3 an extra assembly directive is needed, as shown below:
<#@ assembly name="SharpDevelop.EnvDTE.dll" #>
Add a new import directive as shown below:
<#@ import namespace="EnvDTE = ICSharpCode.PackageManagement.EnvDTE" #>
This creates a namespace alias for EnvDTE so the references to
the Visual Studio object model in the SubSonic T4 template will
In SharpDevelop 4.3 the EnvDTE import directive does not need to be changed.
All that is left now is to save the changes made to the
Settings.ttinclude file and generate the database access code by
executing the ActiveRecord.tt, Context.tt and Struct.tt T4
templates. This can be done by right clicking them in the Projects
window and selecting Execute Custom Tool.
Now we will take a look at the new features that made this
Referencing SharpDevelop Properties and Environment Variables
in T4 Templates
The T4 assembly and include directives now support the
$(PropertyName) syntax to use SharpDevelop properties such as
SolutionDir and ProjectDir.
<#@ assembly name="$(ProjectDir)lib\MyLibrary.dll" #>
<#@ include file="$(SolutionDir)Templates\MyTextTemplate.tt" #>
The assembly and include directives also support environment
variables through the %EnvironmentVariableName% syntax.
<#@ assembly name="%ProgramFiles%\lib\MyLibrary.dll" #>
<#@ include file="%HOMEPATH%\MyTextTemplate.tt" #>
This new feature also supports the addinpath property and is
used to allow a T4 template to reference SharpDevelop's NuGet
addin using the $(addinpath:AddIn-ID) syntax which is expanded to
the directory containing the addin.
Accessing the SharpDevelop IDE from a T4 Template
The SubSonic T4 templates use the Visual Studio object model to
find the project's app.config file and read the database
connection string stored here in order to access your database. To
support this the T4 templating host in SharpDevelop now implements
the IServiceProvider interface which allows you to access
SharpDevelop's implementation of the Visual Studio object
To access the Visual Studio object model you set the T4 template
so it is host specific:
<#@ template language="C#" hostspecific="true" #>
Then you can access the DTE object, which is the top-level
object of the Visual Studio object model, as shown below.
IServiceProvider provider = (IServiceProvider)Host;
EnvDTE.DTE dte = (EnvDTE.DTE)provider.GetService(typeof(EnvDTE.DTE));
The Visual Studio object model is not fully implemented in
SharpDevelop so you may run into problems if you make extensive use
of this object model.
[Update 2012-10-11] Added changes required for SharpDevelop 4.3