Generator Class

Represents the abstract base class for data generators.

Inheritance Hierarchy

System.Object
  Microsoft.Data.Schema.Tools.DataGenerator.Generator

Namespace:  Microsoft.Data.Schema.Tools.DataGenerator
Assembly:  Microsoft.Data.Schema.Tools (in Microsoft.Data.Schema.Tools.dll)

Syntax

'Declaration
<GeneratorAttribute(GetType(DefaultGeneratorDesigner))> _
<CLSCompliantAttribute(True)> _
Public MustInherit Class Generator _
    Implements IGenerator, IDisposable, IExtensionInformation, IExtension
[GeneratorAttribute(typeof(DefaultGeneratorDesigner))]
[CLSCompliantAttribute(true)]
public abstract class Generator : IGenerator, 
    IDisposable, IExtensionInformation, IExtension
[GeneratorAttribute(typeof(DefaultGeneratorDesigner))]
[CLSCompliantAttribute(true)]
public ref class Generator abstract : IGenerator, 
    IDisposable, IExtensionInformation, IExtension
[<AbstractClass>]
[<GeneratorAttribute(typeof(DefaultGeneratorDesigner))>]
[<CLSCompliantAttribute(true)>]
type Generator =  
    class
        interface IGenerator
        interface IDisposable
        interface IExtensionInformation
        interface IExtension
    end
public abstract class Generator implements IGenerator, IDisposable, IExtensionInformation, IExtension

The Generator type exposes the following members.

Constructors

  Name Description
Protected method Generator Called from constructors in derived classes to initialize the Generator class.

Top

Properties

  Name Description
Public property Collation Gets or sets the collation, or linguistically appropriate sorting, of this column.
Public property Column Gets or sets the column.
Public property ColumnName Gets or sets the column name.
Public property Constraints Gets or sets the check constraint information for the column.
Public property DefaultLocaleId Gets or sets the locale ID.
Public property DefaultValue Gets or sets the default value of the column.
Public property ExtensionHandle Gets the extension handle.
Public property MaxLength Gets or sets the maximum length for values in this column.
Public property Nullable Gets or sets a value that indicates whether this column can be set to nulla null reference (Nothing in Visual Basic).
Public property OutputKey Gets or sets the key that is used to retrieve output values.
Public property PercentageNull Gets or sets the percentage of the generated values for this column that the user wants to be nulla null reference (Nothing in Visual Basic). The percentage is expressed as an integer between 0 and 100.
Public property Precision Gets or sets the precision of the data.
Public property RowsToInsert Gets or sets the number of rows to generate.
Public property Scale Gets or sets the scale of the data.
Public property SchemaName Gets or sets the name of the schema to which the column belongs.
Public property Seed Gets or sets the seed. You can use the seed to initialize a deterministic random data generation algorithm.
Public property TableName Gets or sets the name of the table to which the column belongs.
Public property TargetType Gets or sets the target data type of the column.
Public property Unique Gets or sets a value that indicates whether the column that is generated is part of a unique constraint or a primary key constraint.

Top

Methods

  Name Description
Public method Dispose() Releases all resources that are used by the Generator.
Protected method Dispose(Boolean) Releases the unmanaged resources that are used by the Generator and optionally releases the managed resources.
Public method Equals Determines whether the specified Object is equal to the current Object. (Inherited from Object.)
Protected method Finalize Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public method GenerateNextValues Generates the next value according to the data generation algorithm.
Public method GetHashCode Serves as a hash function for a particular type. (Inherited from Object.)
Public method GetOutputValue Retrieves an output value by using the OutputKey.
Public method GetType Gets the Type of the current instance. (Inherited from Object.)
Public method Initialize Initializes the data generator.
Protected method MemberwiseClone Creates a shallow copy of the current Object. (Inherited from Object.)
Protected method OnGenerateNextValues Generates the next value according to the data generation algorithm.
Protected method OnGetOutputValue Retrieves the current set of results that consists of one or more values.
Protected method OnInitialize Initializes the generator.
Protected method OnNextValues Generates the next set of values for a data row.
Protected method OnSetInputValues Sets the input properties of a data generator by using the provided list of named values.
Protected method OnValidateInputs Validates that all the required inputs on the data generator have been set.
Public method SetExtensionHandle Sets the extension handle by using the specified extension handle.
Public method SetInputValues Sets the input values for a data generator.
Public method ToString Returns a string that represents the current object. (Inherited from Object.)
Public method ValidateInputs Validates that all the required inputs on the data generator have been set.

Top

Remarks

If the standard data generators are insufficient, you can create custom data generators. To create a custom data generator, you must create a class that implements IGenerator or inherits from Generator. You identify the class as a data generator by decorating it with GeneratorAttribute.

You can create a custom designer for a custom data generator, or you can use DefaultGeneratorDesigner.

The base class implementation constructs outputs that are based on public properties that are marked with the OutputAttribute. The inputs are set by using the InputAttribute. The use of properties that are marked with attributes provides a simple mechanism for declaring input and output values that are strongly typed.

Examples

