示例#1
0
        public void GenericInterfacesMustBeProxiedByNonGenericMethod()
        {
            var arg1            = new TestArgument2();
            var arg2            = new TestArgument();
            var expectedResult1 = new TestArgument4();
            var expectedResult2 = new TestArgument2();

            var proxyObject = LazyProxyBuilder.CreateInstance(
                typeof(IGenericTestService <TestArgument2, TestArgument, TestArgument4>), () =>
            {
                var mock = new Mock <IGenericTestService <TestArgument2, TestArgument, TestArgument4> >(
                    MockBehavior.Strict);

                mock.Setup(s => s.Method1(arg1, arg2)).Returns(expectedResult1);
                mock.Setup(s => s.Method2(arg1, arg2)).Returns(expectedResult2);

                return(mock.Object);
            });

            var proxy = proxyObject as IGenericTestService <TestArgument2, TestArgument, TestArgument4>;

            Assert.NotNull(proxy);

            var actualResult1 = proxy.Method1(arg1, arg2);
            var actualResult2 = proxy.Method2(arg1, arg2);

            Assert.Equal(expectedResult1, actualResult1);
            Assert.Equal(expectedResult2, actualResult2);
        }
示例#2
0
        public static void Main(string[] args)
        {
            var proxy = LazyProxyBuilder.CreateInstance <IMyService>(() =>
            {
                Console.WriteLine("The real instance creation...");
                return(new MyService());
            });

            Console.WriteLine("Foo execution...");
            proxy.Foo();
        }
示例#3
0
        public static void Main(string[] args)
        {
            var lazyProxy = LazyProxyBuilder.CreateInstance <IMyService>(() =>
            {
                Console.WriteLine("Creating an instance of the real service...");
                return(new MyService());
            });

            Console.WriteLine("Executing the 'Foo' method...");
            lazyProxy.Foo();
        }
        public void BusinessDisposeMethodIsCalled()
        {
            var mock = new Mock <IHaveDisposeBusinessMethod>(MockBehavior.Strict);

            mock.Setup(s => s.Dispose()).Verifiable();

            var proxy = LazyProxyBuilder.CreateInstance(() => mock.Object);

            proxy.Dispose();

            mock.Verify(s => s.Dispose());
        }
        public void RegularDisposeMethodIsNotCalledIfNoInstanceIsCreated()
        {
            var mock        = new Mock <IImplementIDisposable>(MockBehavior.Strict);
            var callCounter = 0;

            mock.Setup(s => s.Dispose()).Callback(() => callCounter++);

            var proxy = LazyProxyBuilder.CreateInstance(() => mock.Object);

            proxy.Dispose();

            Assert.Equal(0, callCounter);
        }
        public void RegularDisposeMethodIsCalledIfInstanceIsCreated()
        {
            var mock = new Mock <IImplementIDisposable>(MockBehavior.Strict);

            mock.Setup(s => s.DoSomething());
            mock.Setup(s => s.Dispose()).Verifiable();

            var proxy = LazyProxyBuilder.CreateInstance(() => mock.Object);

            proxy.DoSomething();
            proxy.Dispose();

            mock.Verify(s => s.Dispose());
        }
示例#7
0
        private static IServiceCollection AddLazy(
            this IServiceCollection collection,
            Type serviceType,
            Func <IServiceProvider, object> implementationFactory,
            ServiceLifetime lifetime)
        {
            Func <IServiceProvider, object> lazyImplementationFactory = (s) => LazyProxyBuilder.CreateInstance(
                serviceType,
                () => implementationFactory(s));

            var descriptor = new ServiceDescriptor(serviceType, lazyImplementationFactory, lifetime);

            collection.Add(descriptor);
            return(collection);
        }
