Share via


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

  1. Create a class library project, and name it TestGenerator.

  2. In Solution Explorer, right-click the project, and click Add Reference.

    The Add Reference dialog box appears.

  3. Click the .NET tab.

  4. In the Component Name list, click Microsoft.VisualStudio.TeamSystem.Data, and then click OK.

  5. (Optional, Visual Basic only) In Solution Explorer, click Show All Files, and expand the References node to verify the new reference.

  6. 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;
    
  7. Rename the class from Class1 to TestGenerator.

    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
    {
    }
    
  8. Specify that your class inherits from Generator, as the following example shows:

    Public Class TestGenerator
        Inherits Generator
    
    End Class
    
    public class TestGenerator: Generator
    {
    }
    
  9. 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

  1. Create a member variable that is named mLimit, as the following example shows:

    Dim limitValue As Integer
    
    int limitValue;
    
  2. Create a property that is named Limit that sets and returns the member variable mLimit, 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;}
    }
    
  3. 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
    
  4. Create a member variable that is named mRandom, as the following example shows:

    Dim randomValue As Integer
    
    int randomValue;
    
  5. Create a read-only property that is named RandomNumber and that returns the member variable mRandom, as the following example shows:

    Public ReadOnly Property RandomNumber() As Integer
        Get
           Return randomValue
        End Get
    End Property
    
    public int RandomNumber
    {
        get {return randomValue;}
    } 
    
  6. Add OutputAttribute to the RandomNumber property, as the following example shows:

    <Output()> _
    Public ReadOnly Property RandomNumber() As Integer
    
    [Output]
    public int RandomNumber
    
  7. 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

  1. 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.

  2. 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);
    }
    
  3. 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

  1. On the Project menu, click TestGenerator Properties.

  2. On the Signing tab, select the Sign the assembly check box.

  3. In the Choose a strong name key file box, click <New...>.

  4. In the Key file name box, type TestGeneratorKey.

  5. Type and confirm a password, and then click OK.

    When you build your solution, the key file is used to sign the assembly.

  6. On the File menu, click Save All.

  7. 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