Introducing AngelaSmith - The Object Initializing Smartypants

Our tooling in the .Net space is getting better and better, as well as the ecosystem that we work in. Take for instance Windows Azure Mobile Services – when we are dealing with greenfield development having your backend services running in minutes is a Godsend and having it work over multiple devices is chocolate on the sundae.  Your database is in place in minutes and you can easily access your data via REST.

What is something that all data-based applications require?  Data!  But missing from the data equation in green field development are two key parts to new app development: sample data and design time data.  If you understand those parts already and want to be able to fire up 250 persons like this:

     var people = Angie
.Configure()
.ListCount(250)
.MakeList<Person>();

…then skip below to the juicy bits.  For the rest of us, a quick primer.

Why It’s Important

For those of us building Windows Store or Windows 8 Phone apps, we have some incredible tools that make designing good looks apps super easy.

First is the MvvmLight Toolkit from Laurent Bugnion that allows you to quickly (and selectively) build out bindable view models, complete with properties, commands and, of course, your data.  If you haven’t worked with the data-binding approach, this is something you should be looking at if you use data at all in your apps.  If you’re not working with data at all in your apps, this is something that you should be looking at if you want users at all in your apps.

Second is the ability to use design time data through tools like Blend or Visual Studio 2012. Design time data sources usually provide static methods that can supply your IDE or editor of choice sample data while you’re designing your view.  It’s tough to build out a good looking UI without having actual data in there to see what things will look like.

When you put these two things together you get this great experience of seeing your app come together without having to build, deploy and run your app.

In other environments (Android, XCode) there may be similar facilities for design, but on all platforms, there’s the actual execution of the app on the device, and you probably want to get some data up there.  How do you test user-level security with only one user?  How do you build a geolocation app presenting points of interest if you don’t have any POI data?  View a contact list with no contacts?  What about just seeing how you app performs under load?  Build screen shots for your app store shots? 

You get the idea. You need data in there.

So You Want to Do the Data Binding Awesomeness

Great, so we agree that some measure of data is needed at some point of the app build process. You need a list of, say, 25 people, but how do you get those in there?  This is how we used to do it:

        List<Person> people = new List<Person>();
people.Add(new Person { FirstName = “Angela”, LastName = “Smith”, Age = 24, PhoneNumber = “867-5309” });
// …do this 23 times…
people.Add(new Person { FirstName = “Susan”, LastName = “Peters”, Age = 22, PhoneNumber = “555-1234” });

But that sucks. Big time.  Because you have to new up every object, and to make it look somewhat interesting, you have to also touch each row (not copy and paste). So you have to sift through 25 lines of code and try to come up with some believable objects.

A Way With Way More Awesome

How about this instead:

        List<Person> people = Angie.FastList<Person>();

Oh yeah. That's how AngelaSmith rolls. One line of code to get a list of 25 people (25 is the default list size).  All the properties are filled in for you as Angie recognizes many common property names. You can use Angie to build out a single object:
        var person = Angie.FastMake<Person>();
Angie’s not alone; she has a friend who can also help fill in a single property:
        string firstName = Susan.FillFirstName();

You can tell her how to build properties:

    var blogTitle = “Angie”;

var post = Angie.Configure()
    .FillBy(<span class="str">"title"</span>, <span class="kwrd">delegate</span>() { <span class="kwrd">return</span> blogTitle; })
    .Make&lt;BlogPost&gt;();</pre>

Or, she can help you fill in properties (because she’s so smart):

     var comments = Angie
.Configure()
.ListCount(1000)
.FillBy(“CommentDate”, delegate() { return Angie.MakeDate(DateRules.PastDates); })
.MakeList<BlogComment>();

And, you can chain it all together to do some cool things.  Here, we’re building a blog post with 5 comments, all the dates are set to a date prior today, and the comments are created using an anonymous function:

  var blogpost = Angie
.Configure()
.FillBy(“CreateDate”, delegate() { return Susan.FillDate(DateRules.PastDate); })
.FillBy(“Comments”, delegate()
{
return Angie
.Set()
.ListCount(5)
.FillBy(“CommentDate”, delegate() { return Susan.FillDate(DateRules.PastDate); })
.MakeList<BlogComment>();
})
.Make<BlogPost>();

At the tail of that, we’re calling “Make” to get a single instance of BlogPost, but we could just as easily call “MakeList” to get a list of entities.

So Get Going!

To install AngelaSmith, use the Package Manager Console:

    install-package AngelaSmith

For more information, check out the AngelaSmith project on GitHub. Of course, you’re welcome to fork and contribute!

Stay tuned for great examples on how to seed data in Windows Azure Mobile Services, Entity Framework and more.