The best kept secret in Redmond, WA are the new T4 Preprocess Text Templates that shipped in Visual Studio 2010 Beta1 and Beta2. In just a few minutes you’ll be in the know and using them.

Background – Condensed

T4 = text template transformation toolkit

T4 templates have been available since Visual Studio 2005 and are part of DSL (Domain Specific Languages) documentation.

In Visual Studio 2010 the templates are MUCH easier to use because they are preprocessed. When you save your T4 template, it is compiled. The T4 template is now “processed” at compile time. This was not the case in previous releases.

Your compiled template is just another type in an assembly that you can now instantiate and call its single method, TransformText. The only run-time requirement to use T4 templates is the .NET 2.0 Framework.

Prerequisites

Visual Studio 2010

T4 Editor Plug In

Visual Studio 2010 does not provide any editing assistance when editing T4 templates (.tt files). You can search the Internet for T4 Editor and you’ll find some information and products.

I’m currently using the Tangible T4 Editor (free) that you can get on the Visual Studio Gallery here:

The editor is an Alpha version, has a few limitations but I was able to author the below template in a few minutes.

Our Scenario

We have been tasked with creating some code that we can pass data fields to and that code will create a VB.NET property. This code will be part of a larger program that is fed information and creates class files.

Before T4 Preprocessed templates we had several options available to us. Write code to spit out the required property, buy a 3rd party product, use CodeDom, etc.

Since we just installed Visual Studio 2010 Beta2, we are going for the productive, inexpensive, simple and out of the box solution, T4 Preprocessed Text Template.

Our Test Bench

This simple application simulates the data harness that will ultimately feed our T4 template and consume its output. Set a few data fields and press Generate Code.

Code Generation – It’s all about the data

Code generation requires metadata to drive the output. With the new T4 templates, getting metadata into your T4 template is a simple matter of a partial class. The members in the partial class are available at design time and run-time. I know this seems so simple, but T4 did not always work this way. Now you have full design time strong typing and instant compile time verification of your template and code.

To keep this example as simple as possible, I’ve exposed the metadata directly as properties and passed values for them in the constructor of our Partial Class PropertyTemplate. For simple scenarios this totally fine. For a larger or multi-language code generation applications, a better solution is to expose the metadata through an Interface. The Interface type can contain metadata and helper methods that can be used in your T4 template.

Kathleen Dollard and I have spoken a good bit about this and think using Interfaces for metadata and helper methods makes the most sense. I’m sure Kathleen will write more on this subject and I know I will.

In the below T4TemplateLibrary project you can see how I’ve set up the project. I had to turn on “Show All Files” to see the hidden file, “PropertyTemplate.vb” below the “PropertyTemplate.tt” file.

Open the hidden “PropertyTemplate.vb” file up and you will see the type of code you and I used to have to write to generate code. Now T4 does it for us!

The below partial class is overly simple and exposes metadata that will be consumed by the T4 template.

T4 Template

To add a T4 template to your project, select Add New Item, then type “preprocessed” in the search box. Select the Preprocess Text Template.

If you used classic ASP, then T4 template editing is a skill you already have. In classic ASP we used <% and <%=. In T4 we use <# and <#=. The reason for this change is so that T4 can be used to generate classic ASP and ASP.NET code. If you didn’t have the joy of shipping classic ASP web sites, no worries. 15 minutes of trail and error and you’ll be fully qualified.

The below image shows the free Tangible T4 Editor Plug-In IntelliSense in action. It provides IntelliSense for T4 directives and template specific constructs like <#. It also colorizes the template code. Colorizing makes editing the template much easier too. One current limitation is that its IntelliSense engine does list member properties defined in the partial class for the template.

The below code block is the T4 template. It has a language directive at the top followed by static text and code blocks.

<#= is the same as the ASP Response.Write. It writes the result of the expression into the template. For example, if the Scope is Public, <#= Me.Scope #> would yield Public when the template is transformed at run-time.

<# is the beginning of a code block. Notice how the IsReadOnly property is tested. Basic on the result, an entire block of code can either run or be by-passed. This feature is so cool when you bring loops from LINQ queries into the picture.

