Writing Fluent APIs in C#: Comparing Dates Fluently

February 13, 2010

in .NET 4.0,Fluent Interfaces,IntelliJ IDEA,Java,Visual Studio 2010

Kent Beck tweeted:  screencast of a simple example of a fluent api driven by tests from @jitterted http://bit.ly/cSqtJE.

Using VS 2010 RC, C# and .NET 4.0

Ted Young gives a nice introduction into building fluent APIs using Java. He returns an instance of a DateBuilder to manage the dates. The .NET example copies this and uses extension methods.

I used VS 2010’s generate stub tooling for creating methods and classes while following Ted using IntelliJ IDEA in his video.

   1: using System;

   2:  

   3: namespace ConsoleApplication1

   4: {

   5:     class Program

   6:     {

   7:         static void Main(string[] args)

   8:         {

   9:             var somedate = new DateTime(2010, 1, 10);

  10:             var otherDate = new DateTime(2010, 1, 31);

  11:  

  12:             System.Diagnostics.Debug.Assert(somedate.IsBefore(otherDate));

  13:             System.Diagnostics.Debug.Assert(otherDate.IsBefore(somedate) == false);

  14:  

  15:             System.Diagnostics.Debug.Assert(Dates.Is(otherDate).OnOrAfter(somedate));

  16:             System.Diagnostics.Debug.Assert(Dates.Is(somedate).OnOrAfter(otherDate) == false);

  17:  

  18:             System.Diagnostics.Debug.Assert(Dates.Is(somedate).OnOrBefore(otherDate));

  19:             System.Diagnostics.Debug.Assert(Dates.Is(otherDate).OnOrBefore(somedate) == false);

  20:  

  21:             System.Diagnostics.Debug.Assert(Dates.Is(otherDate).On(otherDate));

  22:             System.Diagnostics.Debug.Assert(Dates.Is(otherDate).On(somedate) == false);

  23:  

  24:             

  25:  

  26:         }

  27:     }

  28:  

  29:     public static class Dates

  30:     {

  31:         public static bool IsBefore(this DateTime firstDateTime, DateTime otherDate)

  32:         {

  33:             return (firstDateTime <= otherDate) ? true : false;

  34:         }

  35:  

  36:         public static bool IsAfter(this DateTime firstDateTime, DateTime otherDate)

  37:         {

  38:             return (firstDateTime >= otherDate) ? true : false;

  39:         }

  40:  

  41:         public static DatesBuilder Is(DateTime firstDateTime)

  42:         {

  43:             return new DatesBuilder(firstDateTime);

  44:         }

  45:  

  46:         public class DatesBuilder

  47:         {

  48:             private DateTime _firstDateTime;

  49:  

  50:             public DatesBuilder(DateTime firstDateTime)

  51:             {

  52:                 this._firstDateTime = firstDateTime;

  53:             }

  54:  

  55:             public bool OnOrAfter(DateTime otherDate)

  56:             {

  57:                 return !_firstDateTime.IsBefore(otherDate);

  58:             }

  59:  

  60:             public bool OnOrBefore(DateTime otherDate)

  61:             {

  62:                 return !_firstDateTime.IsAfter(otherDate);

  63:             }

  64:  

  65:             public bool On(DateTime otherDate)

  66:             {

  67:                 return _firstDateTime.Equals(otherDate);

  68:             }

  69:         }

  70:     }

  71: }

{ 1 trackback }

UPDATE: Writing Fluent APIs in C#: Comparing Dates Fluently
03.07.10 at 3:17 pm

{ 4 comments… read them below or add one }

Prof. Roger Crawfis 03.01.10 at 2:14 pm

So, I looked at this and Ted’s original article (a link to that is here).

I do not see the point to the DateBuilder class here. Extension methods would be a better approach (IMHO) for all of the methods. There is no reason to have this class.

Roger

Nick Robinson 03.28.10 at 10:39 am

I quite like the idea of doing:

DateTime someTime = ….

if(someTime.IsAfter(otherTime)) …

Which does achieve Teds original objective of making the code/compare more readable. I think canonically smaller builder classes are used however, so that the syntax available as you move down the fluent language tree, are restricted/minimised. I can see ted is just following that approach. But I do prefer the code above.

Nick.

Doug Finke 03.28.10 at 6:21 pm

@Nick, here is an updated version using extension methods.
UPDATE: Writing Fluent APIs in C#: Comparing Dates Fluently

Rakesh 08.23.11 at 2:23 pm

This is a nice article on fluent apis..there’s a similar one at http://dotnetfluent.com/csharp/csharp-net-4-0/implementing-a-fluent-api-using-extension-methods-in-c-4-0/

It uses extension methods..

Leave a Comment

You can use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Contrat Creative Commons

© 2007-2013, Doug Finke