Generator Class
This class is the abstract base class for data generators.
Namespace: Microsoft.Data.Schema.DataGenerator
Assembly: Microsoft.Data.Schema (in Microsoft.Data.Schema.dll)
Syntax
'Declaration
<CLSCompliantAttribute(True)> _
<GeneratorAttribute(GetType(DefaultGeneratorDesigner))> _
Public MustInherit Class Generator _
Implements IGenerator, IDisposable, IExtensionInformation, IExtension
'Usage
Dim instance As Generator
[CLSCompliantAttribute(true)]
[GeneratorAttribute(typeof(DefaultGeneratorDesigner))]
public abstract class Generator : IGenerator,
IDisposable, IExtensionInformation, IExtension
[CLSCompliantAttribute(true)]
[GeneratorAttribute(typeof(DefaultGeneratorDesigner))]
public ref class Generator abstract : IGenerator,
IDisposable, IExtensionInformation, IExtension
public abstract class Generator implements IGenerator, IDisposable, IExtensionInformation, IExtension
[<AbstractClassAttribute>]
[<CLSCompliantAttribute(true)>]
[<GeneratorAttribute(typeof(DefaultGeneratorDesigner))>]
type Generator =
class
interface IGenerator
interface IDisposable
interface IExtensionInformation
interface IExtension
end
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. Using 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 requires that a date is in one of two distinct ranges. You 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)
{
mRandom = new Random(this.Seed); //deterministic
mRandomRange = new Random(this.Seed); //deterministic
//mRandom = new Random(); //non-deterministic
//mRandomRange = new Random(); //non-deterministic
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:
//start 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 class
}//end 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:
'start 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
Inheritance Hierarchy
System.Object
Microsoft.Data.Schema.DataGenerator.Generator
Microsoft.Data.Schema.Generators.Sql.Bit
Microsoft.Data.Schema.Generators.Sql.DatabaseGuid
Microsoft.Data.Schema.Generators.Sql.DataBoundGenerator
Microsoft.Data.Schema.Generators.Sql.DateTime2Generator
Microsoft.Data.Schema.Generators.Sql.DateTimeOffsetGenerator
Microsoft.Data.Schema.Generators.Sql.Image
Microsoft.Data.Schema.Generators.Sql.Numeric<T>
Microsoft.Data.Schema.Generators.Sql.RegexString
Microsoft.Data.Schema.Generators.Sql.SqlDateTimeGenerator
Microsoft.Data.Schema.Generators.Sql.TimeSpanGenerator
Microsoft.Data.Schema.Generators.Sql.VarBinary
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.DataGenerator Namespace
Other Resources
Creating Custom Data Generators