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



 Friday, January 11, 2008

Inspired by Pinku Surana's blog about DSL's and after reading the presentation from Neal Ford I felt compelled to experiment with a little DSL style programming.

My goal was to get as close as possible to something like:

Person mark = Person.Named("Mark").BornOn(theTwentyFifth).Of(September).InTheYear(1972);

As it turns out the linking of methods is pretty easy. Where you might be inclined to create a property, just create a method which returns the base object, in this case Person.
The hardest part I found was trying to come up with something decent for 'theTwentyFifth' and 'September'. My first try was to create enumerations, but then you have to name the enum and you get something like:

Person mark = Person.Named("Mark").BornOn(Day.theTwentyFifth).Of(Month.September).InTheYear(1972);

Which is good because the intellisense in C# is great, but having to prefix it messes it up just a little. An Alternative implementation is to 'abuse' generics and do it like this:

Person mark = Person.Named("Mark").BornOn<theTwentyFifth>().Of<September>().InTheYear(1972);

The draw back to this is that intellisense is less useful and it is messy to be using generics for BornOn and Of, but not for InTheYear.
The implementation is below. I'm still thinking about which solution is more useful.


namespace DSL
{
    class Program
    {
        static void Main(string[] args)
        {
            Person mark = Person.Named("Mark").BornOn<theTwentyFifth>().Of<September>().InTheYear(1972);
        }
    }

    public abstract class CalenderMonth
    {
        public int Month { get; private set; }

        protected CalenderMonth(int month)
        {
            Month = month;
        }
    }

    public class September : CalenderMonth
    {
        public September()
            : base(9)
        {
        }
    }


    public abstract class DayOfMonth
    {
        public int Day { get; private set; }

        protected DayOfMonth(int day)
        {
            Day = day;
        }
    }

    public class theTwentyFifth : DayOfMonth
    {
        public theTwentyFifth()
            : base(25)
        {
        }
    }

    public class Person
    {
        DateTime _dob = new DateTime();
        string _name;


        public const int theFirst = 1;
        public const int theTwentyFifth = 25;

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

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

        public Person BornOn<T>() where T : DayOfMonth, new()
        {
            T d = new T();
            _dob = new DateTime(_dob.Year, _dob.Month, d.Day);
            return this;
        }

        public Person Of<T>() where T : CalenderMonth, new()
        {
            T m = new T();
            _dob = new DateTime(_dob.Year, m.Month, _dob.Day);
            return this;
        }

        public Person InTheYear(int year)
        {
            _dob = new DateTime(year, _dob.Month, _dob.Day);
            return this;
        }
    }

}

 

Friday, January 11, 2008 7:24:33 PM (Eastern Standard Time, UTC-05:00)  #    Comments [0] -
C#
Tracked by:
"http://morningside.edu/mics/_notes/pages/rainbow-brite/index.html" (http://morn... [Pingback]
"http://blastpr.com/wiki/js/pages/zoloft/index.html" (http://blastpr.com/wiki/js... [Pingback]
"http://morningside.edu/mics/_notes/pages/coumadin/index.html" (http://morningsi... [Pingback]
"http://blastpr.com/wiki/js/pages/coumadin/index.html" (http://blastpr.com/wiki/... [Pingback]
"http://morningside.edu/mics/_notes/pages/prozac/index.html" (http://morningside... [Pingback]
"http://blastpr.com/wiki/js/pages/rainbow-brite/index.html" (http://blastpr.com/... [Pingback]
"http://morningside.edu/mics/_notes/pages/hoodia/index.html" (http://morningside... [Pingback]
"http://morningside.edu/mics/_notes/pages/melatonin/index.html" (http://mornings... [Pingback]
"http://morningside.edu/mics/_notes/pages/paxil/index.html" (http://morningside.... [Pingback]
"http://morningside.edu/mics/_notes/pages/wellbutrin/index.html" (http://morning... [Pingback]
"http://morningside.edu/mics/_notes/pages/nexium/index.html" (http://morningside... [Pingback]
"http://morningside.edu/mics/_notes/pages/soma/index.html" (http://morningside.e... [Pingback]
"http://blastpr.com/wiki/js/pages/lipitor/index.html" (http://blastpr.com/wiki/j... [Pingback]
"http://morningside.edu/mics/_notes/pages/ultram/index.html" (http://morningside... [Pingback]
"http://morningside.edu/mics/_notes/pages/prilosec/index.html" (http://morningsi... [Pingback]
"http://blastpr.com/wiki/js/pages/effexor/index.html" (http://blastpr.com/wiki/j... [Pingback]
"http://blastpr.com/wiki/js/pages/viagra/index.html" (http://blastpr.com/wiki/js... [Pingback]
"http://morningside.edu/mics/_notes/pages/viagra/index.html" (http://morningside... [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/accutane/index.html" (http://morningsi... [Pingback]
"http://blastpr.com/wiki/js/pages/celebrex/index.html" (http://blastpr.com/wiki/... [Pingback]
"http://blastpr.com/wiki/js/pages/clomid/index.html" (http://blastpr.com/wiki/js... [Pingback]
"http://blastpr.com/wiki/js/pages/wellbutrin/index.html" (http://blastpr.com/wik... [Pingback]
"http://blastpr.com/wiki/js/pages/prozac/index.html" (http://blastpr.com/wiki/js... [Pingback]
"http://blastpr.com/wiki/js/pages/soma/index.html" (http://blastpr.com/wiki/js/p... [Pingback]
"http://morningside.edu/mics/_notes/pages/claritin/index.html" (http://morningsi... [Pingback]
"http://blastpr.com/wiki/js/pages/lexapro/index.html" (http://blastpr.com/wiki/j... [Pingback]
"http://blastpr.com/wiki/js/pages/prilosec/index.html" (http://blastpr.com/wiki/... [Pingback]
"http://morningside.edu/mics/_notes/pages/tramadol/index.html" (http://morningsi... [Pingback]
"http://morningside.edu/mics/_notes/pages/lexapro/index.html" (http://morningsid... [Pingback]
"http://blastpr.com/wiki/js/pages/hoodia/index.html" (http://blastpr.com/wiki/js... [Pingback]
"http://blastpr.com/wiki/js/pages/cymbalta/index.html" (http://blastpr.com/wiki/... [Pingback]
About
This blog is run by Mark Blomsma.
© Copyright 2008
Develop-One
Sign In
Statistics
Total Posts: 334
This Year: 78
This Month: 2
This Week: 0
Comments: 94
All Content © 2008, Develop-One
DasBlog theme 'Business' created by Christoph De Baene (delarou)