Generator (Clase)
Actualización: noviembre 2007
Clase base abstracta para los generadores de datos.
Espacio de nombres: Microsoft.VisualStudio.TeamSystem.Data.DataGenerator
Ensamblado: Microsoft.VisualStudio.TeamSystem.Data (en Microsoft.VisualStudio.TeamSystem.Data.dll)
Sintaxis
<GeneratorAttribute(GetType(DefaultGeneratorDesigner))> _
<CLSCompliantAttribute(True)> _
Public MustInherit Class Generator _
Implements IGenerator, IDisposable
Dim instance As Generator
[GeneratorAttribute(typeof(DefaultGeneratorDesigner))]
[CLSCompliantAttribute(true)]
public abstract class Generator : IGenerator,
IDisposable
[GeneratorAttribute(typeof(DefaultGeneratorDesigner))]
[CLSCompliantAttribute(true)]
public ref class Generator abstract : IGenerator,
IDisposable
public abstract class Generator implements IGenerator, IDisposable
Comentarios
Si los generadores de datos estándar son insuficientes, se pueden crear generadores de datos personalizados. Para crear un generador de datos personalizados, debe crear una clase que implemente IGenerator o herede de Generator. Identifica la clase como un generador de datos decorándolo con GeneratorAttribute.
Puede crear un diseñador personalizado para un generador de datos personalizados o puede utilizar DefaultGeneratorDesigner.
La implementación de la clase base crea resultados que se basan en propiedades públicas marcadas con OutputAttribute. Las entradas se establecen mediante InputAttribute. El uso de propiedades que se marcan con atributos proporciona un mecanismo simple para declarar valores de entrada y salida con establecimiento inflexible de tipos.
Ejemplos
Los generadores de datos estándar no pueden generar datos para satisfacer algunas de las restricciones CHECK. Por ejemplo, una restricción CHECK requiere que una fecha pertenezca a uno de dos intervalos distintos y, por tanto, no puede utilizar el generador DateTime estándar. Este ejemplo crea un generador de datos personalizado que puede satisfacer una restricción de este tipo. El generador acepta dos intervalos distintos como entrada y genera una fecha aleatoria perteneciente a uno de los dos intervalos. Para obtener más información, vea Tutorial: Crear un generador de datos personalizado para una restricción CHECK.
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
Jerarquía de herencia
System.Object
Microsoft.VisualStudio.TeamSystem.Data.DataGenerator.Generator
Microsoft.VisualStudio.TeamSystem.Data.Generators.Bit
Microsoft.VisualStudio.TeamSystem.Data.Generators.DatabaseDateTime
Microsoft.VisualStudio.TeamSystem.Data.Generators.DatabaseGuid
Microsoft.VisualStudio.TeamSystem.Data.Generators.DataBoundGenerator
Microsoft.VisualStudio.TeamSystem.Data.Generators.Image
Microsoft.VisualStudio.TeamSystem.Data.Generators.Numeric<T>
Microsoft.VisualStudio.TeamSystem.Data.Generators.RegexString
Microsoft.VisualStudio.TeamSystem.Data.Generators.VarBinary
Seguridad para subprocesos
Todos los miembros static (Shared en Visual Basic) públicos de este tipo son seguros para la ejecución de subprocesos. No se garantiza que los miembros de instancias sean seguros para la ejecución de subprocesos.
Vea también
Referencia
Microsoft.VisualStudio.TeamSystem.Data.DataGenerator (Espacio de nombres)
Otros recursos
Crear generadores de datos personalizados
Descripción general de la extensibilidad del Generador de datos
Especificar los detalles de la generación de datos para una columna