Dave Paquette

Caffeine Driven Development

Go Beyond Lorem Ipsum with AngelaSmith

January 14, 2013

Lorem Ipsum is commonly used to fill a UI prototype with random data. This can be useful for validating a design quickly, while not getting bogged down in the details of populating a database or objects with data. Microsoft uses this approach with its Windows Phone and Windows 8 app templates. Today, I am introducing a package that can help you go beyond Lorem Ipsum.

AngelaSmith

AngelaSmith is package that can fill your .NET objects with random, but meaningful data. Need a Person? She can give you one. Need a 1000? She can do that too. Here’s how it works:

Let’s assume we have the following class:

public class Person
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Email { get; set; }
    public string PhoneNumber { get; set; }
    public int Age { get; set; }
}
public class Person
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Email { get; set; }
    public string PhoneNumber { get; set; }
    public int Age { get; set; }
}

To generate some random Person instances, do this:

Person person = Angie.Make<Person>(); 
List<Person> peeps = Angie.FastList<Person>(10); 
Person person = Angie.Make<Person>(); 
List<Person> peeps = Angie.FastList<Person>(10); 

 

Here is an example of what the populated Person objects look like:

image

 

How does she know? Well…Angie is pretty smart. She can inspect your object and make some intelligent guesses at what kind of data you would expect to see in those properties. Of course, Angie can’t figure it all out on her own. If you name your properties something different than she was expecting, she might not recognize it. For example if you had a property called FirNam which is intended to contain a person’s FirstName, Angie will not recognize that property as being a FirstName. That’s why we introduced a powerful fluent API to help configure how Angie generates objects for a specific type.

Person person = Angie.Configure<Person>()
                     .Fill(p => p.FirNam).AsFirstName()
                     .Make(); 
List<Person> peeps = Angie.FastList<Person>(10); 
Person person = Angie.Configure<Person>()
                     .Fill(p => p.FirNam).AsFirstName()
                     .Make(); 
List<Person> peeps = Angie.FastList<Person>(10); 

 

Once you have configured Angie once, you can continue to ask for objects of that type without reconfiguring it (Angie has a good memory too).

The fluent API is pretty straight forward. Use the Fill method to select a particular property, then use one of the many built in methods to specify how you would like that property to be filled. You can even chain them together to keep specifying how to fill multiple properties:

List<Person> oldPeople = Angie.Configure<Person>()
                              .Fill(p => p.FirName).AsFirstName()
                              .Fill(p => p.PreferredEmail).AsEmailAddress()
                              .Fill(p => p.Age).WithinRange(55,98)
                              .MakeList(100); 
List<Person> oldPeople = Angie.Configure<Person>()
                              .Fill(p => p.FirName).AsFirstName()
                              .Fill(p => p.PreferredEmail).AsEmailAddress()
                              .Fill(p => p.Age).WithinRange(55,98)
                              .MakeList(100); 

 

So far, we have seen how AngelaSmith can initialize text, numeric, and date properties, but what about properties that reference other objects? By default, AngelaSmith will leave those properties null. Luckily, with the fluent API, you can tell AngelaSmith how to fill those properties too.

 

Let’s say we class BlogPost with a property called Author of type Person. Here’s what you can do:

List<Person> authors = Angie.MakeList<Person>(10); 
List<BlogPost> posts = Angie.Configure<BlogPost>()
                  .Fill(e => e.Author).WithRandom(authors)
                  .MakeList(100); 
List<Person> authors = Angie.MakeList<Person>(10); 
List<BlogPost> posts = Angie.Configure<BlogPost>()
                  .Fill(e => e.Author).WithRandom(authors)
                  .MakeList(100); 

 

Awesome! Now we have 100 blog posts, each randomly assigned author from our list of 10 authors. As Staples would say: “That was easy!”.

More Info

AngelaSmith is a project that was started by James Chambers, and I was recently asked to join the project. You can find AngelaSmith on GitHub and of course on Nuget.

Install-Package AngelaSmith

Next time you need some random data (or next time you see Lorem Ipsum), give AngleSmith a try. Let us know what you think.

6 thoughts on “Go Beyond Lorem Ipsum with AngelaSmith

  1. Agnieszka says:

    If time is money you’ve made me a weltaheir woman.

  2. Pingback: Using Realistic Data in Unit Testing | Simon Online

  3. Pingback: Dave Paquette | Extending AngelaSmith with Custom Property Fillers

  4. Goptor says:

    I wonder if the real Angela Smith would enjoy being filled with random objects.

  5. Pingback: Dave Paquette | Extending AngelaSmith’s Fluent API

  6. Pingback: Technology Post Roundup–7th Edition « Jonathan Rozenblit

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code lang=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" extra="">

Current day month ye@r *