示例#8
0
        public void ExceptionsFromServiceMustBeThrown()
        {
            const bool arg = true;

            var proxy = LazyProxyBuilder.CreateInstance(() =>
            {
                var mock = new Mock <ITestService>(MockBehavior.Strict);

                mock.Setup(s => s.ParentMethod(arg)).Throws <TestException>();

                return(mock.Object);
            });

            Assert.Throws <TestException>(() => proxy.ParentMethod(arg));
        }
示例#9
0
        public void IndexerGettersMustBeProxied()
        {
            const int    arg    = 3;
            const string result = "result";

            var proxy = LazyProxyBuilder.CreateInstance(() =>
            {
                var mock = new Mock <ITestService>(MockBehavior.Strict);

                mock.Setup(s => s[arg]).Returns(result);

                return(mock.Object);
            });

            Assert.Equal(result, proxy[arg]);
        }
示例#10
0
        private static IServiceCollection AddLazy(
            this IServiceCollection collection,
            Type serviceType,
            Type implementationType,
            ServiceLifetime lifetime)
        {
            var factory = ActivatorUtilities.CreateFactory(implementationType, Array.Empty <Type>());

            Func <IServiceProvider, object> lazyImplementationFactory = (s) => LazyProxyBuilder.CreateInstance(
                serviceType,
                () => factory(s, null));

            var descriptor = new ServiceDescriptor(serviceType, lazyImplementationFactory, lifetime);

            collection.Add(descriptor);
            return(collection);
        }
示例#11
0
        public void MethodsWithDefaultValuesMustBeProxied()
        {
            const string defaultArg = "arg";
            const string result     = "result";

            var proxy = LazyProxyBuilder.CreateInstance(() =>
            {
                var mock = new Mock <ITestService>(MockBehavior.Strict);

                mock.Setup(s => s.MethodWithDefaultValue(defaultArg)).Returns(result);

                return(mock.Object);
            });

            var actualResult = proxy.MethodWithDefaultValue();

            Assert.Equal(result, actualResult);
        }
示例#12
0
        public async Task AsyncMethodsMustBeProxied()
        {
            const string arg    = "arg";
            const string result = "result";

            var proxy = LazyProxyBuilder.CreateInstance(() =>
            {
                var mock = new Mock <ITestService>(MockBehavior.Strict);

                mock.Setup(s => s.MethodAsync(arg)).ReturnsAsync(result);

                return(mock.Object);
            });

            var actualResult = await proxy.MethodAsync(arg);

            Assert.Equal(result, actualResult);
        }
示例#13
0
        public void PropertyGettersMustBeProxied()
        {
            var       result1 = new TestArgument();
            const int result2 = 3;

            var proxy = LazyProxyBuilder.CreateInstance(() =>
            {
                var mock = new Mock <ITestService>(MockBehavior.Strict);

                mock.Setup(s => s.Property).Returns(result1);
                mock.Setup(s => s.ParentProperty).Returns(result2);

                return(mock.Object);
            });

            Assert.Equal(result1, proxy.Property);
            Assert.Equal(result2, proxy.ParentProperty);
        }
示例#14
0
        public void IndexerSettersMustBeProxied()
        {
            const int           arg    = 3;
            const string        result = "result";
            Mock <ITestService> mock   = null;

            var proxy = LazyProxyBuilder.CreateInstance(() =>
            {
                mock = new Mock <ITestService>(MockBehavior.Strict);

                mock.SetupSet(s => s[arg] = result);

                return(mock.Object);
            });

            proxy[arg] = result;

            mock.VerifySet(s => s[arg] = result);
        }
示例#15
0
        public void MethodsWithRefValuesMustBeProxied()
        {
            var          refArg         = new TestArgument();
            const string expectedResult = "result";

            var proxy = LazyProxyBuilder.CreateInstance(() =>
            {
                var mock = new Mock <ITestService>(MockBehavior.Strict);

                // ReSharper disable once AccessToModifiedClosure
                mock.Setup(s => s.MethodWithRefValue(ref refArg)).Returns(expectedResult);

                return(mock.Object);
            });

            var actualResult = proxy.MethodWithRefValue(ref refArg);

            Assert.Equal(expectedResult, actualResult);
        }
