Ejemplo n.º 1
0
        public void ShouldResolveConstructorWithMostResolvableParametersFromContainer()
        {
            var mockSampleService       = new Mock <ISampleService>();
            IServiceContainer container = new ServiceContainer();

            // Add an ISampleService instance
            container.AddService(mockSampleService.Object);
            container.AddDefaultServices();
            var resolver = container.GetService <IMemberResolver <ConstructorInfo> >();

            Assert.IsNotNull(resolver);

            // The resolver should return the constructor with two ISampleService parameters
            ConstructorInfo expectedConstructor =
                typeof(SampleClassWithMultipleConstructors).GetConstructor(new[]
            {
                typeof(ISampleService),
                typeof(ISampleService)
            });

            Assert.IsNotNull(expectedConstructor);

            var             finderContext = new MethodFinderContext(new Type[0], new object[0], null);
            ConstructorInfo result        = resolver.ResolveFrom(typeof(SampleClassWithMultipleConstructors), container,
                                                                 finderContext);

            Assert.AreSame(expectedConstructor, result);
        }
Ejemplo n.º 2
0
        public void ShouldResolveConstructorWithAdditionalArgument()
        {
            var mockSampleService       = new Mock <ISampleService>();
            IServiceContainer container = new ServiceContainer();

            // Add an ISampleService instance
            container.AddService(mockSampleService.Object);
            container.AddDefaultServices();

            var resolver = container.GetService <IMemberResolver <ConstructorInfo> >();

            Assert.IsNotNull(resolver);

            // The resolver should return the constructor
            // with the following signature: Constructor(ISampleService, int)
            ConstructorInfo expectedConstructor =
                typeof(SampleClassWithAdditionalArgument).GetConstructor(new[] { typeof(ISampleService), typeof(int) });

            Assert.IsNotNull(expectedConstructor);


            var             context = new MethodFinderContext(42);
            ConstructorInfo result  = resolver.ResolveFrom(typeof(SampleClassWithAdditionalArgument), container, context);

            Assert.AreSame(expectedConstructor, result);
        }
Ejemplo n.º 3
0
        public void ShouldInstantiateObjectWithConstructorAndArguments()
        {
            ConstructorInfo targetConstructor = typeof(SampleClassWithMultipleConstructors).GetConstructor(new[]
            {
                typeof
                (
                    ISampleService
                )
                ,
                typeof
                (
                    ISampleService
                )
            });

            // Create the method arguments
            var mockSampleService = new Mock <ISampleService>();
            var arguments         = new object[] { mockSampleService.Object, mockSampleService.Object };

            // Initialize the container
            IServiceContainer container = new ServiceContainer();

            container.AddDefaultServices();

            var    constructorInvoke = container.GetService <IMethodInvoke <ConstructorInfo> >();
            object result            = constructorInvoke.Invoke(null, targetConstructor, arguments);

            Assert.IsNotNull(result);
            Assert.IsInstanceOfType(typeof(SampleClassWithMultipleConstructors), result);
        }
Ejemplo n.º 4
0
        private static void Test(string serviceName, Action <IGenerateFactory <ISampleService> > usingFactory, Func <IUsingLambda <ISampleService>, IGenerateFactory <ISampleService> > doInject, Func <string, IServiceContainer, bool> verifyResult)
        {
            var container = new ServiceContainer();

            // HACK: Manually inject the required services into the container
            container.AddDefaultServices();

            Inject(serviceName, usingFactory, doInject, container);
            verifyResult(serviceName, container);
        }
Ejemplo n.º 5
0
        public void ShouldResolveClassWithMultipleNonServiceArgumentConstructors()
        {
            IServiceContainer container = new ServiceContainer();

            container.AddDefaultServices();
            container.AddService("ClassWithMultipleNonServiceArgumentConstructors",
                                 typeof(ISampleService), typeof(SampleClassWithMultipleNonServiceArgumentConstructors),
                                 LifecycleType.OncePerRequest);

            // Match the correct constructor
            var sampleService = container.GetService <ISampleService>("ClassWithMultipleNonServiceArgumentConstructors", "test", 42, SampleEnum.One, (decimal)3.14, 42);

            Assert.IsNotNull(sampleService);
        }
Ejemplo n.º 6
0
        public void ShouldInstantiateClassWithNonServiceArguments()
        {
            var container = new ServiceContainer();

            container.AddDefaultServices();

            container.AddService(typeof(SampleClassWithNonServiceArgument), typeof(SampleClassWithNonServiceArgument));

            string text        = "Hello, World!";
            string serviceName = null;
            var    result      = container.GetService <SampleClassWithNonServiceArgument>(serviceName, text);

            Assert.IsNotNull(result);
            Assert.IsTrue(result.Value == text);
        }
Ejemplo n.º 7
0
        public void MethodInvokerShouldProperlyHandleReturnValues()
        {
            MethodInfo targetMethod = typeof(object).GetMethod("GetHashCode");
            var        instance     = new object();

            int hash = instance.GetHashCode();

            container.AddDefaultServices();

            var invoker = container.GetService <IMethodInvoke <MethodInfo> >();

            Assert.IsNotNull(invoker);

            object result = invoker.Invoke(instance, targetMethod, new object[] {});

            Assert.AreEqual(result, hash);
        }