DynamicProxy mit Spring.Aop aus Spring.NET

Thomas Darimont

Erfahrenes Mitglied
Hallo,

hier mal ein kleines Beispiel wie einfach man mit Spring.NET einen Dynamic Proxy erzeugen kann:
C#:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Spring.Aop.Framework;
using Spring.Aop;
using AopAlliance.Intercept;

namespace De.Tutorials.Spring
{
    public class SpringProxyGeneratorExample
    {
        public static void Main(string[] args)
        {
            ProxyFactory proxyFactory = new ProxyFactory(new BusinessService());
            proxyFactory.AddInterface(typeof(IBusinessService));

            proxyFactory.AddAdvice(new TracingInterceptor());

            IBusinessService businessService = (IBusinessService)proxyFactory.GetProxy();
            Console.WriteLine(businessService.businessOperation("Tom"));
        }
    }

    public interface IBusinessService{
        string businessOperation(string args);
    }

    public class BusinessService : IBusinessService
    {

        #region IBusinessService Members

        public string businessOperation(string args)
        {
            return args + " " + args;
        }

        #endregion
    }

    public class TracingInterceptor : IMethodInterceptor
    {

        #region IMethodInterceptor Members

        public object Invoke(IMethodInvocation invocation)
        {
            object result = null;
            Console.WriteLine("Before: " + invocation);

            result = invocation.Proceed();                

            Console.WriteLine("After: " + invocation);
            return result;
        }

        #endregion
    }

}

Ausgabe:
Code:
Before: Invocation: method 'businessOperation', arguments Tom; target is of Type [De.Tutorials.Spring.BusinessService
After: Invocation: method 'businessOperation', arguments Tom; target is of Type [De.Tutorials.Spring.BusinessService]
Tom Tom

Gruß Tom
 

Neue Beiträge

Zurück