Walkthrough: Creating a Custom Data Generator
As you develop your database schema, you can test proposed changes more effectively by filling a test database with data that represents production data as closely as possible. Custom data generators provide test data that meets your specifications more exactly than built-in data generators can. For example, you can create a generator to fill a table column with random names from a list that you specify or with numbers in a range that you specify. For more information, see Overview of Generating Data.
In this walkthrough, you create a custom generator that generates a random integer between zero and an upper limit that the user of the generator specifies. In this walkthrough, you perform the following tasks:
Create a class that inherits from Generator.
Create an input property to specify the upper limit of the data to be generated.
Create an output property to use as the generator output.
Override the OnGenerateNextValues method to generate the data.
For information about security issues that you should consider before you create a custom data generator, see Security of Data Generators.
Prerequisites
To complete this walkthrough, you need the following:
- Visual Studio Team Edition for Database Professionals
To create a custom generator
Create a class library project, and name it TestGenerator.
In Solution Explorer, right-click the project, and click Add Reference.
The Add Reference dialog box appears.
Click the .NET tab.
In the Component Name list, click Microsoft.VisualStudio.TeamSystem.Data, and then click OK.
(Optional, Visual Basic only) In Solution Explorer, click Show All Files, and expand the References node to verify the new reference.
At the top of the Code window, before the class declaration, add the following line of code:
Imports Microsoft.VisualStudio.TeamSystem.Data.DataGenerator
using Microsoft.VisualStudio.TeamSystem.Data.DataGenerator;
Rename the class from
Class1
toTestGenerator
.Warning
By default, the name that you give your class is the name that appears in the list in the Generator column in the Column Details window. You should specify a name that does not conflict with the name of a built-in generator or of another custom generator.
Public Class TestGenerator End Class
public class TestGenerator { }
Specify that your class inherits from Generator, as the following example shows:
Public Class TestGenerator Inherits Generator End Class
public class TestGenerator: Generator { }
On the File menu, click Save All.
Adding Input and Output Properties to the Generator
In the previous section, you created a class that inherited from the Generator class. In this section, you add an input and an output property to your class. Input properties appear at design time in the Properties window, and you can use them to configure the generator. Output properties contain the values that are used to generate data. Output properties also indicate what types of data this generator can produce.
To add an input and an output property
Create a member variable that is named
mLimit
, as the following example shows:Dim limitValue As Integer
int limitValue;
Create a property that is named
Limit
that sets and returns the member variablemLimit
, as the following example shows:Public Property Limit() As Integer Set(ByVal value As Integer) limitValue = value End Set Get Return limitValue End Get End Property
public int Limit { // The get is optional for input properties set {limitValue = value;} get {return limitValue;} }
Add InputAttribute to the
Limit
property, as the following example shows:<Input(Name:="Limit", Description:="The upper limit of the data that is generated.")> _ Public Property Limit() As Integer
[Input(Name="Limit", Description="The upper limit of the data that is generated.")] public int Limit
Create a member variable that is named
mRandom
, as the following example shows:Dim randomValue As Integer
int randomValue;
Create a read-only property that is named
RandomNumber
and that returns the member variablemRandom
, as the following example shows:Public ReadOnly Property RandomNumber() As Integer Get Return randomValue End Get End Property
public int RandomNumber { get {return randomValue;} }
Add OutputAttribute to the
RandomNumber
property, as the following example shows:<Output()> _ Public ReadOnly Property RandomNumber() As Integer
[Output] public int RandomNumber
On the File menu, click Save All.
Overriding the OnGenerateNextValues Method
Visual Studio Team Edition for Database Professionals calls the OnGenerateNextValues method of each generator for each set of values that it needs. When you create a data generator, you should override this method to provide logic that generates values for each of your output properties.
To override the OnGenerateNextValues method
Create a member variable that is an instance of the Random class, as the following example shows:
Dim random As New Random
Random random = new Random();
Note
This step creates a nondeterministic data generator. To create a deterministic data generator, pass Seed as an argument to the Random constructor.
Override the OnGenerateNextValues method, as the following example shows:
Protected Overrides Sub OnGenerateNextValues() randomValue = CInt(random.NextDouble * Limit) End Sub
protected override void OnGenerateNextValues() { randomValue = (int)(random.NextDouble() * Limit); }
On the File menu, click Save All.
Signing the Generator
You must sign all custom data generators with a strong name before you register them.
To sign the generator with a strong name
On the Project menu, click TestGenerator Properties.
On the Signing tab, select the Sign the assembly check box.
In the Choose a strong name key file box, click <New...>.
In the Key file name box, type TestGeneratorKey.
Type and confirm a password, and then click OK.
When you build your solution, the key file is used to sign the assembly.
On the File menu, click Save All.
On the Build menu, click Build Solution.
You have created a custom data generator.
Next Steps
Now that you have built your data generator, you must register it on your computer before you can use the generator. You can register the data generator manually, as described in Walkthrough: Registering a Custom Data Generator, or you can build a deployment project that registers the data generator automatically.
See Also
Tasks
Walkthrough: Deploying a Custom Generator
How to: Add Input Properties to a Data Generator
How to: Add Output Properties to a Data Generator
How to: Register Custom Data Generators
How to: Create Custom Data Generators
Reference
Microsoft.VisualStudio.TeamSystem.Data.DataGenerator
Other Resources
Creating Custom Generators
An Overview of Data Generator Extensibility
Database Refactoring Walkthroughs