For what purposes have YOU used T4? - visual-studio

T4 has existed for several years in Visual Studio, but doesn't get a lot of attention. However, for those that know it, there seems to be some very creative and useful purposes.
I am researching some different ways that T4 is used, and I would appreciate to hear how YOU may have used it for real life scenarios. I am primarily interested in non-standard and creative uses.
Some interesting examples:
Phil Haack uses T4 to create static CSS files from .less
To Generate WPF and Silverlight Dependency Properties using T4 Templates
Note: I realize this is a discussion-oriented question, but the answers could be helpful to others. I have tagged it as subjective and also marked as "community wiki", so please allow the question to remain open. Thanks!

I am not a big fan of the stringy-ness of app.config/web.config, so I use T4 to read those files and make an AppSettings/WebSettings class that wraps the connection strings and key/values in a real class. This means that, as long as I always use AppSettings.SomeValue to reference my app.config, I get compile time checking, which is really nice.

I've used the T4 Templates within the sharp-architecture to generate everything from models to controllers to basic views.
Definitely worth checking out, even if you just want to see some advanced examples of T4 templates in action

I use T4 to:
Generate CRUD SQL Server and Oracle scripts.
Generate Data Access Layer, based on a database schema
Layer Generate Business Logic Layer, based on a database schema
Generate ASP.Net webforms, both HTML and codebehind, based on a database schema (scafolding).
It gives me a good, quick, simple, basic starting point for my projects.
And the best is I'm in control.
Here you can download an example of my templates

SubSonic 3.0 makes heavy use of T4 templates for generating your entity code.
Essentially it calls GetSchema() on your database connection and runs each table it finds through the T4 entity template. The great thing about using T4 here is that if you don't like the way it's handling your database schema, just edit the template.
I've tweaked the T4's to handle MySQL databases better for my situation, as I make use of many tinyint columns which the default T4 maps to byte types. A quick edit to the T4 gave me the type I wanted instead for my application entities.

LINQ to SQL templates for T4
http://l2st4.codeplex.com/
Templates replicating the functionality of the SQLMetal and the LINQ to SQL classes designer code-generators for both C# and VB.Net requiring just Visual Studio 2008.

Check out this podcast on T4 by Scott Hanselman talking with Kathleen Dollard.
http://www.hanselminutes.com/default.aspx?showID=170

