Object Validation in an Extension Method

I was trying to find a way to define the state of an object based on a set of rules and assign a name to that set of rules that, then defines the state of the object. Here’s what I came up with:

     public static class Extensions {

          public static bool Is<T>(this T item, Predicate<T> conditions) {

               return conditions(item);

This allows me to define the conditions of any object such that I can give a name to its state based on how the business speaks about the object:

     Predicate<MyObject> InACancelledState = (x => x.Cancelled.HasValue);


This leads me to be able to code fluently about the state of the object at runtime:

     if (obj.Is(InACancelledState)) {

          // Do some stuff



4 thoughts on “Object Validation in an Extension Method

  1. Simply Brilliant.Thx for sharing

  2. Good post Jason.
    Keep ones like this coming!

    Cheers – JR

  3. Sweet

    Btw get on twitter!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s