Compartilhar via


Classe Generator

A classe base abstrata para geradores de dados.

Namespace:  Microsoft.VisualStudio.TeamSystem.Data.DataGenerator
Assembly:  Microsoft.VisualStudio.TeamSystem.Data (em Microsoft.VisualStudio.TeamSystem.Data.dll)

Sintaxe

<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

Comentários

Se os geradores de dados padrão não forem suficientes, você pode criar geradores de dados personalizados.Para criar um gerador de dados personalizados, você deve criar uma classe que implementa IGenerator ou herda Generator. Identificar a classe sistema autônomo um gerador de dados decorando-lo com GeneratorAttribute.

Você pode criar um designer personalizado para um gerador de dados personalizados, ou você pode usar DefaultGeneratorDesigner.

A implementação da classe base constrói saídas que sejam baseiam em propriedades públicas são marcadas com o OutputAttribute. As informações são conjunto usando o InputAttribute. Usando as propriedades marcadas com atributos fornece um mecanismo simples para a declaração de entrada e valores de saída são fortemente tipadas.

Exemplos

Os geradores de dados padrão não é possível gerar dados para atender a algumas restrições de verificação.Por exemplo, um restrição CHECK requer que uma data é uma das duas faixas distintas, não é possível usar o gerador de DateTime padrão.Este exemplo cria um gerador de dados personalizados que pode satisfazer esse uma restrição.O gerador aceita dois intervalos distintos sistema autônomo entrada e gera uma data aleatória em um dos dois intervalos.Para obter mais informações, consulte Demonstra Passo a passo: Criando um gerador de dados Personalizar para um Restrição de Marcar.

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

Hierarquia de herança

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

Acesso thread-safe

Quaisquer membros static (Shared no Visual Basic) públicos deste tipo são thread-safe. Não há garantia de que qualquer membro de instância seja thread-safe.

Consulte também

Referência

Membros Generator

Namespace Microsoft.VisualStudio.TeamSystem.Data.DataGenerator

GeneratorAttribute

GeneratorInit

IGenerator

Outros recursos

Criando generators de dados Personalizar

Uma visão geral de extensibilidade do gerador de dados

Especificar detalhes de geração de dados para uma coluna