views:

133

answers:

4

Is it possible to run T4 code generation without needing Visual Studio 2010? Basically I have to build an in house ORM (don't ask..if I had a choice I wouldn't). I was planning to use subsonic as a base but change some things and how they work. However my main question is can I run T4 from an external application that I write, so I can use the features of T4? Or am I better off doing it myself (which I doubt)?

A: 

AFAIK T4 templates are invoked from within Visual Studio IDE.

Building an ORM needs more than text templates. I suggest you look into AtomWeaver (at http://www.atomweaver.com) which is a code generator that lets you build models from individual building blocks (called "Atoms"). These Atoms are smart templates that act both as text templates but also as mini-programs, allowing you to do much more that simple string substitution.

You can develop your own "Atoms" that transform a database structure into source code. Then, for each new database, you combine these Atoms to build your schema, and fire up the generator to obtain the source code. Because what you've built was actually a model of your DB, you can later make any changes and regenerate your code.

AtomWeaver implements ABSE, a kind of model-driven software development (has nothing to do with UML or MDA). Learn the mechanics of ABSE at http://www.abse.info

AtomWeaver is presently in public beta. There isn't much documentation at this point, so you may have a hard time getting up to speed with it.

Rui Curado
A: 

There is a command line utility called TextTransform.exe you can use to generate code for a T4 template. I can't comment on if it is the right tool for building a ORM, but I like it well enough for generating state machines from an XML file.

http://msdn.microsoft.com/en-us/library/bb126245.aspx

Steven Behnke
+3  A: 

TextTransform.exe will do what you want for simple scenarios:

http://msdn.microsoft.com/en-us/library/bb126245.aspx

Here is how to run a T4 template from your own code for templates created in VS 2010:

http://msdn.microsoft.com/en-us/library/ee844259(VS.100).aspx

And here is how to run a T4 template from your own code for templates created in VS 2008:

http://www.capprime.com/software_development_weblog/PermaLink,guid,104d9faf-5780-42ca-88e5-c04cb88f61b3.aspx

There will be some issues running Subsonic T4 templates outside Visual Studio:

http://stackoverflow.com/questions/2325403/how-can-i-automate-the-t4-code-generation-for-subsonic/2363593#2363593

I would stick to T4 rather than roll your own template engine.

Michael Maddox
+1  A: 

T4 is a part of Visual Studio. If your ORM tool can assume that Visual Studio is available, T4 is a good choice. You have an option of redistributing the Visual Studio shell, which also includes T4, with your application. Alternatively, you can use preprocessed templates to compile the templates into executable code generators. In compiled form, these templates don't require Visual Studio, but also cannot be modified.

Oleg

Oleg Sych