13 messaggi dal 06 gennaio 2006
Ciao a tutti,
sto uitlizzando con soddisfazione Unity per la gestione IOc dei miei applicativi.
Ho la necessità di creare un oggetto che è l'insieme di due interfacce e quando chiamo i relativi metodi devono essere eseguiti i metodi sulle due istanze corrette, con il seguente codice il tutto funziona:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.Unity.InterceptionExtension;
using Microsoft.Practices.Unity;
using System.Reflection;
using System.Collections;

namespace ConsoleApplication21
{
    class Program
    {
        static void Main(string[] args)
        {

            UnityContainer c = new UnityContainer();
            
            c.AddNewExtension<Interception>();
            c.RegisterType<IMyInterface, MyInterfaceImpl>();
            c.RegisterType(typeof(IBaseClass), typeof(BaseClass),
                        new Interceptor<InterfaceInterceptor>(),
                        new AdditionalInterface(typeof(IMyInterface)),
                        new InterceptionBehavior(new LogMethosInterceptorBehavior()),
                        new InterceptionBehavior(new VirPropInterceptorBehavior(typeof(IMyInterface), c.Resolve <IMyInterface>() )));
            
            IBaseClass instance = c.Resolve < IBaseClass>();
            IMyInterface instance2 = (IMyInterface) instance;

            instance.MethodBase();
            instance2.Method1();

        }
    }

    public class LogMethosInterceptorBehavior : IInterceptionBehavior
    {
        public IEnumerable<Type> GetRequiredInterfaces()
        {
            return new Type[] { };
        }

        public IMethodReturn Invoke(IMethodInvocation input, GetNextInterceptionBehaviorDelegate getNext)
        {
            Console.WriteLine("Calling:" + input.MethodBase.Name);
            return getNext()(input, getNext);
        }

        public bool WillExecute
        {
            get { return true; }
        }
    }
    public class VirPropInterceptorBehavior : IInterceptionBehavior
    {
        private object propVal;
        private readonly Type _t1;
        private readonly object _instance;

        public VirPropInterceptorBehavior(Type t1, object instance)
        {
            _t1 = t1;
            _instance = instance;
        }

        public IMethodReturn Invoke(IMethodInvocation input, GetNextInterceptionBehaviorDelegate getNext)
        {
            if (_t1.IsAssignableFrom(input.MethodBase.ReflectedType) )
            {
                ICollection args = ((ICollection)(input.Arguments));
                object[] values = new object[args.Count];
                args.CopyTo(values, 0);

                object ret = _instance.GetType().GetMethod(input.MethodBase.Name).Invoke(_instance, values);
                return input.CreateMethodReturn(ret, values);
            }
            else
            {
                return getNext()(input, getNext);
            }            
        }

        public IEnumerable<Type> GetRequiredInterfaces()
        {
            return new[] { _t1 };
        }

        public bool WillExecute
        {
            get { return true; }
        }
    }

    public interface IBaseClass
    {
        void MethodBase();
    }

    public class BaseClass : IBaseClass
    {

        public void MethodBase()
        {

        }
    }
    public class MyInterfaceImpl : IMyInterface
    {
        public void Method1()
        {
           
        }
    }
    public interface IMyInterface
    {
        void Method1();
    }
}



L'unico dubbio è che essendo tutti gli interceptor in catena, se dopo al mio VirPropInterceptorBehavior ci fosse un altro interceptor questo non verrebbe chiamato perchè il ritorno di Invoke sul metodo dell'altra interfaccia ritorna il risultato e non la chiamata a getNext (come succede mettedo LogMethosInterceptorBehavior dopo VirPropInterceptorBehavior ).

Esiste un modo differente per avere lo stesso risultato? oppure è un comportamento corretto?

Grazie a tutti

Torna al forum | Feed RSS

ASPItalia.com non è responsabile per il contenuto dei messaggi presenti su questo servizio, non avendo nessun controllo sui messaggi postati nei propri forum, che rappresentano l'espressione del pensiero degli autori.