Learning in the Open is Ryan’s attempt to share the things that: trip him up, intrique him, and really cheese him off. By reading this blog you are legally bound to be kind in sharing your thoughts in the comments…hey it’s in writing sooooo….

Getting started with ObjectHydrator

ObjectHydrator is a utility to fill POCO (Plain Old Class Objects) and other classes with realistic looking data. The intended usage is to help with quick prototyping for UIs and APIs. Basically, so you can work on properly modeling your data and API before having to create real data structures and data. We’ll get to that in further examples, today is just a simple introduction to become familiar with it’s idioms.

The requirements for this sample are: Visual Studio (any edition should be fine), .Net Core 2.2 and a connection to the internet (to access Nuget.org).

We’ll start by making a .Net Core Console Application.

In Visual Studio: File -> New and select .Net Core on the left and .Net Core Console Application in the middle:

Creating the project

Creating the project

Next we’ll make a simple Customer class with a first name, last name, phone number, and age.

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

Next we need to add ObjectHydrator to the project. The NuGet package can be found here: https://www.nuget.org/packages/FoundationStandard.ObjectHydrator/

Installing is pretty straight-forward (most of the time). This can easily be done via the Package Manager Command Line like this:

Example taken from NuGet.org

Example taken from NuGet.org

Now that it’s installed we’ll consume it by creating a Hydrator that is based on our customer class, generate a list of 5 Customers and loop through them in the console output. Let’s jump over to the Program.cs in our project and get to work.

Replace your Main with this:

static void Main(string[] args)
        {
            var customers = new Hydrator<Customer>().GetList(5);
            foreach (var customer in customers)
            {
                Console.WriteLine("{0} {1} is {2} years old and can be phoned at {3}",
                    customer.FirstName,
                    customer.LastName,
                    customer.Age,
                    customer.PhoneNumber);
            }
            Console.ReadLine();
        }

That’s it! Let’s check our output.

Our first batch of output!

Our first batch of output!

Ok that’s awesome but what just happened?

ObjectHydrator has a set of conventions for the more popular property names and types. In this case it inferred FirstName is exactly that and the same goes for LastName and PhoneNumber. For Age, because it is an Integer it used the standard IntegerGenerator which has defaults of 0-100.

Let’s say we want to change the generation profile to have our Max age be 25. We can do this by overriding the default generator for that at the time we instantiate the Hydrator. Let’s jump back into Program.cs and replace our first line with this:

var customers = new Hydrator<Customer>().WithInteger(x=>x.Age,0,25).GetList(5);

Now on the Age property we should not see any value over 25 or less than 0. Let’s take a look at the result:

Sweet…

Sweet…

Ok let’s make on last change by changing the phone number from a US style, to a UK style. Let’s change our Hydrator line again to match this (line breaks added for readability):

var customers = new Hydrator<Customer>()
                .WithInteger(x=>x.Age,0,25)
                .WithUnitedKingdomLandline(x=>x.PhoneNumber)
                .GetList(5);

Now let’s check the output again:

London calling????

London calling????

Ok so there we have it…a getting started blog post on ObjectHydrator. Sound off in the comments and share your thoughts or questions. A GitHub repo can be found here: https://github.com/PrintsCharming/ObjectHydratorGettingStarted

Thanks so much for stopping by.

Cheers!

Building a custom generator with ObjectHydrator

Let's try this again...