TFDP is designed to integrate with the Visual Studio tools for Databases (VSTDB) projects and to provide T4 templates. The main goals that needed to be met by the extension were:

  • Generate SQL artifacts for existing objects (tables, stored procedures etc) when the definition of that object changes
  • Integrate with source control extensions (TFS, Source Safe, basically anything that implements the Visual Studio source control API)
  • Manual template transformation (like regular T4 templates)

The first goal is realized by integrating with the VSTDB API within Visual Studio and interacting with the Schema Model. The Schema Model parses the SQL files in the database project and builds a in-memory model of the project. This can in turn be used to navigate the database project in a meaningful way. The Schema Model also provides events that notify listeners that changes have been made to the model when you change a file in the editor, for example: add a column to a table. Because the database project listens for file-changes the schema model will parse that specific file and update the in-memory model accordingly.

Integration with the source control extensions was realized by using the already existing QueryEditQuerySave services of Visual Studio. By using this existing service it is easy for generated artifacts to be added to the project and source control so that the developer doesn't have to deal with these tasks manually.

The image below shows the components that make up the TFDP extension:

TFDP Schematic

The main components are:

  • The extension container. This is the host for the processor and it's generators and provides the interaction logic between the processor and Visual Studio/
  • The processor. This contains the application logic that processes the item generation requests from the database project.
  • The generator(s). These are the components that process the model elements that need to be processed.
  • The generation queue (not pictured). This holds all the model elements for which template generation is requested.

All these components together provide the database project with the capability to transform a T4 template for specific model elements in the database project. The following example demonstrates a basic scenario in which a column is added to an existing table:

  • The developer opens the file SomeTable.sql and adds a column called foo
  • The Schema Model is notified of the file change and parses the file
  • In the in-memory model the model element for SomeTable is updated so that the column foo is now part of it
  • Next the ModelChanged event is fired and the extension springs into action
  • The model element is pushed onto the generation queue and the processor is notified that there is work to be done
  • On receiving the notification the processor checks if it has a generator that can handle a model element of the type table
  • The processor executes the generator for table elements.
  • The generator transforms the T4 template that is configured and passes the model element to it as an argument
  • The output of the T4 template is stored in a temporary file and pushed onto the queue of generated artifacts
  • The TFDP extension adds the file to the project if it doesn't exist (or checks out the existing file if it does)

And presto! A new file has been generated and added to the project.

Of course there is quite a lot more to it but in essence the above steps are what happens when you change or add a file to the database project.

To start using the extension you can check the how-to page which shows the steps to follow to make T4 generation work for your project.

Last edited Sep 5, 2011 at 1:23 PM by sandermvanvliet, version 2

Comments

No comments yet.