Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to build a Fluent Nested Guard API

Tags:

c#

lambda

fluent

I am building a simple Guard API to protect against illegal parameters being passed to functions and so on.

I have the following code:

public static class Guard
{
    public static GuardArgument<T> Ensure<T>(T value, string argumentName)
    {
        return new GuardArgument<T>(value, argumentName);
    }
}

public class GuardArgument<T>
{
    public GuardArgument(T value, string argumentName)
    {
        Value = value;
        Name = Name;
    }

    public T Value { get; private set; }
    public string Name { get; private set; }
}

// Example extension for validity checks
public static GuardArgument<T> IsNotNull<T>(this GuardArgument<T> guardArgument, string errorMessage)
{
    if (guardArgument.Value == null)
    {
        throw new ArgumentNullException(guardArgument.Name, errorMessage);
    }

    return guardArgument;
}     

At the moment the code can be used in a similar way to (note this is just a dumb example):

void DummyMethod(int? someObject) {

    Guard.Ensure(someObject, "someObject")
       .IsNotNull()
       .IsGreaterThan(0)
       .IsLessThan(10);
}

This all works fine. What I want to be able to do now is extend the API to include child properties in the checks in the following way:

Guard.Ensure(someObject, "someObject")
    .IsNotNull()
    .Property(
        (x => x.ChildProp1, "childProp1")
           .IsNotNull()
           .IsGreaterThan(10)
     )
     .Property(
        (x => x.ChildProp2, "childProp2")
           .IsNotNull()
           .IsLessThan(10)
     );

Obviously the new .Property method needs to return the parent GuardArgument in order to chain. Furthermore the child property needs to be able to use the existing check methods (IsNotNull() etc) to avoid code duplication.

I cannot work out how to construct the lambda/Property function parameters or where the .Property method should be located - i.e. should it be a property on the GuardArgument or somewhere else, or even if there is a better structure to the API.

like image 389
Graham Avatar asked Jan 15 '16 12:01

Graham


1 Answers

The following function allows for a similar syntax to what you want.

public static GuardArgument<T> Property<T, TProp>(this GuardArgument<T> guardArgument, Func<T, TProp> getProperty, string propertyName, Action<GuardArgument<TProp>> validate)
{
    GuardArgument<TProp> propertyGuardArgument = new GuardArgument<TProp>(getProperty(guardArgument.Value), propertyName);

    validate(propertyGuardArgument);

    return guardArgument;
}

The function creates a new GuardArgument for the selected property and then passes this into the Action parameter to allow you to validate as you wish.

This also allows infinite chaining of properties, although I'm not sure that would be particularly readable.

Usage:

Guard.Ensure(someObject, "someObject")
     .IsNotNull()
     .Property(x => x.ChildProp1, "childProp1", childProp1 =>
         childProp1.IsNotNull()
                   .IsLessThan(10)
                   .Property(y => y.InnerChildProperty, "innerChildProperty", innerChildProperty =>
                       innerChildProperty.IsNotNull()
                    )
     )
     .Property(x => x.ChildProp2, "childProp2", childProp2 =>
         childProp2.IsNotNull()
                   .IsGreaterThan(10)
     );
like image 129
Seaal Avatar answered Nov 08 '22 09:11

Seaal