Login  |    
View Article

Current Articles | Categories | Search | Syndication

Subsonic 3, T4 Templates, SQLite, and WheelMUD

I've spend the weekend figuring out the new Subsonic 3 T4 template based system. This approach is astronomically better than the previous way of doing code generation. I'm going to walk through what I did, so anybody else who wants to use Subsonic 3 can do it without spending 6 or more hours trying to figure it out.

T4 stands for Text Template Transformation Toolkit, and it is a Visual Studio-only technology. Visual Studio does not come with intellisense for this files. There's a lot of manual things that you could do to set this up, but the easiest thing to do is to get and install the Clarius T4 Template Editor. This will create all the registry entries needed, setup up syntax-highlighting, and intellisense, so that you can edit the templates inside Visual Studio. For our purposes, I downloaded and installed Visual T4 Editor Community edition for VS 2008.

The first thing I want to point out, is that I'm using the Pill Crusher Visual Studio theme. You can find it here: http://idehotornot.ning.com/

I want to start with how the templates will work out in an actual Visual Studio project/solution. T4 templates have a *.tt extension. You can compartmentalize code by splitting it into different include files. That's what all the *.ttinclude files are.

Make sure that the project where the templates are, have a reference to Subsonic.Core.

You will need five files:

  • ActiveRecord.tt
  • Context.tt
  • Settings.ttinclude
  • SQLite.ttinclude
  • App.config

The first file you will need to configure is App.config. You will need to setup your connection string to your SQLite database.

Just make sure that the values for providerName and connectionString are correct. Now remember the value for the name attribute, because we will be using it next. We will now edit the Settings.ttinclude file.

There are four things that you will want to change. I have already changed them here for WheelMUD related settings.

  • Namespace - This is the namespace where all of the code will fall into.
  • ConnectionStringName - This is the value that we set up in the App.config file. This tells the templates what and where the database is.
  • DatabaseName - This is something that the templates will use internally.
  • ExcludeTables - This is used to exclude tables from getting processed.

Now we are set to run the templates. To run them, right click on any of the files with a *.tt extension. You will see a "Run Custom Tool" entry in the context menu.

Make sure that you run Context.tt, before you run ActiveRecord.tt. ActiveRecord.cs references code in Context.cs. The first time you run a template you will see this warning dialog:

I checked the "Do not show this message again" box, so that this wouldn't appear every time I saved or ran the temaplate. I suggest you do the same.

Once you run the ActiveRecord.tt template, you will see this:

I changed the templates a bit so that they generate code compliant to our standards. I zipped everything that is needed to make this templates run. This means that the latest version of Subsonic 3 is included as an assembly as well.

WheelMUD Subsonic3 and T4 templates.zip

 

posted @ Sunday, June 14, 2009 11:55 AM by Fastalanasa

Previous Page | Next Page

COMMENTS

I am trying to follow the steps described here and run your sample but getting the following error: Compiling transformation: Metadata file 'System.Data.SQLite' could not be found C:\...\Templates\Context.tt. Although I have a reference to System.Data.SQLite.dll in the project. What am I missing?

posted @ Sunday, July 05, 2009 8:09 AM by Sergey


Sergey,

Make sure that the connectionString value in App.config points to your SQLite database.

posted @ Tuesday, July 07, 2009 12:21 AM by Fastalanasa


Just read the article and that sounds great. The idea that you could create objects on the fly and create relevant datastructure is so cool :)SQLite has limited support for ALTER, and article implies no support for SQLite. But it may work for some alters ie additions (rather than modfications and deletions).Not neen very involved in recent developments, so how does this sit with recent discussions on simplifying the DAL?

posted @ Friday, February 26, 2010 5:03 AM by gioco del casino online


posted @ Thursday, March 06, 2014 7:40 AM


Only registered users may post comments.
Copyright 2007-2014 by WheelMUD  |  Terms Of Use |  Privacy Statement
Web Analytics