This project is read-only.

Project Description
Randomize is a small simply configurable library to generate instances of any class with random property values that could be used in testing or database feeding.

Features

  • Custom Generators and Descriptors support
  • Fluent style description of custom classes generation configuration
  • Built-in support for standart .NET types: int, double, string, DateTime
  • IEnumerable<T> properties support

Code samples

The following code generates an integer number:

var randomizer = new Randomizer();
int generatedNumber = randomizer.Generate<int>(); // 123451

To generate an instance of custom type we need to write these lines:

Here is our class:

public class Employee
{
    public string Name { get; set; }

    public double Salary { get; set; }

    public DateTime BirthDate { get; set; }
}

Next thing is to create a descriptor to specify what properties need to be generated and configuration of desired values like this:

public EmployeeDescriptor()
{
    this.Property(p => p.Name)
        .WithPool(new[] {"John", "Andy", "Carl"});

    this.Property(p => p.Salary,
                    (person, salary) => person.Salary = salary*1000) //override property setter
        .WithMin(1)
        .WithMax(3)
        .WithFractional(1);

    this.Property(p => p.BirthDate)
        .WithMin(new DateTime(1940, 1, 1))
        .WithMax(DateTime.Now.AddYears(-18))//we're not hiring children:)
        .IgnoreTime();

    this.CreateUsing(() => new Employee()); //set up constructing function to avoid Activator.CreateInstance() usage;
}
And finally, let's register and use this descriptor:
 
var randomizer = new Randomizer();
randomizer.Register(new EmployeeDescriptor());

IEnumerable<Employee> employees = randomizer.Generate<Employee>(10);
foreach(var employee in employees)
{
    Console.WriteLine("Name: {0}, Salary: {1}, Birth date: {2}", 
employee.Name, employee.Salary, employee.BirthDate.ToShortDateString()); } //Name: Carl, Salary: 2000, Birth date: 11/6/1954 //Name: Carl, Salary: 2200, Birth date: 9/13/1977 //Name: John, Salary: 1000, Birth date: 8/8/1963 //Name: Andy, Salary: 1600, Birth date: 9/29/1944 //Name: Carl, Salary: 2700, Birth date: 10/5/1942 //Name: Carl, Salary: 2300, Birth date: 2/5/1945 //Name: Andy, Salary: 1300, Birth date: 7/16/1988 //Name: Carl, Salary: 1300, Birth date: 8/17/1973 //Name: Andy, Salary: 1100, Birth date: 2/3/1984 //Name: Andy, Salary: 1100, Birth date: 10/13/1941

Last edited Feb 27, 2012 at 7:30 AM by killedthedream, version 13