One thing you’ll notice is that all the code blocks are left justified. Yes, this makes reading the template a little harder. However, if you don’t do this, the resulting code will be indented the same amount as the code block indentation.

So what I do when editing my templates is to indent everything to make it easier to edit the template. When I”m done, I go back and move the code to the left.

Close

There is a fair amount of information on Visual Studio 2005/2008 T4 templates. When reading these blog posts, magazine articles and MSDN Documentation, keep in mind that the new T4 templates do not have wide spread documentation yet. Most of what you will find is applicable to editing of the templates and directives used in the templates. Beyond that, just filter the information and you’ll stay on course.

Visual Studio 2010 T4 Preprocessed Text Templates are a great tool for your toolbox. Hope to see you use the best kept secret in Redmond.

I have received great feedback from users of XAML Power Toys. In response to their suggestions and some isolated problems loading some assemblies I spent the day testing and refining assembly loading in the solutions they sent me. Thank you for your suggestions and for reporting issues.

New Features In v3.4

Corrected the loading of assemblies. (When I write my Code Project article on XAML Power Toys, I’ll write about why this was challenging.)

All dialogs now appear in the Windows Taskbar. (One user reported Task Switching back and forth and the About Box was hidden. This corrects this problem.)

In the last few days I’ve been getting questions about XAML Power Toys like; why did I write it; what was the motivation behind the work, etc?

I was actually beginning the UI development in my soon to be released Ocean Platform (details below) and found myself looking at creating 8 forms for Ocean’s metadata maintenance and code generation.

I was thinking, if I lay Ocean down for a week or two, I can write a tool that will allow me to author Ocean’s UI in no time at all. Ocean has it’s own built-in advanced UI generation but Ocean is designed to consume metadata. Since I didn’t have metadata yet, I needed to tool to help me author the UI forms.

You see, I really didn’t want to hand type 8 more forms if I could get the computer to do it for me. So because I’m lazy, XAML Power Toys was born.

Time

Time is one resource that can not be replaced. It goes by so fast and seems to slip between our fingers very easily. It’s vulnerable and somehow “things” inject themselves into our time.

As developers, we can (should) take steps to maximize our time. We can maximize our time by having the correct tools, proven workflow and an understanding how to use these.

We developers need to manufacture time by accomplishing tasks faster and with laser surgery accuracy. In our world the goal is to deliver defect free, maintainable, standards based code.

The best way I know to meet this goal and to manufacture time is to generate as much production code as possible.

This gives the developer more time to allocate to a better application design, to learn new patterns and to keep up with the flow of new technologies that can only be compared to the water flowing at Niagara Fall’s. I wrote Ocean to give me time back.

Ocean

Back in May of 2008 I wrote this blog post: Metadata – a Voice Crying in the Wilderness, Hey! I’m Over Here. This was a call to architects to consume the warehouses of metadata that most projects have but do not ever get into the hands of the developers. You say that’s not entirely true. OK, then why do developers type ToolTip text into their forms? Why is a developer typing XML comments on business objects?

These are two over simplified examples of metadata that is located in a design document somewhere, but is not in a format or physical location that makes it easily consumable by the developer or a code generation product. The term disparate data storage comes to mind.

I strongly agree with one of my mentors, Bill Jones that developers should own their code generation system. We live in a very fast and volatile world. Things simply change a lot. Today’s architects and developers need full access to the code generation system’s data model, templates, template engine and generation code. Yes, I know this will raise a few eyebrows, especially if your company provides Code Generation software. But I wrote Ocean because I couldn’t find an off the shelf Code Generation product that did what I needed, was easy to understand, manage and had examples for VB.NET developers.

Right behind code generation an application of any size requires a set of libraries that sit on top of .NET that provide the services a Line of Business (LOB) application requires.

Ocean provides a light weight, comprehensive framework that works in WPF and Silverlight. This was one of the requirements of Ocean, that it support Silverlight.

Ocean is divided into five assemblies and I’ll be adding one more that is specific to Silverlight. Below are some of the highlights of each assembly.

