Exploring Randomize.Net

Randomize.Net provides an easy and lightweight extensions for System.Random for creating random instances of any given Type T, with generated random value. This can be highly useful for generating POCO’s to test your sample code, including LINQ queries against. The API has been designed to make it extremely simple and devoid of any complex syntax.

Generating Random Instances

For Example,

// Primitive Types
var _random = new Random();_
var randomString = _random.GenerateInstance<string>();
var randomInt32 = _random.GenerateInstance<Int32>();

// Output
// randomString = OfuQ42CoIe
// randomInt32 = 239016499

Randomize.Net works with User Defined Types as well, including nested User Defined Types.

public class SampleClass
public string StringProperty{get;set;}
public int Int32Property {get;set;}
public char CharProperty {get;set;}

public class AnotherSampleClass
public SampleClass SampleClassProperty{get;set;}
public string StringProperty{get;set;}

var sampleClass = _random.GenerateInstance<SampleClass>();
var anotherSampleClass = _random.GenerateInstance<AnotherSampleClass>();

/* Output
* sampleClass.StringProperty = ORCPiyNoy5
* sampleClass.Int32Property = -1461704929
* sampleClass.CharProperty = q
* anotherSampleClass.StringProperty = D5wfOfQhAK
* anotherSampleClass.SampleClassProperty.StringProperty = DC7ArGwDj5
* anotherSampleClass.SampleClassProperty.Int32Property = -1311605510
* anotherSampleClass.SampleClassProperty.CharProperty = y

Randomize.Net also supports generation of Collections.

// Method Signature
public static IEnumerable<T> GenerateCollection<T>(this Random source,int count = 1)

var sampleClassCollection = _random.GenerateCollection<SampleClass>(5);

// sampleClassCollection.Count = 5

Further examples on Randomize.Net would follow soon. If you are interested in looking at the code you can access it my Github.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s