Generating Data Transfer Objects with Seperate Files from a Schema Using T4

by jmorris 3. December 2009 04:21

T4, or Text Template Transformation Toolkit, is a template based solution for generating code that is built into VS2008 (it's also available as an add in VS2005). Alas, it has minimum support in VS2008 in that there are are no Visual Studio Template for adding a T4 template - you cannot just right click Add > New Item and add a T4 file to you project. However, you can create a new file and change the extension to ".tt" and VS will know that you are adding a T4 file to the project (after a security prompt asking you if you want to really add a T4 file) and create the appropriate T4 template and .cs code behind file that accompanies each .tt file. For a detailed explaination of how to do this, please see Hanselman's post here.

T4 templates are pretty cool once you get the hang of some of nuances of the editor, which is somewhat lacking in features (reminds me of using Notepad to write Assembly code in college). There are in fact at least two VS add ons that add some degree of intellisense and code/syntax highlighting: Clarius Visual T4 and Tangible T4 Editor for VS. They both offer free developer editions with limited functionality if you just want to get a feel for what they can do without forking out the cash.

Out of the box, T4 templates have one glareing weakness: only one template (.tt) file can be associated with one output file (.cs, et al). This is not really ideal in that we typically associate one source code artifact (class, sproc, etc) with it's own file. This makes it easier to grok, manage, read a projects source and also makes versioning easier using version control software such as Git or Subversion. It's much easier to track changes to a single class in a file than multiple classes in a file. With a little work and a little help it is possible to generate multiple source files from template files, however.

So, T4 aside, what are Data Transfer Objects (DTOs) and why do need them? DTOs are exactly what they propose to be: objects containg data, typically corresponding to a single record in a database table. In my opinion, they are anemic in that they contain NO behavior whatsoever. They are the data...constrast this with domain objects, which are data and behavior. A very typical scenario involving DTOs are situations where data must be moved from one part of the system, to another where they are consumed and used, likely by a domain object(s). For instance, we may use an ORM such as NHibernate or Entity Framework to access the database, bring back a set of data and then map it a DTO.

In many cases your DTOs are mapped directly to your database schema in that there is a one to one mapping between table and object field or property. In this situation, manually creating an object per entity becomes tedious and scales poorly in terms of developer productivity. This is where using a code generation solution, such as one created with T4 really shines.

For example, given the following schema, generate a DTO for each entity:

The first step is getting enough information about the tables from the database metatables so that you can generate objects for each table. Assuming you are mapping to pure DTOs, you can ignore any of the relationships in the form of 'Has A' in your objects. It's not these relationships do not exist; they do, just not explictly. Instead you maintain the relationships through properties that represent the foriegn keys between the objects. An obvious benefit to this sort of convention is that you immediatly resolve the potential n+1 problems inherit with ORMs and lazy loading, at the expense some other features of ORMs, such as object tracking. IMO ignoring these relationships is a personal preference as well as a architectural concern; for this example I am ignoring these relations.

The following sproc is an example of how to get this data from a database, in this case I am using MS SQL Server 2008:



This stored procedure returns a record describing the table and each column of the table or at least the relevent parts: name, data type, and whether or not the column is a primary key. This sproc is called from a T4 template to load a description of each table and it's columns into memory. Here is the relevent code:



In order to generate seperate files for each artifact generated, we will be using three seperate T4 templates: GenerateEntities.tt, Entity.tt, and MultiOuput.tt. GenerateEntities.tt contains the code above as well as another code block in which it loops through the results returned from GetTypes() and uses the Entity.tt template to generate the artifact. The MultiOuput.tt takes the code generated by GenerateEntities.tt and Entity.tt to write the output file to disk and add the file Visual Studio. Note that MultiOutput.tt comes from one of many excellant posts by Oleg Sych and that an updated version of the file is purported to be available with the T4 Toolkit up on CodePlex.com.




The code above loops through the table definitions and uses remoting to set a property on the Entity.tt template with each value. Finally, the MultiOutput.tt.ProcessTemplate(...) method is called which writes the output of Entity.tt to disk and adds the file to Visual Studio. Entity.tt is pretty straight forward:



When the solution is saved, the T4 templating engine will process the templates a file will be added to Visual Studio under the GenerateEntities.tt file that represents a DTO for each table in the database.



References:

  1. http://www.hanselman.com/blog/T4TextTemplateTransformationToolkitCodeGenerationBestKeptVisualStudioSecret.aspx
  2. http://www.codeplex.com/t4toolbox
  3. http://www.olegsych.com/2008/03/how-to-generate-multiple-outputs-from-single-t4-template/

Tags: , , , , ,

Comments

2/23/2010 5:48:48 AM #

Michael Nurre

Nice post. Take a look at Damien Guard's Manager.tt template. It splits files into multiple outputs without having to install any additional software (I hate un-necessary dependencies). I've modified his template to be able to output the files to different folders and even different projects. Very handy.

Cheers.

Michael Nurre United States | Reply

12/16/2010 6:42:04 PM #

trackback

T4 Templates Part Duex

T4 Templates Part Duex

Rantdriven.com | Reply

6/22/2011 3:32:34 AM #

escort ankara

thank you admin http://www.bayanescortlarim.org

escort ankara Turkey | Reply

6/24/2011 2:07:34 AM #

escort bayan ankara

thank you admin http://www.escortbayanankara.com

escort bayan ankara Turkey | Reply

11/23/2011 12:01:22 PM #

Technology Blog

Just want to say your article is as surprising. The clarity in your post is just excellent and i can assume you\'re an expert on this subject. Fine with your permission allow me to grab your RSS feed to keep updated with forthcoming post. Thanks a million and please carry on the gratifying work.  Thanks !

Technology Blog | Reply

11/26/2011 11:02:43 PM #

security toronto

Hey , many thanks you for bringing up this topic on this website. I wound up inside your blog right after researching physical fitness connected issues on Yahoo… guess I lost track of what I had been performing! Anyway I’ll be back once again within the long run to check out your blog posts down the road.

security toronto | Reply

12/7/2011 12:59:47 AM #

Levrige Business Evolution Consultants

As a business owner you have many challenges and responsibilities.  Using Internet technology correctly can help you grow your business and increase sales quickly and easily without a huge investment.  Evolve your business and start working less while making more by leveraging Internet technology in your marketing.

Levrige Business Evolution Consultants | Reply

12/8/2011 6:01:12 AM #

diy burglar alarm

Great blog, i really like the way you write
I linked your blog to mine, please contact question@youralarmstore.com or http://www.youralarmstore.com if you like me to remove your link.

diy burglar alarm | Reply

12/23/2011 6:02:13 PM #

Toronto security guard companies

Hey , Just to follow up on the up-date of this topic on your site and would really want to let you know simply how much I valued the time you took to publish this handy post. In the post, you actually spoke on how to actually handle this concern with all comfort. It would be my pleasure to get some more concepts from your site and come as much as offer some others what I have benefited from you.

Toronto security guard companies | Reply

Add comment


(Will show your Gravatar icon)

  Country flag

biuquote
  • Comment
  • Preview
Loading



Who Am I?

My name is Jeff Morris a software engineer/developer in Southern California, USA. My platform du jour is dotnet, but I dabble on the darkside occasionally.

Tag cloud

Page List