예제 #1
0
        /// <summary>
        /// Get an invoker for an activator's pipeline.
        /// </summary>
        /// <param name="activator">The activator.</param>
        /// <param name="registry">The applicable component registry.</param>
        /// <returns>A func to call that invokes the pipeline.</returns>
        public static Func <ILifetimeScope, IEnumerable <Parameter>, T> GetPipelineInvoker <T>(this IInstanceActivator activator, IComponentRegistry registry)
        {
            var services        = new RegistryServices(registry);
            var pipelineBuilder = new ResolvePipelineBuilder(PipelineType.Registration);

            activator.ConfigurePipeline(services, pipelineBuilder);

            var built = pipelineBuilder.Build();

            return((scope, parameters) =>
            {
                // To get the sharing scope from what might be a container, we're going to resolve the lifetime scope.
                var lifetimeScope = scope.Resolve <ILifetimeScope>() as LifetimeScope;

                var request = new DefaultResolveRequestContext(
                    new ResolveOperation(lifetimeScope, lifetimeScope.DiagnosticSource),
                    new ResolveRequest(new TypedService(typeof(T)), Mocks.GetResolvableImplementation(), parameters),
                    lifetimeScope,
                    lifetimeScope.DiagnosticSource);

                built.Invoke(request);

                return (T)request.Instance;
            });
        }
예제 #2
0
        public void CanAddMiddlewareInReversePhaseOrder()
        {
            var pipelineBuilder = new ResolvePipelineBuilder(PipelineType.Service);

            var order = new List <string>();

            pipelineBuilder.Use("3", PipelinePhase.Sharing, (ctxt, next) =>
            {
                order.Add("3");
                next(ctxt);
            });
            pipelineBuilder.Use("2", PipelinePhase.ScopeSelection, (ctxt, next) =>
            {
                order.Add("2");
                next(ctxt);
            });
            pipelineBuilder.Use("1", PipelinePhase.ResolveRequestStart, (ctxt, next) =>
            {
                order.Add("1");
                next(ctxt);
            });

            var built = pipelineBuilder.Build();

            built.Invoke(new PipelineRequestContextStub());

            Assert.Collection(
                order,
                el => Assert.Equal("1", el.ToString()),
                el => Assert.Equal("2", el.ToString()),
                el => Assert.Equal("3", el.ToString()));
        }
예제 #3
0
        public void CanControlPhaseAddPriorityWithPrecedingPhase()
        {
            var pipelineBuilder = new ResolvePipelineBuilder(PipelineType.Service);

            var order = new List <string>();

            pipelineBuilder.Use("3", PipelinePhase.ScopeSelection, (ctxt, next) =>
            {
                order.Add("3");
                next(ctxt);
            });
            pipelineBuilder.Use("1", PipelinePhase.ResolveRequestStart, (ctxt, next) =>
            {
                order.Add("1");
                next(ctxt);
            });
            pipelineBuilder.Use("2", PipelinePhase.ScopeSelection, MiddlewareInsertionMode.StartOfPhase, (ctxt, next) =>
            {
                order.Add("2");
                next(ctxt);
            });

            var built = pipelineBuilder.Build();

            built.Invoke(new PipelineRequestContextStub());

            Assert.Collection(
                order,
                el => Assert.Equal("1", el.ToString()),
                el => Assert.Equal("2", el.ToString()),
                el => Assert.Equal("3", el.ToString()));
        }
예제 #4
0
        public void CanAddMultipleMiddlewareToPipelineWithExistingMiddleware()
        {
            var pipelineBuilder = new ResolvePipelineBuilder(PipelineType.Service);
            var order           = new List <string>();

            pipelineBuilder.UseRange(new[]
            {
                new DelegateMiddleware("1", PipelinePhase.ResolveRequestStart, (ctxt, next) =>
                {
                    order.Add("1");
                    next(ctxt);
                }),
                new DelegateMiddleware("3", PipelinePhase.ScopeSelection, (ctxt, next) =>
                {
                    order.Add("3");
                    next(ctxt);
                }),
                new DelegateMiddleware("5", PipelinePhase.ServicePipelineEnd, (ctxt, next) =>
                {
                    order.Add("5");
                    next(ctxt);
                })
            });

            pipelineBuilder.UseRange(new[]
            {
                new DelegateMiddleware("2", PipelinePhase.ResolveRequestStart, (ctxt, next) =>
                {
                    order.Add("2");
                    next(ctxt);
                }),
                new DelegateMiddleware("4", PipelinePhase.ScopeSelection, (ctxt, next) =>
                {
                    order.Add("4");
                    next(ctxt);
                }),
                new DelegateMiddleware("6", PipelinePhase.ServicePipelineEnd, (ctxt, next) =>
                {
                    order.Add("6");
                    next(ctxt);
                })
            });

            var built = pipelineBuilder.Build();

            built.Invoke(new PipelineRequestContextStub());

            Assert.Collection(
                order,
                el => Assert.Equal("1", el.ToString()),
                el => Assert.Equal("2", el.ToString()),
                el => Assert.Equal("3", el.ToString()),
                el => Assert.Equal("4", el.ToString()),
                el => Assert.Equal("5", el.ToString()),
                el => Assert.Equal("6", el.ToString()));
        }
예제 #5
0
        public void CanHaveSingleStage()
        {
            var pipelineBuilder = new ResolvePipelineBuilder(PipelineType.Service);
            var order           = new List <string>();

            pipelineBuilder.Use(PipelinePhase.ResolveRequestStart, (ctxt, next) =>
            {
                order.Add("1");
                next(ctxt);
            });

            var built = pipelineBuilder.Build();

            built.Invoke(new PipelineRequestContextStub());

            Assert.Collection(
                order,
                e => Assert.Equal("1", e));
        }