Results 1 to 2 of 2

Thread: Hardcoded aspects

  1. #1
    Join Date
    Sep 2006
    Posts
    7

    Default Hardcoded aspects

    Hello,

    I would like to apply an interceptor at runtime to a number of registered services. How do I do this?

    To be more precise, I would like to apply a Transaction interceptor to every registered service that implements ITask at runtime. Without XML configuration.

    Is this possible?

  2. #2
    Join Date
    Oct 2005
    Location
    Belgium
    Posts
    213

    Default

    I think you'll find what you need in the ApplicationContext extension points, http://www.springframework.net/doc-l...s.html#d4e1700 .

    Did a quick spike myself, don't know if it'll help but it works

    Code:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Spring.Aop.Framework;
    using Spring.Context;
    using Spring.Context.Support;
    using Spring.Objects.Factory.Support;
    
    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                GenericApplicationContext ctx = new GenericApplicationContext();
                RegisterTaskService(ctx);
                RegisterPostProcessor(ctx);
                ctx.Refresh();
                ITaksService service = ctx.GetObject("TaskService") as ITaksService;
                if (service != null)
                {
                    service.DoIt();
                }
                else
                {
                    Console.WriteLine("Oops, something is wrong.");
                }
                Console.ReadLine();
            }
    
            private static void RegisterPostProcessor(GenericApplicationContext context)
            {
                MyPostProcessor postProcessor = new MyPostProcessor();
                postProcessor.InterceptInstancesOfInterface(typeof(ITaksService));
                context.ObjectFactory.AddObjectPostProcessor(postProcessor);
            }
    
            static void RegisterTaskService(GenericApplicationContext context)
            {
                IObjectDefinitionFactory objectDefinitionFactory = new DefaultObjectDefinitionFactory();
                ObjectDefinitionBuilder builder = ObjectDefinitionBuilder.RootObjectDefinition(objectDefinitionFactory, typeof(TaskService));
                context.RegisterObjectDefinition("TaskService", builder.ObjectDefinition);
    
            }
    
        }
    
        public interface ITaksService
        {
            void DoIt();
        }
    
        public class TaskService
            : ITaksService
        {
    
            #region ITaksService Members
    
            public void DoIt()
            {
                Console.WriteLine("Doing my stuff");
            }
    
            #endregion
        }
    
    
        public class WriteLineAspect
            : AopAlliance.Intercept.IMethodInterceptor
        {
    
            #region IMethodInterceptor Members
    
            public object Invoke(AopAlliance.Intercept.IMethodInvocation invocation)
            {
                Console.WriteLine("Calling");
                object returnValue = invocation.Proceed();
                Console.WriteLine("Finished Calling");
                return returnValue;
            }
    
            #endregion
        }
    
        public class MyPostProcessor
            : Spring.Objects.Factory.Config.IObjectPostProcessor
        {
            private List<Type> typesToIntercept = new List<Type>();
    
            #region IObjectPostProcessor Members
    
            public object PostProcessAfterInitialization(object instance, string objectName)
            {
                if (InstanceShouldBeProxied(instance))
                {
                    ProxyFactory factory = new ProxyFactory(instance);
                    factory.AddAdvice(new WriteLineAspect());
                    return factory.GetProxy();
                }
                return instance;
            }
    
            public object PostProcessBeforeInitialization(object instance, string name)
            {
                return instance;
            }
    
            #endregion
    
            protected virtual bool InstanceShouldBeProxied(object instance)
            {
                return (from t in typesToIntercept
                        where t.IsAssignableFrom(instance.GetType())
                        select t).FirstOrDefault() != null;
    
            }
    
            public void InterceptInstancesOfInterface(Type type)
            {
                typesToIntercept.Add(type);
            }
        }
    
    
    }
    Last edited by .ben; 01-21-2009 at 08:29 PM. Reason: typo

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •