Alex Rivera | Logout

Are there any tools to populate class properties with random data?

Asked 2009-09-11T21:20:39.043
12

What I'd like to do is create a class with some attributes on different properties, pass that class to another that will set the properties with appropriate random data... here in pseudo code:

public class Customer
{
   [Attribute("FirstName")]
   private string CustomerFirstName;
   public {get;set} //etc

   [Attribute("LastName")]
   private string CustomerLastName;
   public {get;set;} //etc

   [Attribute("DateTime")]
   private DateTime CustomerSignUpDate;
   public DateTime {get;set;} //yadda

   [Attribute("Phone")]
   private string CustomerPhone;
   public string {get;set;} //yadda
}

And then do like this

IList<Customer> CustomerList=ClassFillerOutClass(new Customer(),5);

And the result would be a List of 5 Customers that have appropriate 'random' data in their properties.

If this doesn't exist...I guess I could start a project myself to do...I just don't want to reinvent the wheel if it's not necessary.

EDIT: I forgot a piece. I'm looking to use this as a test tool. So in the example above I could quickly create a list of 5 customers with random but appropriate values. And then say pass that to my persistence method, and have something I can check against. I'm trying to avoid manually creating a populated object everytime for my TDD purposes.

EDIT 2: Ok so I started rolling my own...I'll post it on Codeplex this weekend and link it here...I clearly won't be done but it'll be a start if anyone else wants to work on it.

Edit
Report

2 Answers

9

Ok...so I never found one....so I decided to start my own. Check out: Object Hydrator.

The project is presently a quick spike...but I think it has potential...let me know if you have ideas how to make it better.

answered 2009-09-12T04:07:47.500
0

I don't know your goals here, but I'll keep this close to the code level. This approach may not work for you, but it has in the past for me.

You can generate random data and place it into your database. There are a few commercial at-cost products that do this. The one I use is SQL Data Generator by RedGate.

With that data in hand, you can do some text manipulations in SQL to convert columnar data:

Table Customer
FirstName | LastName | SignUpDate | Phone
Bob         Smith      1/2/2009     555-555-1212
Jane        Doe        9/11/2009    555-300-1334
...

Into:

new Customer () 
{
  CustomerFirstName = "Bob", 
  CustomerLastName = "Smith", 
  CustomerSignUpDate = DateTime.Parse("1/2/2009"), 
  Phone = "555-555-1212"
},
new Customer () 
{
  CustomerFirstName = "Jane", 
  CustomerLastName = "Doe", 
  CustomerSignUpDate = DateTime.Parse("9/11/2009"), 
  Phone = "555-300-1334
},

and wrap this into the following using a text editor (this is C# 3.0 syntax):

public class FakeCustomerRepository
{
  private IList<Customer> m_Customers = new List<Customer>()
               {
                  [insert SQL text transform here]
               };

  public Customer Get(...)
  {
    return m_Customers.Find(...);
  }
}

Again, I don't know your needs and whether you need random data at runtime. The approach described above is only helpful if you want to generate a lot of data quickly and statically.

answered 2009-09-11T21:36:56.253

Your Answer