Click here to Skip to main content
Click here to Skip to main content
Technical Blog

Tagged as

Object Decoration – Solution to Runtime Behaviors for Compiled Languages

, 14 Mar 2012 CPOL
Rate this:
Please Sign up or sign in to vote.
Discuss adding runtime behaviors for compiled languages

Compiled languages have been criticized for their inability to add 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.


License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Gary H Guo

United States United States
Object-oriented (OO) is about "classes" not "objects". But I truly believe that "objects" deserve more our attentions. If you agree, read more on... Dynamic Object Programming (DOP), Component-Based Object Extender (CBO Extender), AOP Container and Dynamic Decorator Pattern.
 
Mobile development is not just another type of front end. The real challenge is actually in the back end: How to present meaningful information in time to mobile users with exponentially increased data flooding around? Here is my first mobile solution: SmartBars - Barcode Reader, Price Comparison and Coupons.
 
Gary lives in southeast Michigan. My first programming language is FORTRAN. For the last a few years, I have primarily focused on .NET technologies with Mobile Development as my newest interest.

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.141220.1 | Last Updated 14 Mar 2012
Article Copyright 2012 by Gary H Guo
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid