Joe

Mister Doctor Professor
posts - 23, comments - 19, trackbacks - 134

Extension Methods and Fluent Interfaces

File under stupid semantic tricks.

Extension methods (which seem ripe for abuse and all kinds of bad code) provide some great flexibility when working with fluent interfaces.  For example, take the following extensions to the date class

 

    public static class ExtensionMethods
    {
        public static bool IsBefore(this DateTime dt, DateTime other)
        {
            return other > dt;
        }
    }

 

This allows us to better express our intent

 

    public class WorkingWith
    {
        public void Test1()
        {
            DateTime myBirthday = new DateTime(2008, 3, 1);
            DateTime christmans = new DateTime(2008, 12,25);
            
            if (myBirthday.IsBefore(christmans))
            {
                Console.WriteLine("yes");
            }
 
        }
    }

Which is nice, but can get better by wiring in a call to a utility class

 

    public static class ExtensionMethods
    {
        ...
 
        public static IsBetweenDateRange IsBetween(this DateTime dt, DateTime lowDate)
        {
            return new IsBetweenDateRange(dt, lowDate);
        }
    }

which allows a readable (as opposed to parse-then-translate-able) statement such as

        public voidTest2()
        {
            DateTime myBirthday = new DateTime(2008, 3, 1);
            DateTime halloween = new DateTime(2008, 10, 31);
            DateTime christmans = new DateTime(2008, 12,25);
            
            if (halloween.IsBetween(myBirthday).And(christmans))
            {
                
            }
        }

 

The utility class in this case being

    public class IsBetweenDateRange
    {
        private DateTime _baseDate;
        private DateTime _lowDate;
 
        public IsBetweenDateRange(DateTime baseDate, DateTime lowDate)
        {
            _baseDate = baseDate;
            _lowDate = lowDate;
        }
 
        public bool And(DateTime highDate)
        {
            return _baseDate > _lowDate && _baseDate < highDate;
        }
 
    }

Print | posted on Thursday, January 10, 2008 8:52 AM |

Feedback

Gravatar

# Animal sex.

Gay animal sex. Animal sex pictures. Animal and human sex. Animal sex dvds. Animal sex stories. Sex animal. Free animal sex pics. Animal sex.
8/12/2008 5:50 PM | Animal sex.
Gravatar

# Angelina jolie diet and exercise.

Angelina jolie original sin. Angelina jolie.
8/26/2008 1:10 AM | Angelina jolie.
Gravatar

# Jessica simpson.

Jessica simpson. Nude jessica simpson.
9/16/2008 9:09 PM | Jessica simpson s breasts.
Gravatar

# Free animal sex videos.

Animal sex video. Farm animal sex. Animal sex.
10/18/2008 11:31 AM | Animal sex.
Gravatar

# Incest.

Incest.
12/25/2008 1:35 AM | Incest.
Gravatar

# Lesbian sex.

Lesbian sex.
12/29/2008 12:16 AM | Lesbian sex.
Gravatar

# Amateur porn tryouts.

Amateur porn free. At home sex network live amateur porn chat. Free amateur porn. Amateur porn galleries. Your amateur porn. Amateur porn.
12/30/2008 9:32 AM | Amateur couples porn.
Gravatar

# Her first lesbian sex.

Lesbian sex stories. Lesbian. Black lesbian. Lesbian xxx. Lesbian porno. Free lesbian videos. Free lesbian porn. Teen lesbian.
12/31/2008 6:41 AM | Lesbian chat.
Gravatar

# Sex.

Animal sex movies. Gay sex. Virtual sex. Sex positions.
12/31/2008 8:01 AM | Sex movies.
Gravatar

# Rape video.

Rape fantasy stories.
12/31/2008 7:59 PM | Rape stories.

Post Comment

Title  
Name  
Email
Url
Comment   
Please add 5 and 3 and type the answer here:

Powered by: