Skip to content

Object Decoration – Solution to Runtime Behaviors for Compiled Languages

January 26, 2012

Compiled languages have been criticized for their inability to addding behaviors at runtime as their counterpart – interpreted languages – does. That should not be the case. With object decoration, behaviors can be added to objects at runtime in compiled languages. It is a simple yet powerful way for software extensibility. It complements class-based programming of compiled languages and has advantages over dynamic typing of interpreted languages.

Thinking in Object Decoration

Object decoration means attaching extra behaviors to an object. Here is the idea: You have an object whose methods perform defined behaviors. Now, you want some extra behaviors before or after the execution of methods. So, you attach the extra behaviors to the methods by decoration so that when invoking the methods at runtime the attached behaviors are executed before or after.

Once you get the idea, decoration itself is straightforward. At the very basic, you need to specify an object, method(s) of the object, extra behavior(s) and placements of the extra behavior(s) (before or after). All these are done in one call as follows.

doAThing = ObjectProxyFactory.CreateProxy2<IDoAnything>(
    doAThing,
    new string[] { "DoThing" },
    new Decoration2(SayHello, null),
    new Decoration2(SayBye, null)
);

Here, you specify an object doAThing as the first argument, then, its method DoThing, the preprocessing (before) behavior SayHello and the postprocessing (after) behavior SayBye. All it says is that I want to add behavior SayHello before and behavior SayBye after the method DoThing of the object doAThing. That call returns a proxy of the original object. Then, you can use the proxy as if it was the original object. Now, the code

doAThing.DoThing();

executes SayHello first, then the actual method DoThing, and last SayBye.

The complete code is listed as follows.

using CBOExtender;

namespace HelloWorld
{
    public interface IDoAnything {
        void DoThing();
    }

    public class DoAnything : IDoAnything {
        public void DoThing() { }
    }

    class Program
    {
        static void Main(string[] args)
        {
            IDoAnything doAThing = new DoAnything();

            doAThing = ObjectProxyFactory.CreateProxy2<IDoAnything>(
                doAThing,
                new string[] { "DoThing" },
                new Decoration2(SayHello, null),
                new Decoration2(SayBye, null)
            );

            doAThing.DoThing();
        }

        public static void SayHello(AspectContext2 ctx, dynamic parameters)
        {
            System.Console.WriteLine("Hello!");
        }

        public static void SayBye(AspectContext2 ctx, dynamic parameters)
        {
            System.Console.WriteLine("Bye!");
        }
    }
}

More Information

To download CBOExtender, use NuGet from inside Visual Studio 10 by searching CBOExtender, or click here.

To see how logging, security checking and sorting are added to objects as extra behaviors at runtime by decoration, click here.

To see how transaction is added to objects as extra behaviors at runtime by decoration, click here.

To see how object decoration is different from dynamic typing, click here.

To see all my articles and blogs, click here.

Advertisements

From → CodeProject

Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: