예제 #1
0
        /// <summary>
        /// Adds a handler before the first instance of handler of type T.
        /// </summary>
        /// <typeparam name="T">Type of the handler before which the given handler instance is added.</typeparam>
        /// <param name="handler">The handler to be added to the pipeline.</param>
        public void AddHandlerBefore <T>(IPipelineHandler handler)
            where T : IPipelineHandler
        {
            if (handler == null)
            {
                throw new ArgumentNullException("handler");
            }

            var type = typeof(T);

            if (_handler.GetType() == type)
            {
                // Add the handler to the top of the pipeline
                AddHandler(handler);
                SetHandlerProperties(handler);
                return;
            }

            var current = _handler;

            while (current != null)
            {
                if (current.InnerHandler != null &&
                    current.InnerHandler.GetType() == type)
                {
                    InsertHandler(handler, current);
                    SetHandlerProperties(handler);
                    return;
                }
                current = current.InnerHandler;
            }

            throw new InvalidOperationException(
                      string.Format(CultureInfo.InvariantCulture, "Cannot find a handler of type {0}", type.Name));
        }
예제 #2
0
        public void RemoveHandler <T>()
        {
            ThrowIfDisposed();
            Type typeFromHandle = typeof(T);

            for (IPipelineHandler pipelineHandler = _handler; pipelineHandler != null; pipelineHandler = pipelineHandler.InnerHandler)
            {
                if (pipelineHandler.GetType() == typeFromHandle)
                {
                    if (pipelineHandler == _handler && _handler.InnerHandler == null)
                    {
                        throw new InvalidOperationException("The pipeline contains a single handler, cannot remove the only handler in the pipeline.");
                    }
                    if (pipelineHandler == _handler)
                    {
                        _handler = pipelineHandler.InnerHandler;
                    }
                    if (pipelineHandler.OuterHandler != null)
                    {
                        pipelineHandler.OuterHandler.InnerHandler = pipelineHandler.InnerHandler;
                    }
                    if (pipelineHandler.InnerHandler != null)
                    {
                        pipelineHandler.InnerHandler.OuterHandler = pipelineHandler.OuterHandler;
                    }
                    pipelineHandler.InnerHandler = null;
                    pipelineHandler.OuterHandler = null;
                    return;
                }
            }
            throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, "Cannot find a handler of type {0}", typeFromHandle.Name));
        }
예제 #3
0
        public void AddHandlerBefore <T>(IPipelineHandler handler) where T : IPipelineHandler
        {
            if (handler == null)
            {
                throw new ArgumentNullException("handler");
            }
            ThrowIfDisposed();
            Type typeFromHandle = typeof(T);

            if (_handler.GetType() == typeFromHandle)
            {
                AddHandler(handler);
                SetHandlerProperties(handler);
                return;
            }
            for (IPipelineHandler pipelineHandler = _handler; pipelineHandler != null; pipelineHandler = pipelineHandler.InnerHandler)
            {
                if (pipelineHandler.InnerHandler != null && pipelineHandler.InnerHandler.GetType() == typeFromHandle)
                {
                    InsertHandler(handler, pipelineHandler);
                    SetHandlerProperties(handler);
                    return;
                }
            }
            throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, "Cannot find a handler of type {0}", typeFromHandle.Name));
        }
예제 #4
0
        private static IEnumerable <IPipelineHandler> OrderHandlers(IPipelineHandler[] steps)
        {
            Queue <IPipelineHandler> queue = new Queue <IPipelineHandler>(steps);
            var map     = steps.ToDictionary(h => h.GetType());
            var ordered = new HashSet <Type>();

            while (queue.Count > 0)
            {
                IPipelineHandler     handler      = queue.Dequeue();
                Type                 handlerType  = handler.GetType();
                PipelineDepencency[] dependencies = PipelineDepencency.GetDepencencies(handler);
                if (dependencies.Length < 1 || dependencies.All(d => ordered.Contains(d.Type)))
                {
                    ordered.Add(handlerType);
                }
                else
                {
                    IEnumerable <PipelineDepencency> unknownDependencies = dependencies
                                                                           .Where(dep => !map.ContainsKey(dep.Type))
                                                                           .ToArray();
                    if (unknownDependencies.Any())
                    {
                        string message = handlerType.FullName + " has dependencies to be satisfied, missing dependencies:\n\r"
                                         + string.Join("\n\r - ", unknownDependencies.Select(d => d.Type.FullName));
                        throw new DependencyResolverException(message);
                    }
                    queue.Enqueue(handler);
                }
            }
            return(ordered.Select(type => map[type]).ToList());
        }
예제 #5
0
        protected static string FormatErrorMessage(IPipelineHandler handler, params string[] dependencies)
        {
            var handlerType    = handler == null ? "Handler" : handler.GetType().Name;
            var dependencyText = !dependencies.Any()
                ? "is missing a dependency"
                : "is missing dependenc" +
                                 (dependencies.Count() == 1
                    ? $"y - {dependencies[0]}"
                    : $"ies - {string.Join(",", dependencies)}");

            return($"{handlerType} {dependencyText}");
        }
예제 #6
0
        public void ReplaceHandler <T>(IPipelineHandler handler) where T : IPipelineHandler
        {
            if (handler == null)
            {
                throw new ArgumentNullException("handler");
            }
            ThrowIfDisposed();
            Type             typeFromHandle  = typeof(T);
            IPipelineHandler pipelineHandler = null;

            for (IPipelineHandler pipelineHandler2 = _handler; pipelineHandler2 != null; pipelineHandler2 = pipelineHandler2.InnerHandler)
            {
                if (pipelineHandler2.GetType() == typeFromHandle)
                {
                    handler.InnerHandler = pipelineHandler2.InnerHandler;
                    handler.OuterHandler = pipelineHandler2.OuterHandler;
                    if (pipelineHandler != null)
                    {
                        pipelineHandler.InnerHandler = handler;
                    }
                    else
                    {
                        _handler = handler;
                    }
                    if (pipelineHandler2.InnerHandler != null)
                    {
                        pipelineHandler2.InnerHandler.OuterHandler = handler;
                    }
                    pipelineHandler2.InnerHandler = null;
                    pipelineHandler2.OuterHandler = null;
                    SetHandlerProperties(handler);
                    return;
                }
                pipelineHandler = pipelineHandler2;
            }
            throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, "Cannot find a handler of type {0}", typeFromHandle.Name));
        }