Extending AngelaSmith with Custom Property Fillers

AngelaSmith is a package that can intelligently fill objects with random data (See Go Beyond Lorem Ipsum).

Under the hood, it does this by inspecting the properties on your object.  For each property, it looks for a PropertyFiller that matches based on the name of the class, the type of the property and the name of the property. Once a matching property filler is found, AngelaSmith asks for a value from the property filler and uses that value to fill the property.

AngelaSmith has a number of built in property fillers, but as Simon Timms pointed out: It would be nice if we could extend AngelaSmith by plugging in our own custom property fillers. For example, if you are working in the oil industry, you could create a set of fillers to deal with Wells, Rigs, Production, etc.

I want tasty ingredients…not that gross stuff!

Let’s take a simple example. Assume we are working on a domain that contains Recipes. A Recipe has a list of Ingredients, and we would like to fill the Ingredient’s Name with realistic looking data. By default, AngelaSmith doesn’t know anything about ingredients so it will use random text. I don’t want to bake a bicycle, a parachute and a pair of shoes. That sounds disgusting.

public class RecipeIngredient
{
public string Name { get; set; }
}

To create a custom property filler, create a class that inherits from PropertyFiller<T> where T is the type of the property you will be filling. You will need to do 2 things.

1) Call the base constructor, passing in these 2 parameters:

•    objectTypeNames – A list of names matching the class name of objects this filler is targeting

•    propertyNames -  A list of names matching the property name of the property this filler is targeting

2) Implemented the GetValue() method, which should return a value that will be used to fill a property

public class IngredientPropertyFiller : PropertyFiller<string>
{
    public IngredientPropertyFiller()
        : base(new[] { "recipeingredient", "ingredient" }, new[] { "name" })
    {
    }

    private static readonly string[] IngredientsList = new[]
        {
            "Salt", "Pepper", "Olive Oil", "Basil", "Chicken Broth", "Cloves", "Garlic", "Onion", "Eggs",
            "Tomatoes", "Chicken Thighs", "Chicken Breast", "Pork Ribs", "Mozzarella Cheese", "Parmesan Cheese",
            "Butter", "Nutmeg",
            "Fettuccine", "Parsley", "Orange Zest", "Whipping Cream", "Cranberries", "Tomatoe Paste", "Mushrooms",
            "Cambozola Cheese",
            "Green Onions", "Flour", "Bran", "Green Beans", "Lima Beans", "Vinegar", "Mustard", "Water", "Raisins",
            "Corn", "Tabasco", "Cashews", "Celery", "Ground Beef", "Rib Steak", "Asparagus", "Cooking Oil",
            "Soy Sauce",
            "Ketchup", "Round Roast", "Beef Tenderloin", "Pork Tenderloin", "Pork Ribs"
        };

    public override object GetValue()
    {
        return Jen.GetRandomValue(IngredientsList);
    }
}

Now just call use AngelaSmith like you normally would and magically, your ingredients are now filled with actual ingredients….yum!

List<RecipeIngredient> ingredients = Angie.FastList<RecipeIngredient>(100);
foreach (RecipeIngredient ingredient in ingredients)
{
    Console.WriteLine(ingredient.Name);
}

In case you are curious, we implemented this using MEF to discover implementations of IPropertyFiller in the current app domain directory.

What Next?

You can find AngelaSmith on GitHub and Nuget. Let us know what you think, and try building your own fillers. We are actively adding more fillers and we would be happy to include yours!