Classe Generator

Classe base astratta per i generatori di dati.

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

Sintassi

'Dichiarazione
<CLSCompliantAttribute(True)> _
Public MustInherit Class Generator
    Implements IGenerator, IDisposable
'Utilizzo
Dim instance As Generator
[CLSCompliantAttribute(true)] 
public abstract class Generator : IGenerator, IDisposable
[CLSCompliantAttribute(true)] 
public ref class Generator abstract : IGenerator, IDisposable
/** @attribute CLSCompliantAttribute(true) */ 
public abstract class Generator implements IGenerator, IDisposable
CLSCompliantAttribute(true) 
public abstract class Generator implements IGenerator, IDisposable

Note

Se i generatori di dati standard non sono sufficienti, è possibile creare generatori di dati personalizzati. Per creare un generatore di dati personalizzato, è necessario creare una classe che implementa l'interfaccia IGenerator o eredita dalla classe Generator. Per identificare la classe come generatore di dati, è necessario decorarla con la classe GeneratorAttribute.

È possibile creare una finestra di progettazione personalizzata per un generatore di dati personalizzato oppure utilizzare la classe DefaultGeneratorDesigner.

L'implementazione della classe base costruisce output basati su proprietà pubbliche contrassegnate con la classe OutputAttribute. Gli input vengono impostati utilizzando la classe InputAttribute. L'utilizzo di proprietà contrassegnate con attributi offre un semplice meccanismo per dichiarare valori di input e output fortemente tipizzati.

Esempio

I generatori di dati standard non possono generare dati per soddisfare alcuni vincoli CHECK. Se, ad esempio, un vincolo CHECK richiede che una data appartenga a uno di due intervalli distinti, non è possibile utilizzare il generatore DateTime standard. Nell'esempio seguente viene creato un generatore di dati personalizzato che consente di soddisfare tale vincolo. Il generatore accetta due intervalli distinti come input e genera una data casuale appartenente a uno dei due intervalli. Per ulteriori informazioni, vedere Procedura dettagliata: creazione di un generatore dati personalizzato per un vincolo 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

Gerarchia di ereditarietà

System.Object
  Microsoft.VisualStudio.TeamSystem.Data.DataGenerator.Generator

Codice thread safe

I membri statici pubblici (Shared in Visual Basic) di questo tipo sono validi per le operazioni multithreading. I membri di istanza non sono garantiti come thread safe.

Vedere anche

Riferimenti

Membri Generator
Spazio dei nomi Microsoft.VisualStudio.TeamSystem.Data.DataGenerator
GeneratorAttribute
GeneratorInit
IGenerator

Concetti

Cenni preliminari sulla extensibility dei generatori di dati
Specifica dei dettagli di generazione dati per una colonna

Altre risorse

Creazione di generatori di dati personalizzati