Using COM objects in a T4 template? - t4

I'm new to Visual Studio's T4 template engine, and I'm wondering how I can use COM objects to build templates. Specifically, SQL-DMO (to generate classes from stored procedures on a SQL 2000 server, SNO is not an option).
Have any of you guys done this? Any tips?
<## import namespace="SQLDMO" #>
is not working.
Thanks!

You can use SMO 2008 with SQL Server 2000 (download here).
In order to use a COM object, you need to reference its primary interop assembly with use <## assembly #> directive first. If the assembly is not available, you can generate it with tlbimp.

Related

T4 templates vs TT Templates vs Includes in visual studio

In visual studio there are 2 types of text transformation templates file extensions .T4 vs .TT
I would like to know their differences and which one I should use when I want extend to build Views Controllers and Models, when I read a schema off a DB
I also want to know if .includes can be reused in both.
There is no difference. Back around 2008 this feature was treated as if it were an add-in (even though it ended up being built into VS directly). Microsoft called it the "Text Template Transformation Toolkit," hence the .T4 extension. Common usage shortened that to "text templates" yielding the .TT extension and this appears to have become the standard extension.
Transformation files are just code, they can use any feature of the language you choose. For example, in C# templates I regularly reference .NET assemblies as follows:
<## import namespace="System.Text" #>

Failed to resolve type for directive processor T4Toolbox.VolatileAssemblyProcessor

