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….

Building a custom generator with ObjectHydrator

While ObjectHydrator comes with a large number of generators out of the box, you might find yourself needing a custom generator to suit your needs.

Building on the previous tutorial, let’s pretend that we need to add a location to our Customer object. Now this location will be comprised of a Latitude and Longitude. We’re going to add a class to our project that will hold these two values, then add a property to the Customer object to hold it. Let’s go ahead and define that Location class.

public class Location
    {
        public double Lat { get; set; }
        public double Long { get; set; }
    }

Next, let’s add the reference in our Customer class, the complete version is below:

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

Now with that out of the way, we can get to building the generator.

ObjectHydrator comes with an interface which our generator will implement. The interface is pretty simple (shown below).

using System;
using System.Collections.Generic;
using System.Text;
namespace FoundationStandard.ObjectHydrator.Interfaces
{
    public interface IGenerator
    {
        object Generate();
    }
    public interface IGenerator<T>
    {
        T Generate();
    }
}

Next let’s talk a little about Latitude and Longitude. Our generator will need to create values that fall into the valid ranges of Latitude and Longitude. For Latitude values range from -90 to 90, and for Longitude the limits are -180 to 180. I’ll paste the result in below…I think it will make sense when you see it.

public class LocationGenerator : IGenerator<Location>
    {
        private double LatMin = -90d;
        private double LatMax = 90d;
        private double LongMin = -180d;
        private double LongMax = 180d;
        Random random;
        public LocationGenerator()
        {
            random= RandomSingleton.Instance.Random;
        }
        public Location Generate()
        {
            var next = random.NextDouble();
            double Lat= LatMin + (next * (LatMax - LatMin));
            next = random.NextDouble();
            double Long = LongMin + (next * (LongMax - LongMin));
            return new Location {Lat = Lat, Long = Long};
        }
    }

What this will do is return a Location object filled with a valid Latitude and Longitude. Now that we have a generator, let’s revisit the calling syntax with our new generator.

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

The line .With(x=>x.Location,new LocationGenerator() returns a Location object and drops in the Location property of the Customer object. Let’s take a look at the output:

Where in the world are these places???

Where in the world are these places???

And there you have it…a custom generator. The code for this can be found in the Github repo in the CustomeGenerator branch.

https://github.com/PrintsCharming/ObjectHydratorGettingStarted

Blazor Sourcery with ObjectHydrator

Getting started with ObjectHydrator