Esempio n. 1
0
        /// <summary>
        /// Returns the advices for this invocation
        /// </summary>
        /// <returns></returns>
        public IList <ISolidProxyInvocationAdvice <TObject, TMethod, TAdvice> > GetSolidInvocationAdvices()
        {
            lock (_mutex)
            {
                if (_advices != null)
                {
                    return(_advices);
                }
                var stepTypes = MethodConfiguration.GetSolidInvocationAdviceTypes();
                var sp        = ProxyConfiguration.SolidProxyConfigurationStore.ServiceProvider;

                //
                // create advices
                //
                _advices = stepTypes.Select(t =>
                {
                    if (t.IsGenericTypeDefinition)
                    {
                        switch (t.GetGenericArguments().Length)
                        {
                        case 1:
                            t = t.MakeGenericType(new[] { typeof(TObject) });
                            break;

                        case 2:
                            t = t.MakeGenericType(new[] { typeof(TObject), typeof(TMethod) });
                            break;

                        case 3:
                            t = t.MakeGenericType(new[] { typeof(TObject), typeof(TMethod), typeof(TAdvice) });
                            break;

                        default:
                            throw new Exception("Cannot create handler.");
                        }
                    }

                    var step = (ISolidProxyInvocationAdvice <TObject, TMethod, TAdvice>)sp.GetService(t);
                    if (step == null)
                    {
                        throw new Exception($"Advice not registered in service provider:{t.FullName}");
                    }
                    return(step);
                }).ToList();

                _advices = new ReadOnlyCollection <ISolidProxyInvocationAdvice <TObject, TMethod, TAdvice> >(_advices);

                //
                // configure the advices - remove the ones that return false
                // in config method. Do this in reverse order - thus removing
                // the implementation advice before any other advice is configured.
                // This lets the other advices determine if an implementation exists
                // based on if the implementation advice exists.
                //
                foreach (var advice in _advices.Reverse())
                {
                    if (!SolidConfigurationHelper.ConfigureAdvice(advice, this))
                    {
                        _advices = new ReadOnlyCollection <ISolidProxyInvocationAdvice <TObject, TMethod, TAdvice> >(_advices.Where(o => o != advice).ToList());
                    }
                }
                return(_advices);
            }
        }