I'm trying to use types from my own project in a T4 template, and so I want to use the VolatileAssembly directive so I don't have to keep restarting Visual Studio:
<## VolatileAssembly processor="T4Toolbox.VolatileAssemblyProcessor" name="C:\Josh\Archimetrics\Archimetrics.Consulting\Suntex\SuntexFirstInMathTools\bin\Debug\SuntexFirstInMathTools.exe" #>
But I keep getting the following error:
A processor named 'T4Toolbox.VolatileAssemblyProcessor' could not be found for the directive named 'VolatileAssembly'. The transformation will not be run.
The following Exception was thrown:
System.IO.FileNotFoundException: Failed to resolve type for directive processor
T4Toolbox.VolatileAssemblyProcessor.
at Microsoft.VisualStudio.TextTemplating.VSHost.TextTemplatingService.ResolveDirectiveProcessor(String processorName)
at Microsoft.VisualStudio.TextTemplating.Engine.ProcessCustomDirectives(ITextTemplatingEngineHost host, TemplateProcessingSession session, IEnumerable`1 directivesToBeProcessed)
I've installed the T4 toolbox and restarted, but for some reason it will not recognize the VolatileAssemblyProcessor.
How can I fix this?
They are also harder to use than templates because code they generate
is invisible to you by default. In addition, the VolatileAssembly is
no longer necessary because in Visual Studio 2012 the assembly
directive shadow-copies the assembly files before loading them
- the developers page -
So I think you do not need this directive anymore. Only use <## assembly name="YourPathOrName" #> will fix your problem.

Can't reference an assembly in a T4 template

I have the following code in a tester class in my main assembly, PocoGenerator. This assembly is supposed to use a T4 template to generate POCO's based on L2S entities in a referenced assembly (a project reference), DataObjects.
var assemblyName = "DataObjects";
var dataObjects = AppDomain.CurrentDomain.Load(new AssemblyName(assemblyName));
Try as I may, I cannot get T4 to find the DataObjects assembly. I have tried various forms of assembly directives, like:
<## assembly name="DataObjects" #>
<## assembly name="DataObjects, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" #>
to no avail. The code above works in the tester class, but not in the template. What am I doing wrong?
ADDED:
I have resolved this issue by using the absolute path to the assembly in bot places I reference it, the directive as well as the class feature block, i.e.
<## assembly name="C:\Development\PocoGenerator\DataObjects\bin\Debug\DataObjects.dll" #>
and
var sourceAssembly = Assembly.LoadFile(#"C:\Development\PocoGenerator\DataObjects\bin\Debug\DataObjects.dll");
But I really don't like this, as I would like to use this template in various projects, and I just plain hate duplication, especially of magic strings.
<## assembly name="$(ProjectDir)bin\Debug\ProofOfConcept.dll" #>
Happy Coding!
To reference assembly in T4 template in VS2010 you have some options:
GAC your assemblies and use Namespace Reference or Fully Qualified Type Name
Use a hard-coded Fully Qualified UNC path
Copy assembly to Visual Studio "Public Assemblies Folder" and use Namespace Reference or Fully Qualified Type Name.
Use or Define a Windows Environment Variable to build a Fully Qualified UNC path.
Use a Visual Studio Macro to build a Fully Qualified UNC path.
I would suggest that you put a referenced assembly in your Public Assemblies Folder, another, maybe even better solution would be to hard code the path of your referenced assemblies.
Very nice post on this topic: T4 Template error - Assembly Directive cannot locate referenced assembly in Visual Studio 2010 project.
Basically MS decided to the the braking change, that the project referenced assemblies are not referenced by T4 engine, too.
T4's assembly set is completely separated from the containing project's assembly set to avoid picking up the wrong assemblies when a project targets previous framework versions. Project assemblies are no longer used to resolve template assembly directives.
More on that: What's new in T4 in Visual Studio 2010
I had a similar problem when I tried to include Less Css for .NET in my Web project.
I've ended up with copying the assembly in the root folder of my project and including it as a reference in the project itself. Then, I've added the following lines in the .tt file:
<## assembly name="dotless.Core.dll" #>
<## import namespace="dotless.Core" #>
<## import namespace="dotless.Core.configuration" #>
I'm sure that something similar should work with your assembly as well...
I've found there are a number of cases in creating and using the gax toolkit and packages where the build is perfectly happy with the way references are structured but the runtime gets all bothered because it can't find what it's looking for - this usually occurs when the main assembly references an assembly that uses gax elements and then that assembly in turn references another assembly that the main does not.
try directly including the assembly in question in your main assembly - and consider that you may need to write post build instructions to move it to an 'expected' location - while a nusiance, it should beat having to hardwire the path.
YMMV

Referencing an assembly in a T4 template

It's been a while since I last used T4 and this is probably a silly question...
Is it possible to reference an arbitrary assembly from a template?
Example:
I have a class that I'd like to use in Project X
Project X.Test references X and contains the .tt file
I assume the following should work
<## assembly name="X" #>
But I get the following error on save:
Compiling transformation: Metadata
file 'X' could not be found
What am I doing wrong?
(In case anyone's interested: I'm trying to automatically generate a particular type of tests based on some metadata that I get from X)
Update: it looks like VS2010 has broken the assembly resolution behavior that I was expecting. From Link:
T4's assembly set is completely
separated from the containing
project's assembly set to avoid
picking up the wrong assemblies when a
project targets previous framework
versions. Project assemblies are no
longer used to resolve template
assembly directives.
Are there any workarounds, besides using absolute paths?
You can use VS macro variables such as $(SolutionDir) in your reference as of VS2010
e.g.
<## assembly name="$(SolutionDir)\Project1\bin\debug\Foo.dll" #>
You can also check here on SO: Can't reference an assembly in a T4 template
#GarethJ gives a good answer but for all the methods of referencing an assembly from a T4 template try this: T4 Template error - Assembly Directive cannot locate referenced assembly in Visual Studio 2010 project.
And if you like the VS Macro solution then you can find 'em all here: Macros for Build Commands and Properties

How can I use Linq in a T4 template?

I am using T4 to generate some screens and middle-tier code for a project, and would like to use Linq to simplify some of my template code. However, when I try to use Linq, the template reports a syntax error.
By default in Visual Studio 2008 (and as used in most online examples) the template is compiled with the 2.0 Framework, which does not include Linq. (See MSDN forum thread)
To solve the problem, three steps are needed:
In your template's language attribute, specify "C#v3.5" or "VBv3.5" - this step is not required for VS2010, where .Net 4.0 is always used.
Add an assembly directive for System.Core.dll
Import the System.Linq namespace
Your template will now look something like this:
<## template language="C#v3.5" #>
<## assembly name="System.Core" #>
<## import namespace="System.Linq" #>
You can now use Linq and other new language features in your template.

Resources