Building a Custom Data Generator


While the default data generators are very useful, especially the regular expression one, there may be times when those data generators just don't do quite what you want them to. At that point, you will need to create a custom data generator. In this section, you walk step-by-step through creating a custom data generator for credit card numbers. This data generator will create Visa (number begins with a four) or MasterCard (number begins with a five) numbers, and will even add dashes between the numbers as an optional setting.

  1. To get started, create a new C# class library project in Visual Studio and name it CreditCardNumberdg.

  2. Rename the Class1 file to be CCNum.

  3. Next, add a reference to the Microsoft.VisualStudio.TeamSystem.Data.dll. This DLL is located at c:\Program Files\Microsoft Visual studio 8\DBPro. Once you have added that reference, add the following using statement to the top of your CCNum.cs file:

     using Microsoft.VisualStudio.TeamSystem.Data.DataGenerator; 

  4. Add the Generator() attribute to the top of the CCNum class declaration:

     [Generator()] Public class CCNum { ... } 

  5. Your class also needs to inherit from the Generator abstract class, so make the following change:

     [Generator()] Public class CCNum : Generator { ... } 

  6. Right-click the Generator word, and select Implement Abstract Class, to stub out all the methods you must implement for this class. The only required method it creates is the GenerateNextValues() method.

  7. Next, you want to create the input properties for your custom data generator. These properties can be set from the Properties window of Visual Studio. To create an input property, you create a public property and add the Input() attribute. Take the following code:

                 public enum CreditCardTypes { V, M, A } private string _ccNumber; private CreditCardTypes _creditCardType = CreditCardTypes.A; private bool _includeDashes; [Input()] public CreditCardTypes CreditCardType { set { _creditCardType = (CreditCardTypes)value; } } [Input()] public bool IncludeDashes { set { _includeDashes = value; } } 

    First, you declared an enumerated type called CreditCardTypes. This type contains three values: V for Visa, M for MasterCard, or A for all numbers. Next you declared three variables. _ccNumber is our final credit card number. _creditCardType is the type of credit card to create. _includeDashes is a Boolean value that determines whether to include dashes in the credit card number.

    Now you get to the two input properties. The first one, CreditCardTypes, creates a dropdown list of values for the user to choose from for the type of credit card to create. The second, IncludeDashes, allows the user to set this to true or false.

  8. You have your input properties, and now need an output property. You declare a private variable to hold the credit card number, but you need a public property, decorated with the Output() attribute, to access this value.

               [Output()] public string ccNumber { get { return _ccNumber; } } 

    This creates a public property, ccNumber, which contains the credit card number.

  9. The final coding step is to implement the GenerateNextValues method. This method is required, and is responsible for creating the actual test data. Here is the method for the example:

     public override void GenerateNextValues() { Random randomNumber = new Random(); int tempNumber; int i; _ccNumber = ""; if (_creditCardType == CreditCardTypes.M) _ccNumber = "5"; else if (_creditCardType == CreditCardTypes.V) _ccNumber = "4"; else if (_creditCardType == CreditCardTypes.A) { tempNumber = randomNumber.Next(10); _ccNumber = tempNumber.ToString(); } for (i = 2; i <= 16; i++) { ///Thread.Sleep(1); tempNumber = randomNumber.Next(10); _ccNumber += tempNumber.ToString(); if ((_includeDashes) && ((i % 4) == 0) && (i != 16)) _ccNumber += "-"; } } 

    This method uses a random number generator to create the credit card numbers. First, it initializes the random number generator. Next, it checks to see which type of credit card was selected, and sets the first digit in the credit card number to the appropriate value. It then proceeds to loop fifteen times, creating the credit card number. It also checks to see if dashes are to be included in the number, and adds them as appropriate.

You have now created a custom data generator. However, you are not done yet. Next, you must sign your custom data generator with a strong name, before you register it.

  1. To do this, right-click the project in Solution Explorer, and click Properties. On the properties window, check the Sign the assembly check box.

  2. Select New File from the drop down-list box, and enter ccnum.txt as the key file name. Uncheck the Protect my key file with a password check box. Now save your changes to your project properties.

    Before you go any farther, save all your files and make sure your class library will build. If it does not, fix any errors, and then continue.

    Once you have created your generator, and signed it, you have to register it with Visual Studio to be able to use it:

  3. Open the class library project you were just working on in Visual Studio. Select ViewOther WindowsCommand Window to open the command window in Visual Studio. Enter the following into the command window:

     ? System.Reflection.Assembly.LoadFrom([FilePath]).FullName 

    In this line, [FilePath] is the path to the compiled DLL file. For your example, this should look similar to this:

     ? System.Reflection.Assembly.LoadFrom("C:\\Documents and Settings\\TFSSETUP\\My Documents\\Visual Studio 2005\\Projects\\CreditCardNumberdg\\CreditCardNumberdg\\ bin\\Debug\\CreditCardNumberdg.dll").FullName 

    Running this statement results in something similar to this:

     "CreditCardNumberdg, Version=1.0.0.0, Culture=neutral, PublicKeyToken=4826364792c75d48" 

  4. Save this information to a text file, as you will need it shortly. You need to add this information to the Microsoft.VisualStudio.TeamSystem.Data.Extensions.xml file. This file contains a <type> entry for each data generator used by Visual Studio. The format of the tag is:

     <type>FullyQualifiedClassName,AssemblyName,Version,Culture,PublicKeyToken</type> 

    For your example, the type tag would look like this:

     <type>CreditCardNumberdg.CCNum, CreditCardNumberdg, Version=1.0.0.0, Culture=neutral, PublicKeyToken=4826364792c75d48</type> 

  5. Finally, copy the DLL to the C:\Program Files\Microsoft Visual Studio 8\Common7\IDE\PrivateAssemblies directory. Restart Visual Studio, and you are ready to use your custom data generator.

To test this, open the MyNorthWindDB database project, and open the data generation plan you created earlier in this chapter. Select the CreditCardNumber table. Notice how, for the credit_card_number column, you now have the option, under the Generator heading, for the CCNum generator you created earlier, as shown in Figure 8-20.

image from book
Figure 8-20

The two input options appear in the properties window. Set the CreditCardType to be V for Visa, and set the IncludeDashes property to true. If you preview the data, you will see that all the numbers begin with a 4, indicating it is a Visa, and that the numbers are separated with dashes, as shown in Figure 8-21.

image from book
Figure 8-21

Important

Notice how some of the numbers repeat. This is due to the random number generator, and how quickly the numbers are being created. You could add a pause in between each number to make each credit card number unique.

There you have it; you have created your first custom data generator. Another possible data generator you might consider creating is a name generator, which reads names from a database or a file, and adds them to the tables. The possibilities are endless.



Professional Team Foundation Server
Professional Team Foundation Server
ISBN: 0471919306
EAN: 2147483647
Year: 2004
Pages: 168

Similar book on Amazon
Professional Team Foundation Server 2010 (Wrox Programmer to Programmer)
Professional Team Foundation Server 2010 (Wrox Programmer to Programmer)
Professional Application Lifecycle Management with Visual Studio 2010 (Wrox Programmer to Programmer)
Professional Application Lifecycle Management with Visual Studio 2010 (Wrox Programmer to Programmer)
Professional Scrum with Team Foundation Server 2010 (Wrox Programmer to Programmer)
Professional Scrum with Team Foundation Server 2010 (Wrox Programmer to Programmer)
Team Foundation Server 2008 in Action
Team Foundation Server 2008 in Action

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net