In-Depth

Build a Custom Data-Bound Generator

Learn how to write custom data-bound generator.

Build a Custom Data-Bound Generator
Learn how to write a custom data-bound generator.
by Jeff Levinson

May 29, 2007

In this information-packed column, I will show you how to write a custom data-bound generator (DBG) for Visual Studio 2005 Team Edition for Database Professionals (DB Pros). You can download the solution code associated with this column (it's available in both C# and VB). This generator is almost identical to the one that comes with DB Pros except for one difference — it reads data sequentially (and you'll find it easy to update to allow the user to select between sequential or random). The generator has some other limitations because of space, but you can modify this to suit your needs.

A data-bound generator is a bit more complicated than a standard generator because you don't know the output ahead of time. You have to create not only the DBG, but also the Data Bound Generator Designer. The designer shows up in the drop-down list in the column details window (see Figure 1).

Start building your DBG by creating the designer. Create a new C# Class Library project called CustomDataGenerators. Rename Class1 to SequentialGeneratorDesigner. Add a reference to Microsoft.VisualStudio.TeamSystem.Data. Reference the class file named "SequentialGeneratorDesigner.cs" in the code download. This class inherits from the base DefaultGeneratorDesigner, which simplifies things for you.

You have to override two key methods in this class — the OnInputValueChanged method and the GetOutputs method. The OnInputValueChanged method is called whenever an input to your generator is changed (these inputs are decorated with the InputAttribute discussed later in this column). In this case, the code is simply checking to ensure the Connection Info and Select Statement are stored.

The GetOutputs method is more complicated. This method returns the information displayed in the designer. For the purpose of the DBG, the GetOutputs method retrieves the schema returned by the select statement (because you need to be able to choose the correct column of data to bind to in the designer). Each returned row represents a column in the schema. The code pulls the column information out for display in the drop-down list.

The heavy-lifting class is the SequentialDataBoundGenerator. Add this class to your solution (see SequentialDataBoundGenerator.cs in the code download). This class inherits from the base Generator class (all generators should inherit from this class or implement the IGenerator interface — trust me, using this class is the easiest). It is decorated with a GeneratorStyles attribute that links the Sequential DBG to the Sequential designer.

The inputs are not just properties; rather they are decorated with the InputAttribute that has a "friendly name" value. You'll see these inputs pop up in the Properties window. On the ConnectionInfo property, you should use the Editor Type setting and the designer that is in the Web.UI.Design space (this should look familiar). This gives you a button in the property window that brings up a larger Connection String Editor window. Other editor windows are available; however, the one used by DB Pros is sealed so you cannot use it.

As for the methods, the OnInitialize method calls out to the database and fills the internal dataset. The OnGenerateNextValues method points to the current record from which data will be retrieved. The OnGetOutputValue method returns a value to Visual Studio for use in populating data. The base.OutputKey returns the item that you selected in the IDE for a given column. Finally, the StripBrackets method undoes the formatting on the column names used to populate the drop-down list.

Deploying the DBG is straightforward, and this process is the same for all custom generators. First, sign the assembly by clicking on the Signing tab of the Project Properties window, selecting Sign the Assembly and generating the key. Once you've completed this process, build the project and open a Visual Studio Command Window (from within Visual Studio) and enter this command:

? System.Reflection.Assembly.LoadFrom([Path to file here]).FullName

Next, copy the Public Key Token from the output.

After you've copied the key, create a new XML file named CustomDataGenerators.Extensions.xml (the name of the new file and the assembly name must match). Add the information from CustomDataGenerator.Extensions.xml in the code download to the new XML file, making sure to replace the PublicKeyToken value with the one you just copied. Save the file and copy it to this folder:

C:\Program Files\Microsoft Visual Studio 8\DBPro folder 

Next, copy CustomDataGenerators.dll to this folder:

C:\Program Files\Microsoft Visual Studio 8\DBPro\Extensions

And, this folder:

C:\Program Files\Microsoft Visual Studio 8\Common7\IDE\PrivateAssemblies

You can also just add it to the DLL file to GAC.

Finally, close all instances of Visual Studio on your desktop and restart. Create a new Database project, reverse-engineer a schema and create a new data-generation plan. Now, you can select Sequential Data Bound Generator from the list of available generators.

About the Author
Jeff Levinson is a senior consultant at Accentient, Inc. He specializes in methodology and process improvement using Visual Studio Team System. He is a former solutions design and integration architect for The Boeing Company. He is a Microsoft Team System MVP and holds the MCAD, MCSD, MCDBA and security certifications. He is co-author of the book Pro Visual Studio 2005 Team System.

About the Author

Jeff Levinson is the Application Lifecycle Management practice lead for Northwest Cadence specializing in process and methodology. He is the co-author of "Pro Visual Studio Team System with Database Professionals" (Apress 2007), the author of "Building Client/Server Applications with VB.NET" (Apress 2003) and has written numerous articles. He is an MCAD, MCSD, MCDBA, MCT and is a Team System MVP. He has a Masters in Software Engineering from Carnegie Mellon University and is a former Solutions Design and Integration Architect for The Boeing Company. You can reach him at [email protected].

comments powered by Disqus

Featured

Subscribe on YouTube