When dealing with POCOs, a common scenario is to track changes. Look at this interface:

namespace DirtyObjects
{
    interface IView
    {
        string Name { set;get;}
        int Weight { set;get;}
        bool Modified { set;get;}
    }
}

It is the interface of a simple view class containing the name and weight of a product. The Modified property is used to track whether the object has changed its value, so that we know if we should save the object after it has been edited.

A typical implementation might look like this:

namespace DirtyObjects
{
    class MyCumbersomeView : IView
    {
        private string name;
        private int weight;
        private bool modified = false;
 
        public int Weight
        {
            get { return weight; }
            set
            {
                if (weight != value)
                {
                    weight = value;
                    modified = true;
                }
            }
        }
 
        public string Name
        {
            get { return name; }
            set
            {
                if (name != value)
                {
                    name = value;
                    modified = true;
                }
            }
        }
 
        public bool Modified
        {
            get { return modified; }
            set { modified = value; }
        }	
    }
}

It can be seen above that modified is set to true if a change is detected whenever a setter is called. This is pretty straightforward, yet it seems that an annoying pattern emerges when there is more than a few properties.

How can we then get rid of all the boilerplate code, all the noisy bits in between? Well, there is a way: by using Castle‘s DynamicProxy and a simple technique from aspect oriented programming called method interception. Read on to see an example.
Continue reading »

A fluent interface

c# Comments Off
Jan 152008

A funny thing in programming, captured in a term by Martin Fowler and Eric Evans, is fluent interface.

Explained shortly, it is a style in programming which attempts to make your programs (or parts thereof) resemble sentences as you could (but probably seldomly would) have pronounced them in a fairly understandable human language.

It is easier in dynamic languages like Ruby, but it is still possible in a staticly bound world like that of C# – and it is often a fun challenge to design your utility classes or domain logic to use fluent interface! Moreover, it has a legitimate use in implementing internal domain-specific languages because of its resemblance to human language.

A small example of a list converter class using some kind of fluent interface can be seen if you carry on reading below.
Continue reading »

Jan 142008

Greetings! Welcome to yet another coding blog!

There are so many coding blogs out there, so what is my justification for starting a new one? I believe that it is important that a blog has some kind of thematic, or else it will become this anonymous stream of nothingness that noone will read.

Having said that, I am sorry to admit that I have yet to come up with a theme for this blog. My plan is to post whatever comes into my mind, and then, hopefully, the blog will converge towards something meaningful. I do know, however, that I will be posting mostly C# code, perhaps mixed with a bit of Ruby.

So for now, it will serve a simple purpose as an outlet for my thoughts on coding… – though it would not hurt if someone besides me read it… :-)

© 2010 mookid on code Suffusion WordPress theme by Sayontan Sinha