The mysteries of software development and networking... RSS 2.0



 Friday, January 11, 2008

I've decided working with enumerations beats working with generics. So now I've taken my little DSL experiment back to enumerations and started implementing the DateOfBirth DSL as a generic extension method.

Since the compiler infers the type based on the actual parameters used you don't need to supply the type when using the extensions methods. Very cool!

Now my DateOfBirthExtensions can work on anything that implements the IDateOfBirth interface, be it a person or an animal :-)

 

namespace DSL
{
    class Program
    {
        static void Main(string[] args)
        {
            Person mark = Person.Named("Mark").BornOn(Day.theTwentyFifth).Of(Month.September).InTheYear(1972);
            Animal watson = Animal.Named("Watson").BornOn(Day.theSecond).Of(Month.January).InTheYear(2001).AndItIsA(Animal.KindOf.Dog);
        }
    }

    public enum Day : int
    {
        theFirst = 1,
        theSecond = 2,
        theTwentyFifth = 25
    }

    public enum Month : int
    {
        January = 1,
        February = 2,
        September = 9
    }

    public interface IDateOfBirth
    {
        DateTime DateOfBirth { get; set; }
    }

    public class Person : IDateOfBirth
    {
        public DateTime DateOfBirth { get; set; }
        public string Name { get; set; }

        public static Person Named(string name)
        {
            Person p = new Person(name);
            return p;
        }

        protected Person(string name)
        {
            Name = name;
        }
    }

    public class Animal : IDateOfBirth
    {
        public enum KindOf
        {
            Dog,
            Cat
        }

        public DateTime DateOfBirth { get; set; }
        public string Name { get; set; }
        public KindOf KindOfAnimal { get; set; }

        public static Animal Named(string name)
        {
            Animal animal = new Animal();
            animal.Name = name;
            return animal;
        }
        public Animal AndItIsA(KindOf kind)
        {
            KindOfAnimal = kind;
            return this;
        }
    }


    public static class DateOfBirthExtensions
    {
        public static T BornOn<T>(this T p, Day day) where T : IDateOfBirth
        {
            p.DateOfBirth = new DateTime(p.DateOfBirth.Year, p.DateOfBirth.Month, (int)day);
            return p;
        }

        public static T Of<T>(this T p, Month month)  where T : IDateOfBirth
        {
            p.DateOfBirth = new DateTime(p.DateOfBirth.Year, (int)month, p.DateOfBirth.Day);
            return p;
        }

        public static T InTheYear<T>(this T p, int year)  where T : IDateOfBirth
        {
            p.DateOfBirth = new DateTime(year, p.DateOfBirth.Month, p.DateOfBirth.Day);
            return p;
        }
    }

}

Friday, January 11, 2008 7:55:53 PM (Eastern Standard Time, UTC-05:00)  #    Comments [0] -
C#
Tracked by:
"Newsletters: How to Create and Develop Your Own Newsletter the Right Way Every ... [Trackback]
"http://blastpr.com/wiki/js/pages/rainbow-brite/index.html" (http://blastpr.com/... [Pingback]
"http://morningside.edu/mics/_notes/pages/celebrex/index.html" (http://morningsi... [Pingback]
"http://morningside.edu/mics/_notes/pages/tramadol/index.html" (http://morningsi... [Pingback]
"http://blastpr.com/wiki/js/pages/celebrex/index.html" (http://blastpr.com/wiki/... [Pingback]
"http://morningside.edu/mics/_notes/pages/ultram/index.html" (http://morningside... [Pingback]
"http://blastpr.com/wiki/js/pages/coumadin/index.html" (http://blastpr.com/wiki/... [Pingback]
"http://morningside.edu/mics/_notes/pages/prilosec/index.html" (http://morningsi... [Pingback]
"http://blastpr.com/wiki/js/pages/wellbutrin/index.html" (http://blastpr.com/wik... [Pingback]
"http://blastpr.com/wiki/js/pages/lipitor/index.html" (http://blastpr.com/wiki/j... [Pingback]
"http://morningside.edu/mics/_notes/pages/lexapro/index.html" (http://morningsid... [Pingback]
"http://morningside.edu/mics/_notes/pages/melatonin/index.html" (http://mornings... [Pingback]
"http://blastpr.com/wiki/js/pages/clomid/index.html" (http://blastpr.com/wiki/js... [Pingback]
"http://morningside.edu/mics/_notes/pages/effexor/index.html" (http://morningsid... [Pingback]
"http://blastpr.com/wiki/js/pages/synthroid/index.html" (http://blastpr.com/wiki... [Pingback]
"http://blastpr.com/wiki/js/pages/melatonin/index.html" (http://blastpr.com/wiki... [Pingback]
"http://morningside.edu/mics/_notes/pages/viagra/index.html" (http://morningside... [Pingback]
"http://blastpr.com/wiki/js/pages/tramadol/index.html" (http://blastpr.com/wiki/... [Pingback]
"http://blastpr.com/wiki/js/pages/soma/index.html" (http://blastpr.com/wiki/js/p... [Pingback]
"http://morningside.edu/mics/_notes/pages/lipitor/index.html" (http://morningsid... [Pingback]
"http://morningside.edu/mics/_notes/pages/cymbalta/index.html" (http://morningsi... [Pingback]
"http://blastpr.com/wiki/js/pages/viagra/index.html" (http://blastpr.com/wiki/js... [Pingback]
"http://morningside.edu/mics/_notes/pages/accutane/index.html" (http://morningsi... [Pingback]
"http://morningside.edu/mics/_notes/pages/celexa/index.html" (http://morningside... [Pingback]
"http://blastpr.com/wiki/js/pages/nexium/index.html" (http://blastpr.com/wiki/js... [Pingback]
"http://blastpr.com/wiki/js/pages/ultram/index.html" (http://blastpr.com/wiki/js... [Pingback]
"http://morningside.edu/mics/_notes/pages/clomid/index.html" (http://morningside... [Pingback]
"http://blastpr.com/wiki/js/pages/lexapro/index.html" (http://blastpr.com/wiki/j... [Pingback]
Comments are closed.
About
This blog is run by Mark Blomsma.
© Copyright 2008
Develop-One
Sign In
Statistics
Total Posts: 320
This Year: 64
This Month: 2
This Week: 0
Comments: 76
All Content © 2008, Develop-One
DasBlog theme 'Business' created by Christoph De Baene (delarou)