Generator Class

Visual Studio Team Data Managed Ref
Generator Class

The abstract base class for data generators.

Namespace: Microsoft.VisualStudio.TeamSystem.Data.DataGenerator
Assembly: Microsoft.VisualStudio.TeamSystem.Data (in microsoft.visualstudio.teamsystem.data.dll)

C#
[CLSCompliantAttribute(true)]
public abstract class Generator : IGenerator, IDisposable

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.

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

posted on 2008-12-17 13:53  DecleorMX  阅读(664)  评论(0编辑  收藏  举报

导航