AspectSharp ein AOP Framework für .Net/Mono

Hallo!

Im Moment verwende ich davon nur das DynamicProxy Framework um Logging, Tracing, Validierung und SecurityChecks zu implementieren.

Mit Castle ist die Arbeit mit Dynamic Proxies ein wenig einfacher als mit "purem" .net:

C#:
using System;
using System.Collections.Generic;
using System.Text;
using IInterceptor = Castle.DynamicProxy.IInterceptor;
using ProxyGenerator = Castle.DynamicProxy.ProxyGenerator;

namespace De.Tutorials.Training
{
    public class CastleDynamicProxyExample
    {
        public static void Main(string[] args)
        {
            ProxyGenerator proxyGenerator = new ProxyGenerator();
            IBubuService businessService = (IBubuService)proxyGenerator.CreateProxy(typeof(IBubuService), new TracingInterceptor(), new BubuService());
            Console.WriteLine(businessService.BubuOperation("Foo", 4711));
        }
    }

    public interface IBubuService
    {
        string BubuOperation(string arg, int i);
    }

    public class BubuService : IBubuService
    {

        #region bubuOperation Members

        public string BubuOperation(string arg, int i)
        {
            Console.WriteLine("-->" + arg + " " + i);
            return arg + arg + " " + i;
        }
        #endregion
    }

    public class TracingInterceptor : IInterceptor
    {
        #region IInterceptor Members

        public object Intercept(Castle.DynamicProxy.IInvocation invocation, params object[] args)
        {
            Console.WriteLine("About to call : {0} with args: ({1})", new object[] { invocation.Method.Name, Join(",", args) });
            object result = null;
            try
            {

                result = invocation.Proceed(args);
                Console.WriteLine("Calling: {0} with args: ({1}) produced result: {2}", new object[] { invocation.Method.Name, Join(",", args), result });
            }
            catch (Exception exception)
            {
                Console.WriteLine("Error while calling: {0} with args: {1}", new object[] { invocation.Method.Name, Join(",", args) });
                throw exception;
            }
            return result;
        }
        #endregion

        static string Join(string delimiter, object[] args)
        {
            if (0 == args.Length)
            {
                return string.Empty;
            }
            StringBuilder str = new StringBuilder(args[0].ToString());
            for (int i = 1; i < args.Length; i++)
            {
                str.Append(delimiter);
                str.Append(args[i]);
            }

            return str.ToString();
        }
    }
}

Ausgabe:
Code:
About to call : BubuOperation with args: (Foo,4711)
-->Foo 4711
Calling: BubuOperation with args: (Foo,4711) produced result: FooFoo 4711
FooFoo 4711
Drücken Sie eine beliebige Taste . . .

Mit "purem" .Net ging das so:
http://www.tutorials.de/forum/net-application-und-service-design/249443-dynamic-proxy-unter-net.html

Gruß Tom
 
Zurück