Search

What's this

DecoratorSharp is a simple AOP library used like a python decorator.

How to use it

In python we can use decorator like this
#! /usr/bin/env python
#coding=utf-8

def logger(name):
    def loggerWrapper(fun):
        def logging(self):
            print "User is %s." % name
            print "Start Logging"
            result = fun(self)
            print "End Logging."
            return result
        return logging
    return loggerWrapper

class MyClass():
    @logger("Leven")
    def Test(self):
        print("function MyClass::Test called.")
        return "I am Reuslt."
        

if __name__ == "__main__":
    mc = MyClass()
    print "Result:%s" % mc.Test()

With DecoratorSharp:
public class LoggerAttribute : Attribute, IDecoratorFilter { 
        public string Name { get; private set; } 
        public LoggerAttribute(string name) { 
            Name = name; 
        } 
        public Func<object, object[], object> Execute(DecoratorContext context) { 
            return (instanse, parameters) => { 
                Console.WriteLine("User is {0}.", Name); 
                Console.WriteLine("Start Logging."); 
                var result = context.Invoker(instanse, parameters); 
                Console.WriteLine("End Logging."); 
                return result; 
            }; 
        } 
} 

public class TestClass { 
        [Logger("Leven")] 
        public virtual string Test() { 
            Console.WriteLine("Method TestClass::Test() called."); 
            return "I am Result."; 
        } 
}

Then,you can create a TestClass instanse with TypeFactory
var instanse = TypeFactory.CreateInstanse<TestClass>(); 
Console.WriteLine(instanse.Test());

Console will write:
result

Future

DecoratorSharp now only support proxy mode.I'll continue to optimize its performance and improve its functions.
Last edited Dec 29 2009 at 4:54 PM by leven, version 2
Updating...
© 2006-2012 Microsoft | Get Help | Privacy Statement | Terms of Use | Code of Conduct | Advertise With Us | Version 2012.1.11.18365