Ocean is the UI for maintaining the metadata the Ocean Code Generator consumes. It also makes calls to the OceanCodeGenerator to generate an object like a stored procedure, trigger, DAL, BLL, entity object or UI object. I’m going to also add support to generate MVVM ViewModels.

OceanCodeGenerator is an ASP.NET web that runs in a secondary AppDomain under Ocean’s control. This assembly takes a request from Ocean and generates an object.

OceanFramework is the code that runs in WPF and Silverlight. It requires no code changes between the two platforms. This assembly provides declarative validation on business entity objects, UI case correction and business entity base classes. The validation is attribute based, but can also be declared in code. Compiled, this library weights in at only 62KB. For Silverlight, that is a real winner.

OceanFrameworkSQL is a light weight and super fast replacement for the DAAB I loved in .NET 1.1. This library weights in at 29KB. In addition to enabling single line of code database calls from your DAL, this library also has built in concurrency checking and object building from a DataReader or DataRow. I’ve run tests and was able to build 19,000 unique objects per second using this library. This is on par with LINQ To SQL and other object loading techniques. Check out my three blog posts on Performance to review the test data.

OceanFrameworkWPF provides WPF specific code, commands, converters, UserControls and Custom Controls. Most of this code is already on my blog.

OceanFrameworkSilverlight to be written soon. Will provide Silverlight specific code and controls.

Ocean’s Workflow

Ocean does not claim or attempt to be all things to all developers. It is not a silver bullet. It’s a metadata driven LOB application development and maintenance tool.

Note that the Ocean libraries can be used without the code generation pieces. You have probably noticed the XAML Power Toys uses the OceanFramework library.

My current workflow is:

Design the data model using SQL Server Tools

Sync Ocean with that database

Enter in all metadata for the application

Press a button and all the stored procedures, DAL, BLL and Entity objects are immediately generated

Easily generate additional required stored procedures, DAL, BLL and Entity objects for subsets of a class. For example, the customer class has 25 properties. The customer search results only has 5 properties. Ocean allows you to create everything for this search results class in a split second and assists you with creating the DataTemplate for the UI.

Use Ocean to generate each UI object as the developer requires

Ocean handles changes to the data model and application requirements with ease by allowing some or all objects to be recreated on demand.

Closing

So on nights and weekends, I’m back to working on Ocean and hope to ship, including detailed documentation by the end of October or early November. When released, Ocean will appear on Code Project and my blog.

The video shows you all the effects contained in the library. This library is free and super cool! Thank you!

Dr. WPF’s VB.NET & C# Snippets Library

I was working with Dr. WPF the other day and he was typing in three characters and all of a sudden the code window was painted with a complete stub including the surrounding region for a dependency property. I stopped him and asked, “how did you just do that?” OK, I admit it. I have not used code snippets before since I have an Add-In I wrote that does this. However, the snippets were easier and less typing. I’m now a snippet convert.

On Friday night I lit my hair on fire cranked up the jams and spent the weekend adding full support for Silverlight 2 RC0 to XAML Power Toys.

XAML Power Toys is a multi-AppDomain Visual Studio Add-In for WPF and Silverlight developers that provides features to Visual Studio to enhance the XAML editing experience and the laying out of forms, DataGrids, ListViews.

Silverlight developers you will want to watch at least two of the video on the XAML Power Toys home page; Installation and Setup and the Silverlight Features. This will help you get up and running real fast and visually see how productive you will become when your toolbox has XAML Power Toys in it.

Current XAML Power Toys Users

Current XAML Power Toys users, please upgrade even if you are not a Silverlight developer. I have made many fixes and corrected things I didn’t like and increased stability even more.

WPF developers, you do not have to install the Silverlight RC0 bits if you don’t want to. The only reason for installing Silverlight RC0 is so that you can do Silverlight development prior to the RTM and because the XAML spit for Silverlight projects is RC0 compliant.

All Developers

This week Pete O’Hanlon will be posting a complete walk through for using his MO XAML Power Tools Add-In to very quickly create Silverlight friendly business object and then use XAML Power Toys to lay it out. Thanks Pete for doing this!

