Click here to Skip to main content
Licence CPOL
First Posted 21 Jul 2011
Views 4,030
Downloads 44
Bookmarked 4 times

Generic Dynamic Decorator

By | 22 Jul 2011 | Technical Blog
The Generic Dynamic Decorator enhances the Dynamic Decorator to be strongly typed by using .NET Generics
A Technical Blog article. View original blog here.[^]

The Generic Dynamic Decorator enhances the Dynamic Decorator to be strongly typed by using .NET Generics.

As you may be already aware of, when creating proxy using ObjectProxyFactory.CreateProxy you need to cast it to a specific interface type, whose methods you want to attach extra behaviors to. The issue is that if it is cast to a wrong type of interface, the code passes the compilation and even executes until you use it to call a method. Then, it generates a runtime error.

With the Generic Dynamic Decorator, the ObjectProxyFactory.CreateProxy<T> returns a strongly-typed interface. There is no need to do the dynamic cast any more. If a wrong type of interface is specified, the code may pass the compilation but will generate a runtime error during the creation of the proxy before you get a chance to use the interface to call a method.

The following code demonstrates how to use the Generic Dynamic Decorator to add entering log to the method DetailsByLevel of the object emp of the class Employee.

static void Main(string[] args)
{
    IEmployee emp = new Employee(1, "John", "Smith", new DateTime(1990, 4, 1), 1);
    System.Int32? id = null;
    System.String detail = "";
 
    emp = ObjectProxyFactory.CreateProxy<IEmployee>(
        emp,
        new String[] { "DetailsByLevel" },
        new Decoration((x, y) =>
        {
            IMethodCallMessage method = x.CallCtx;
            string str = "Calling " + x.Target.GetType().ToString() + 
				"." + method.MethodName +
                "(";
            int i = 0;
            foreach (object o in method.Args)
            {
                if (i > 0)
                    str = str + ", ";
                str = str + o.ToString();
            }
            str = str + ")";
 
            Console.WriteLine(str);
            Console.Out.Flush();
        }, null),
        null);
 
    id = emp.DepartmentID;
    detail = emp.DetailsByLevel(2);
}

More information about extending functionality of an object using Dynamic Decorator can be found in the article Dynamic Decorator Pattern.

For examples and features which can be used to write powerful aspects for objects, please refer to the article Add Aspects to Object Using Dynamic Decorator.

For principles on how to use the Dynamic Decorator in your application development in general, please refer to the article Components, Aspects, and Dynamic Decorator.

For using Dynamic Decorator in ASP.NET application, please refer to the article Components, Aspects and Dynamic Decorator for ASP.NET Application.

For using Dynamic Decorator in ASP.NET MVC application, please refer to the article Components, Aspects, and Dynamic Decorator for ASP.NET MVC Applications.

For using Dynamic Decorator in Silverlight/WCF application, please refer to the article Components, Aspects, and Dynamic Decorator for Silverlight / WCF Service Applications.

For using Dynamic Decorator in MVC/AJAX/REST application, please refer to the article Components, Aspects and Dynamic Decorator for MVC/AJAX/REST Application.


modified on Thursday, July 21, 2011 12:16 PM

License

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

About the Author

Gary H Guo



United States United States

Member

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.
 
Gary lives in southeast Michigan and works as a Senior Consultant in a consulting group. We provide technology solutions to complicate business problems using Web strategy and Business Intelligence. My first programming language is FORTRAN. For the last a few years, I have primarily focused on .NET technologies, SQL Server and Business Objects, etc.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
-- There are no messages in this forum --
Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120517.1 | Last Updated 22 Jul 2011
Article Copyright 2011 by Gary H Guo
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid