Fluent API with Nullable this time :)

Reading Time: 2 minutes

Yesterday, I attended Kevin’s session about building a fluent API. One of the things he mentioned while building the API, was that he would have liked to constrain the use of the NotNull extension method to cases where it makes sense. And I thought I knew how, but it was already past 8 PM…

After he released the code for the session this morning, I took another go at it. And behold:

FluentAPI - Nullable


Now, to achieve this is actually really simple once you know the magic. The original code for the NotNull extension method was this:

Let’s first fix this one, so it only allows reference types. Easy: put another type constraint to make sure that TProp is a reference type:

Now, for Nullable types, we need to add another extension method. One where TProp is something nullable, of course.

The magic here is in the difference between TValue and TValue? (or Nullable<TValue>). When calling NotNull, we want to pass a struct or value type for the TValue type parameter, but the RuleBuilder will use the nullable one as property type. While we can’t define NotNull<T, TValue?> or use Nullable in the type constraint, we can pass it along to the RuleBuilder. The compiler is smart enough to deduce the rest using type inference 🙂