I know it has been two weeks, but it is finally here. Trust me, I’ve crawled through a hot desert and survived a tidal wave to get this code released. Another way to say it, I ate from a table full of humble pies! These multi AppDomain Add-In’s can present challenges in a number of areas including security. I’ll write a full Code Project article on the source code and how it works over the next two weeks or so.

Goal

The primary goal of XAML Power Toys is to deliver tools that enable developers to quickly layout and maintain Line of Business Application forms. You’ll notice that the below features are business form focused. This does not limit the use of the software, I’m just providing the current target project type.

After Silverlight RTM is released, I’ll update this software to render XAML targeted for Silverlight also.

Features

Create Business Form For Class

Create Business Form

Show Fields List For Class

Group Into

Border, StackPanel, WrapPanel, etc.

Remove Designer XAML

Remove All Margins

Edit Grid Column and Rows

Videos

I’m begging every developer in the strongest possible terms, please view the below videos. They will get you up and running and productive in a very short time. These are all new videos updated on 16 Sept 2008.

Code Name: Hawaii

There is a rumor going around that I went to Hawaii this past weekend, (13-14 September) just to purchase new Hawaiian shirts. Yes, that rumor is true. I had a great time, got a super price on the ticket and got the new shirts I needed. Also worked on this code a good bit during the trip.

So in remembrance of shirt buying spree in Oahu, this code based is named; Hawaii.

Cider Designer Goodness

The other day I was speaking with Mark Boulter, a Program Manager and Technical Lead on the Cider Team about the Cider Designer and why it was programmed the way it is. He explained to me that one of the primary goals was to give developers the ability to lay a WPF form out using x,y coordinate layout and then quickly convert that layout to a table or grid layout. This conversation helped me to use the designer rather than fight it.

In the above XAML Power Toys Accessories video, I demonstrate using the Cider Designer to quickly lay a form out using control drag and drop and then use XAML Power Toys to convert the layout to a flow layout. In the video, you’ll see just how easy and fast you can lay a form out using the Cider Designer.

Version 3

Please leave your feedback, both good and bad.

Josh Smith and I will be leading the work on this project and have already created a decent size list of new features and enhancements.

Thanks To

I want to thank Bea Costa for her outstanding drag and drop example code, Rob Zelt and Josh Smith for their help on this project. It is always a rewarding pleasure to work with them.

I can see another, “Mole like” sprint looming on the horizon. Ideas and great feedback, fueling the hard work to develop useful features for WPF and Silverlight developers.

Welcome to Metadata Island. It’s a beautiful place. Rich in resources that represent the hard work of application designers, business analysts and their customers. Those very long specification meetings. Countless missed meals typing up specifications. Yes we have all contributed to the paradise repository called, Metadata Island. Not to mention this vast metadata resource that sits dormant which companies paid tens of thousands of dollars to create. If you were to get in your boat, the lone guy managing the metadata can be heard on the beaches waving his arms and crying out, “Hey!! I’m over here!”

The Problem

Bottom line; the once thought out and complete metadata never gets off the island after it arrives. Current development tools are not set up to take advantage of developer metadata during the form or code generation processes. Anyone ever used Microsoft Access? Since Access 1.0, the program utilizes the developers metadata stored in the table object. The form and report designers expose this metadata in a ComboBox allow the developer to drag a field to the design surface and Access will create the control and corresponding label. It also has Mr. Wizard that creates forms and reports for you.

Why should our developers have to retype ToolTip text into form property grids? Why should developers spend time entering data into property grids at all, especially the same data that is sitting on Metadata Island? Beside being error prone, software companies are having to pay another person to enter this same data into yet another interface.

Why Did I Write This?

Before I get much further I must explain the context of this blog post. First and foremost, I’m not a Microsoft finger pointer, not by a long shot. We have fantastic tools and outstanding developers that I have deep respect for standing behind them.

I have been to two recent Microsoft events, MIX08 and the 2008 Microsoft MVP Summit. I didn’t hear anything in any keynote, presentation or off-line conversation about any plans for Visual Studio or Visual Studio related tools to consume developer metadata. I feel so strongly about this, so I wrote this blog post to raise the awareness of this need and to demonstrate how simple this is to accomplish.

