Пример #1
0
        public void ExpressionTarget_ResolvingByHelperShouldGoThroughActiveScope()
        {
            var targets = CreateTargetContainer();

            targets.RegisterType <Disposable>();
            //note - this expression is ultimately identical to using 'RegisterType'
            targets.RegisterExpression(() => new RequiresDisposable(ExpressionFunctions.Resolve <Disposable>()));

            var container = CreateContainer(targets);

            RequiresDisposable outerResult = null;
            RequiresDisposable innerResult = null;

            using (var scope = container.CreateScope())
            {
                outerResult = scope.Resolve <RequiresDisposable>();
                using (var childScope = scope.CreateScope())
                {
                    innerResult = childScope.Resolve <RequiresDisposable>();
                }
                Assert.Equal(1, innerResult.DisposedCount);
                Assert.True(innerResult.Disposable.Disposed);
            }
            Assert.Equal(1, innerResult.DisposedCount);
            Assert.True(innerResult.Disposable.Disposed);

            Assert.Equal(1, outerResult.DisposedCount);
            Assert.True(outerResult.Disposable.Disposed);
        }
Пример #2
0
        public void ExpressionTarget_LambdaShouldResolveViaHelperAndReturnAMethodCall()
        {
            //same as above, except not using the generic version
            var targets = CreateTargetContainer();

            targets.RegisterType <ServiceWithFunctions>();
            targets.RegisterExpression(() => ((ServiceWithFunctions)ExpressionFunctions.Resolve(typeof(ServiceWithFunctions))).GetChild(5));

            var container = CreateContainer(targets);

            Assert.Equal(new ServiceWithFunctions().GetChild(5).Output, container.Resolve <ServiceChild>().Output);
        }
        public void ShouldLocateViaHelperInParameterlessLambda()
        {
            // <example6>
            var container = new Container();

            container.RegisterType <MyService, IMyService>();
            container.RegisterExpression(() =>
                                         new RequiresMyService(ExpressionFunctions.Resolve <IMyService>())
                                         );
            var result = container.Resolve <RequiresMyService>();

            Assert.NotNull(result.Service);
            // </example6>
        }
Пример #4
0
        public void ExpressionTarget_ExpressionShouldResolveViaHelper()
        {
            //testing that the ExpressionFunctions static works in a non-lambda
            //to do this we just use the body of a lambda
            var targets = CreateTargetContainer();

            targets.RegisterType <ServiceWithFunctions>();
            Expression <Func <ServiceChild> > f = () => ((ServiceWithFunctions)ExpressionFunctions.Resolve(typeof(ServiceWithFunctions))).GetChild(9);

            targets.RegisterExpression(f.Body, typeof(ServiceChild));

            var container = CreateContainer(targets);

            Assert.Equal(new ServiceWithFunctions().GetChild(9).Output, container.Resolve <ServiceChild>().Output);
        }
Пример #5
0
        public void ExpressionTarget_LambdaShouldResolveViaGenericHelperAndReturnAMethodCall()
        {
            //When you want to explicitly resolve something in an expression, but don't want to or can't
            //use a lambda argument, you can also use the Function.Resolve helper method.  It does nothing
            //at run time (except throw an exception) but it instructs the target adapter to replace that
            //call with a ResolvedTarget, which is more efficient and flexible than simply emitting a
            //runtime call to the container's Resolve method.
            var targets = CreateTargetContainer();

            targets.RegisterType <ServiceWithFunctions>();
            targets.RegisterExpression(() => ExpressionFunctions.Resolve <ServiceWithFunctions>().GetChild(5));

            var container = CreateContainer(targets);

            Assert.Equal(new ServiceWithFunctions().GetChild(5).Output, container.Resolve <ServiceChild>().Output);
        }