Project Description
Hydrator is a highly configurable test data generator

Now available on NuGet!

https://www.nuget.org/packages/Hydrator/

Features

  • extensibility (write your own TypeConventions or DataTypeFormatters)
  • flexibility (configure existing TypeConventions or DataConventions or override them with your own)
  • support for common types (American FirstName, LastName, States, City)

Usage

    public class Person : Entity
{
[DataType(DataType.PhoneNumber)]
public string Phone { get; set; }

public string FirstName { get; set; }

public string LastName { get; set; }

public int Weight { get; set; }

public int Height { get; set; }

public Address PhysicalAddress { get; set; }

public DateTime? DateOfBirth { get; set; }
}

Let this be our example class. Now all we have to do (if we want Hydrator to use default settings) is to initialize Hydrator and hydrate Person.

var hydrator = new Hydrator(); 
var person = hydrator.Hydrate<Person>();

Output:

{My.Hydrator.Tests.Person}
DateOfBirth: {1963-01-01 00:00:00}
FirstName: "FKATRWYEEUPCYGSXBAIISWMSAYFOJCYNTYDIVABBVCAMBLTIVSDHKTKHXHSSQKTBVCHACJCBYHMPWYQOEWRQXRCAOVKOHHJKHXPY"
Height: 893716067
LastName: "DTFLQCHDGTJXPEOSRDCEYHLGYMTQWCIXJRKJRGAVJYSTMMYCSQSEVLYVIYLDHQGELKOWSEBLDGUANQTMMQWFJECWROBSTVNRSOIB"
Phone: "609-199-280"
PhysicalAddress: {My.Hydrator.Tests.Address}
Weight: 446946766

This may not be our desired effect, but with a little configuration our effects may be a way better.

            hydrator.Configure(x =>
{
x.For<Person>().Property(y => y.FirstName).Is(CommonType.AmericanName);
x.For<Person>().Property(y => y.LastName).Is(CommonType.AmericanLastName);
x.For<Person>().Property(y => y.Weight).Use(new IntTypeConvention(99, 150));
x.For<Person>().Property(y => y.Height).Use(new IntTypeConvention(100, 999));
});

Output:

{My.Hydrator.Tests.Person}
DateOfBirth: {1949-09-17 00:00:00}
FirstName: "HANNAH"
Height: 761
LastName: "BAKER"
Phone: "404-884-174"
PhysicalAddress: {My.Hydrator.Tests.Address}
Weight: 136

It is that simple!

TypeConventions

First step in hydrating process is to find a convention that matches a property type.

Built-in conventions:

  • Boolean
  • DateTime
  • Decimal
  • Double
  • Float
  • Guid
  • Int
  • String
  • List and Dictionary

All conventions are implementing ITypeConvention interface (and you should too if you want to write your own custom conventions)

    public interface ITypeConvention
{
Type Type { get; }

object GenerateValue(object[] customAttributes);
}

So to write your conventions all you need to do is to implement this interaface...

    public class AddressConvention : BaseTypeConvention, ITypeConvention
{
public Type Type
{
get { return typeof(Address); }
}

public object GenerateValue(object[] customAttributes)
{
return new Address()
{
City = "City",
State = -1,
StreetName = "StreetName",
StreetNumber = -1
};
}
}

...and use it in configuration

x.For<Person>().Property(y => y.PhysicalAddress).Use(new AddressConvention());

If you want to use this convention not only in Person you can setup global convention

x.For<Address>().Use(new AddressConvention());

IMPORTANT: In hydrator hierarchy is important, so if you override Address.City property with your custom convention and then use for Address another convention the Hydrator will use Address convention only, and discard the Address.City one.

DataTypeFormatters

DataTypeAttributes are passed into TypeConvention and can be used to find specific formatter for passed DataType

There are few built-in DataTypeFormatters:

  • Date and DateTime
  • Email
  • Multiline
  • Numeric
  • Phone
  • Text
  • ZipCode

All of them implement IDataTypeFormatter interface

    public interface IDataTypeFormatter
{
DataTypeAttribute FormatterDataType { get; }

object GetFormat();
}

So again, if you want to we your custom data type convention, just implement it...

    public class CustomFormatter : BaseDataTypeFormatter, IDataTypeFormatter
{
public DataTypeAttribute FormatterDataType
{
get { return new DataTypeAttribute("Custom"); }
}

public object GetFormat()
{
return "CustomFormatter";
}
}

...and configure hydrator

x.For("Custom").Use(new CustomFormatter());

For more information send me a message or refer to Tests project in Source. Have fun using it!

Last edited Nov 13, 2013 at 4:50 PM by ppekrol, version 18