I like Visual Studio 2008 and Expression Blend 2 and 2.5. I like that I can open a designer and the tool creates classes for me. Someone worked real hard to deliver that tool. Where I get disappointed is when I have a great tool that I can’t use because it can’t consume my metadata.

For example the LINQ to SQL and SQLMetal tools don’t currently add XML comments to my classes or add my attributes to my properties. But this would be easy to accomplish.

Currently there is no way to use the tools and have them populate a control’s properties based on my metadata as the control is being dragged to the design surface. But this would be easy to accomplish.

Video Demonstrations

Please watch the two below videos. The first one demonstrates a program I wrote that uses metadata to generate WPF forms in milliseconds. It can also generate individual controls The second is a short video on the program and how simple it was to write.

When I made the videos, this was using the Alpha One version. Since then I have updated it after getting suggestions. The version in the video works great but has been updated and now looks like the version at the bottom of this post.

The video links require Microsoft Silverlight 1.0. If you do not have it, you will be prompted to install it when you click one of the links. After the short installation is completed, close the browser window that you did the install in and re-click the video you want to watch. You can also download it here. Windows XP or Vista required.

The Solution

We need bridges from our tools to Metadata Island. Bridges is such a friendly and happy word. The phrase, “bridge building or building bridges” is normally used in the context of good, of creating relationships or joining separate objects.

After watching the first video, no one can now say, WPF is not RAD capable. In fact this simple tool is way faster than the great Winforms designer. I spent a days time thinking the tool out, writing and testing it.

I have come up with two bridge building methods I think would work. Of course there are much smarter architects and developers working on tools than me, so maybe they would have a better solution that what I’m proposing here. I’m just praying the ball gets rolling in time for the next release of Visual Studio or possibly a service pack to Visual Studio 2008.

Solution A: Bridge Provider Model

This would be the simplest solution to implement and one that completely decouples the tools from the developers.

In each Visual Studio Solution the developer would place a .dll in the MetadataProvider directory of the solution that was decorated with the required attributes.

When Visual Studio opens the solution, it looks for it, similar to the way it currently does for Visualizer .dlls.

Here is the workflow. When LINQ to SQL or SQLMetal needs to write a class or property object they pass the class name and property name and the MetadataProvider either returns a string or nothing. If nothing, then the tool simply uses its default implementation like it does today. If a string is returned, then that string is used for that object. This would allow developers to create the property with XML comments, attributes and property getter and setter code that they want to use. This way the code the the LINQ to SQL designer generates now looks exactly like code I would have hand coded. Now everyone is happy, very happy. How easy would this be to code into the current tools?

If the developer wanted to generate a form, they would select a business object class that would be used to populate a Data Grid like my program and then generate the code. So in this case, Visual Studio would be interacting with the .dll in the MetadataProvider directory twice. Once to fill up the grid and once when each object is created. Again, very simple workflow and almost zero impact on the current tools.

Advantages

The major advantage to this solution is that it completely decouples the tools from the solution. The tools do not have to be concerned about metadata management. Just call the provider and use what it returns.

Solution B: Metadata Model

This solution would require that a metadata model be established and then developers could populate. This model would be consumed by the tools during object construction.

This model would require that the solution expose templates that could be modified so that the constructed classes, properties and controls be generated exactly if a developer had hand coded them.

Program

I have receieved a few emails about the program so I’ll give the overview.

Most of the columns are editable. The control type column is a ComboBox and the two button columns allow the creation of the control by clicking the button to copy the control to the clipboard as in the demo video or allow the control to be created by dragging the button to a Visual Studio or Blend XAML editor and the control is created there.

You can see I’ve also have put my Code Generator here also. I’ll post some more videos soon on this entire application and demonstrate how RAD WPF development can be.

Close

My program is not a templated solution. It’s coded to generate code they way I would write it. This makes my program very useful to me and anyone that subscribes to my methods and Core library that I have written. We also have a super clean metadata store for our applicaiton generation.

Until the tools catch up, I hope this encourages you to start mining your own rich metadata stores and bringing that well thought out data into your development cycles.