Пример #1
0
        public IInstanceFactory Handle(IResolverRequest request, Func<IResolverRequest, IInstanceFactory> nextHandler)
        {
            if (request == null) throw new ArgumentNullException("request");
            if (nextHandler == null) throw new ArgumentNullException("nextHandler");

            // If the component handler is last in the pipeline, this will return null.
            // However, it might not *always* be the last handler, so stick to the pattern
            // and pass to the next handler rather than returning null explicitly.
            if (request.RequestType.IsAbstract) return nextHandler(request);

            var ctors = request.RequestType
                .GetConstructors(BindingFlags.Public | BindingFlags.DeclaredOnly | BindingFlags.Instance);

            // this bad boy does the actual resolving...
            var ctorResolver = new ConstructorResolver(_pipeline);

            // ... we just order the results
            var factory = ctors
                .Select(ctor => ctorResolver.TryResolve(request, ctor))
                .Where(f => f != null) // remove unresolvable ctors
                .OrderByDescending(f => f.Resolvable) // order by most parameters
                .OrderBy(f => f.Optional) // with the least optional parameters
                .FirstOrDefault();

            return factory;
        }