示例#16
0
        public void ServiceCtorMustBeExecutedAfterMethodIsCalledAndOnlyOnce()
        {
            var constructorCounter = 0;
            var proxy = LazyProxyBuilder.CreateInstance(() =>
            {
                constructorCounter++;
                return(Mock.Of <ITestService>());
            });

            Assert.Equal(0, constructorCounter);

            proxy.VoidMethod();

            Assert.Equal(1, constructorCounter);

            proxy.VoidMethod();

            Assert.Equal(1, constructorCounter);
        }
示例#17
0
        public void GenericMethodsMustBeProxied()
        {
            const string arg            = "arg";
            const string expectedResult = "result";

            var proxy = LazyProxyBuilder.CreateInstance(() =>
            {
                var mock = new Mock <ITestService>(MockBehavior.Strict);

                mock
                .Setup(s => s.GenericMethod <TestArgument2, TestArgument, TestArgument4>(arg))
                .Returns(expectedResult);

                return(mock.Object);
            });

            var actualResult = proxy.GenericMethod <TestArgument2, TestArgument, TestArgument4>(arg);

            Assert.Equal(expectedResult, actualResult);
        }
示例#18
0
        /// <inheritdoc />
        protected override void Initialize()
        {
            var registrationName = Guid.NewGuid().ToString();

            Context.Container.RegisterType(_typeFrom, _typeTo, _name, _getLifetimeManager());
            Context.Container.RegisterType(_typeFrom, _typeTo, registrationName, _getLifetimeManager(), _injectionMembers);

            Context.Policies.Set(_typeFrom, _name, typeof(ResolveDelegateFactory),
                                 (ResolveDelegateFactory)((ref BuilderContext _) =>
                                                          (ref BuilderContext c) =>
            {
                var container = c.Container;
                var type = c.RegistrationType;
                var overrides = c.Overrides;

                return(LazyProxyBuilder.CreateInstance(type,
                                                       () => container.Resolve(type, registrationName, overrides)
                                                       ));
            })
                                 );
        }
示例#19
0
        public void MethodsMustBeProxied()
        {
            const string arg1    = "test";
            const int    arg2    = 7;
            var          arg3    = new TestArgument();
            const bool   arg4    = true;
            const string result1 = "result1";
            const string result2 = "result2";

            var proxy = LazyProxyBuilder.CreateInstance(() =>
            {
                var mock = new Mock <ITestService>(MockBehavior.Strict);

                mock.Setup(s => s.Method(arg1, arg2, arg3)).Returns(result1);
                mock.Setup(s => s.ParentMethod(arg4)).Returns(result2);

                return(mock.Object);
            });

            Assert.Equal(result1, proxy.Method(arg1, arg2, arg3));
            Assert.Equal(result2, proxy.ParentMethod(arg4));
        }
示例#20
0
        public void PropertySettersMustBeProxied()
        {
            var       value1 = new TestArgument();
            const int value2 = 3;

            Mock <ITestService> mock = null;

            var proxy = LazyProxyBuilder.CreateInstance(() =>
            {
                mock = new Mock <ITestService>(MockBehavior.Strict);

                mock.SetupSet(s => s.Property       = value1);
                mock.SetupSet(s => s.ParentProperty = value2);

                return(mock.Object);
            });

            proxy.Property       = value1;
            proxy.ParentProperty = value2;

            mock.VerifySet(s => s.Property       = value1);
            mock.VerifySet(s => s.ParentProperty = value2);
        }
 private static object CreateLazyProxy(
     IComponentContext context, Type type, string name, IEnumerable <Parameter> parameters) =>
 LazyProxyBuilder.CreateInstance(type,
                                 () => context.ResolveNamed(name, type, parameters)
                                 );