The standard data generators cannot generate data to satisfy some check constraints. For example, a check constraint that requires a date to be in one of two distinct ranges cannot use the standard DateTime generator. This example creates a custom data generator that can satisfy such a constraint. The generator accepts two distinct ranges as the input and generates a random date that is in one of the two ranges. For more information, see Walkthrough: Creating a Custom Data Generator for a Check Constraint.

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.VisualStudio.TeamSystem.Data.DataGenerator;

namespace GeneratorDateRangesCS
{
    public class GeneratorDateRangesCS : Generator
    {
        DateTime mRange1Min;
        DateTime mRange1Max;

        DateTime mRange2Min;
        DateTime mRange2Max;

        [Input]
        public DateTime Range1Min
        {
            set {mRange1Min = value;}
        }

        [Input]
        public DateTime Range1Max
        {
            set {mRange1Max = value;}
        }

        [Input]
        public DateTime Range2Min
        {
            set {mRange2Min = value;}
        }

        [Input]
        public DateTime Range2Max
        {
            set {mRange2Max = value;}
        }

        DateTime mRandomDate;

        [Output]
        public DateTime RandomDate
        {
            get {return mRandomDate;}
        }

        Random mRandom;
        Random mRandomRange;

        protected override void OnInitialize(GeneratorInit initInfo)
        {
            // Deterministic
            mRandom = new Random(this.Seed);
            mRandomRange = new Random(this.Seed);  //deterministic

            // Non-deterministic
            //mRandom = new Random();
            //mRandomRange = new Random();

            base.OnInitialize(initInfo);
        }

        protected override void OnGenerateNextValues()
        {
            DateTime min;
            DateTime max;

            // Generate a random date from either range 1 or range 2.
            // Randomly select either range 1 or range 2 by randomly 
            // generating an odd or an even random number.
            if (mRandomRange.Next() % 2 == 0)  //check for odd or even
            {
                min = mRange1Min;
                max = mRange1Max;
            }
            else
            {
                min = mRange2Min;
                max = mRange2Max;
            }

            // The formula for creating a random number in a specific
            // range is: of range + (size of range * random number 
            // between 0 and 1)

            //size of range
            TimeSpan range = max - min;

            // (Size of range * random number between 0 and 1)
            TimeSpan randomNumber = new TimeSpan(
                (long)(range.Ticks * mRandom.NextDouble()));

            // Start of range + (size of range * random number 
            // between 0 and 1)
            mRandomDate = min + randomNumber;
        }
    } // End of class
}     // End of namespace
Imports Microsoft.VisualStudio.TeamSystem.Data.DataGenerator

Public Class GeneratorDateRangesVB
    Inherits Generator

    Dim mRange1Min As Date
    Dim mRange1Max As Date

    Dim mRange2Min As Date
    Dim mRange2Max As Date

    <Input()> _
    Public WriteOnly Property Range1Min() As Date
        Set(ByVal value As Date)
            mRange1Min = value
        End Set
    End Property

    <Input()> _
    Public WriteOnly Property Range1Max() As Date
        Set(ByVal value As Date)
            mRange1Max = value
        End Set
    End Property

    <Input()> _
    Public WriteOnly Property Range2Min() As Date
        Set(ByVal value As Date)
            mRange2Min = value
        End Set
    End Property

    <Input()> _
    Public WriteOnly Property Range2Max() As Date
        Set(ByVal value As Date)
            mRange2Max = value
        End Set
    End Property

    Dim mRandomDate As Date

    <Output()> _
    Public ReadOnly Property RandomDate() As Date
        Get
            Return mRandomDate
        End Get
    End Property

    Dim mRandom As Random
    Dim mRandomRange As Random

    Protected Overrides Sub OnInitialize(ByVal initInfo _
        As GeneratorInit)

        mRandom = New Random(Me.Seed)       'deterministic
        mRandomRange = New Random(Me.Seed)  'deterministic

        'mRandom = New Random()              'non-deterministic
        'mRandomRange = New Random()         'non-deterministic

        MyBase.OnInitialize(initInfo)
    End Sub

    Protected Overrides Sub OnGenerateNextValues()
        Dim min As Date
        Dim max As Date

        ' Generate a random date from either range 1 or range 2.
        ' Randomly select either range 1 or range 2 by randomly 
        ' generating an odd or an even random number.
        '------------------------------------------------------------
        If mRandomRange.Next() Mod 2 = 0 Then  'check for odd or even
            min = mRange1Min
            max = mRange1Max
        Else
            min = mRange2Min
            max = mRange2Max
        End If

        ' The formula for creating a random number in a specific 
        ' range is: of range + (size of range * random number 
        ' between 0 and 1)

        ' Size of range
        Dim range As TimeSpan = max - min

        ' (Size of range * random number between 0 and 1)
        Dim randomNumber As TimeSpan = _
            New TimeSpan(CLng(range.Ticks * mRandom.NextDouble()))

        ' Start of range + (size of range * random number 
        ' between 0 and 1)
        mRandomDate = min + randomNumber
    End Sub
End Class

Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

See Also

Reference

Microsoft.Data.Schema.Tools.DataGenerator Namespace

GeneratorAttribute

GeneratorInit

IGenerator

Other Resources

Walkthrough: Creating a Custom Data Generator for a Check Constraint

Creating Custom Data Generators

An Overview of Data Generator Extensibility

Specifying Details of Data Generation for a Column