I've used T4 to generate:
proxies (design time, for injecting/wrapping monitoring
code/logging/... in a very specific exposed api).
interface generation for a one-on-one interface/class mapping
replace reflection
code by "directly/real" calling code (maintenance advantage of
reflection code, but performance of the actual code) for instance
when allowing access to properties through an indexer, or something
in that direction.
xml generation for a java project (couldn't find a
T4-like solution for java, that is easily shared within a company, T4
is easy because it's build in and you can run it from command line)
generate enums from a master database (we generated them both for a delphi-code base and .Net code base)

T4 Templates are used heavily in the Web Service Software Factory (Service Factory).

See here for a list of more than 30 T4 Generators from the community in several areas including ASP.NET,WCF, UML, ADO.NET, .NET
http://t4-editor.tangible-engineering.com/How-Do-I-With-T4-Editor-Text-Templates.htm

Related

Find all references doesn't locate member references in other .NET projects of differing language

I have a VS 2013 solution with one VB.NET ASP.NET project which references several c# class library projects (not DLLs) within the same solution. I made lots of changes to logic within some of the method overloads in one of the c# projects. Since the VB.NET project uses these overloads extensively, I want to see how many places these methods are referenced. I go to the c# project that contains the changes, right-click the overload I want, and select Find All References, but it only shows me the references in the c# projects. However, when I go to one of those methods in the VB.NET project, it finds all the references within the VB.NET project as well as the method declarations in the c# project.
I could do a solution-wide text search for the method name, but this method name is used multiple times because it's overloaded. Therefore, I want to search for a particular overloaded method signature, not the method name.
Is this a known shortcoming of VS 2013? Something else I've also noticed that I've noticed is that selecting Go To Reference from within a VB.NET project on a method that's declared in a c# project will take you to the object browser rather than directly to the method definition in the source project itself.
This is an old problem, there are many bug feedback reports for it on Microsoft's feedback site. The canonical one is probably this one. Quoting Dustin Campbell's response:
The general problem is that C# and Visual Basic do not share any common infrastructure with regard to the symbol tables that are used for features like Find All References, Go to Definition or Rename. So, such features don't work between multiple languages. That said, we are looking at ways to change this in the future, but the work required is very substantial.
Annotating a bit, the C# and VB.NET IDEs look similar at the surface but they are very different under the hood. Part of that is them being supported by distinct groups within Microsoft, a survival strategy for any software company, big groups don't work. But above all history plays a strong role in this, Visual Basic had strong IDE support for a very long time. They did not throw away that work when they moved to VB.NET, not in the least because they had to support all previous IDE features so not to alienate their existing customer base. The C# team got a fresh start without that baggage, they started from scratch. Inevitably the internals between them, like the IntelliSense symbol tables, are drastically different.
Tackling the "common infrastructure" problem was not forgotten, the Roslyn project was the instrumental step. It took a very long time to get finished but it is ready to go today. Integrated into VS2015. Whether it actually solves this problem is something I don't know yet, I will in a couple of months. Maybe somebody that has the RC version can confirm. They did create two versions of Roslyn, one for C# and another for VB.NET. Uh-oh.

Possibilities of the IWizard interface

I've played around with Visual Studio Project Templates and found them useful so far.
Now I've found some material on Wizards and the IWizard interface.
The first basic steps are fairly easy to comprehend and seem very useful, but now I'd like to explore what else the IWizard interface has to offer, other than mere Text replacement. I can imagine a few use cases for the ShouldAddProjectItem method, but what - for example - if I want the user to be able to pick database tables and have the wizard generate model, mapping and CRUD-dao code (I already have an assembly for that tedious task)?
Can anyone give me links or hints on facilities of the interface?
In order to make CRUD, DAO, etc... I'll suggest you to check T4 Templates and, if you want more power.
Combine T4 with Dev Art's Entity Developer
Put it all inside a IWizard
Let me explain it a little bit more...
You'll need to develop some T4 Template integrated with Entity Developer (to apply either Entity Framework's or NHibernate's metadata) or standalone, in order to create as many CRUD-Dao-Service-DTO like classes you need.
Then, with the T4, you can create a new project, add it as long as a Entity Developer empty model, export it as project template, and make a IWizard with a custom dialog as show in the example you referenced.
In this IWizard, you ask for the Connection String, then you make text replacement with IWizard replacement and... you're done! User makes a "Update Model from Database" and the Entities are done.
I hope this hints are enough to start!

Customizing Linq-to-SQL DBML template

I am using Linq-to-sql for generating code for database interaction. I need the generated code to include Interfaces for each concrete class, and believe the only easy way to achieve this is to modify the Linq-to-SQL code generating template.
I have seen a guide by Damien Guard (http://damieng.com/blog/2008/09/14/linq-to-sql-template-for-visual-studio-2008), however I have 2 constraints prohibiting the approach:
I am on VS2010; and
I am working on a Dev environment in which I cannot install non-approved tools/etc.
Are there any other ways to modify how the Linq-to-sql code is generated the template for VS2010 that do not require installing 3rd party templates/etc?
Alternatively, are there any other easy ways to reverse engineer Interfaces to match existing concrete classes?

Is it possible to generate from code using T4

I have used T4 to generate partial classes from some input file (XML, etc) and then hand code additional partial bits onto those generated classes.
Is it possible to go the other way? To hand craft partial classes, and use T4 to template boiler plate bits to them?
Obviously I can't use reflection to look for the classes since it's not compiled yet, but I see Visual Studio inspect uncompiled code for different utilities. Perhaps Visual Studio offers some feature to support this I don't know about. Long shot, I guess.
Thanks
Also, you can use T4 with VS's CodeModel to read the code in your project without compiling and then generate from that metadata.
There's some pointers to examples here: http://blogs.msdn.com/b/garethj/archive/2009/09/25/dte-and-t4-better-together.aspx
Actually, T4 is used this way frequently. Yes, it requires reflection, but partial classes compile even if bits of them aren't generated yet. I would look at examples for generating strongly typed views as described here for examples of using reflection to generate new files.

Using T4 templates to generate ViewModels

In my mind this sounds like a superb idea. Using the EnvDTE would make this possible too, so why isn't there more examples on this available?
Maybe I'm missing an disadvantage of doing this...?
Any pointer to good T4 and EnvDTE resources would be great. :)
You probably don't see it around much because it's actually quite difficult to implement well. I've been using T4 to generate model classes from WCF DTOs for use in an WinForms MVP variant for a while now, and it took quite some time to get it working right.
Using a class as a "data" source for a template is pretty difficult in-and-of itself. You'll need to choose between using reflection (or a similar API) to read compiled IL or CodeDom to read the source code. If you choose to work with compiled assemblies, you'll need to contend with problems like file locking and loading referenced assemblies. If you choose to work with source code, you'll need to deal with potential uncompilable code.
Once you've made that decision, copying properties will be the most trivial thing you'll need to do. You'll also need to make decisions about which interfaces and attributes (if any) on the source class should be reimplemented/copied to the generated class. Depending on how you're implementing things like validation, this can raise all sorts of little, picky problems. There's are also a lot of fun decisions to make around how to handle inheritance hierarchies and references to other model classes.
All of the above is addressable, but a one-size-fits-all approach would be pretty hard to implement. Returning to the "example" part of your question, there's also the potential issue of doing quite so much work without getting paid for it. I'd love to be able to share the T4 I created for model generation, but it belongs to my employer, and I have better things to do with my spare time than re-implement the approach for posting on the web...
Using a class as a "data" source for a template is pretty difficult
This is wrong. Look at asp mvc 3 scaffolding.
http://blog.stevensanderson.com/2011/01/13/scaffold-your-aspnet-mvc-3-project-with-the-mvcscaffolding-package/

Resources