public void GetInstance_DecoratedCompound_DecoratesCompound_DoesNotDecorateImplementations()
        {
            var implementation1           = new ServiceImplementationInfo(typeof(TestImplementation1), LifetimeKind.InstancePerDependency, RegistrationType.Multiple);
            var implementation2           = new ServiceImplementationInfo(typeof(TestImplementation2), LifetimeKind.InstancePerDependency, RegistrationType.Multiple);
            var decorator                 = new ServiceImplementationInfo(typeof(TestDecorator1), LifetimeKind.InstancePerDependency, RegistrationType.Decorator);
            var compound                  = new ServiceImplementationInfo(typeof(TestCompound), LifetimeKind.InstancePerDependency, RegistrationType.Compound);
            var serviceConfigurationEntry = new ServiceConfigurationEntry(typeof(ITestType), implementation1, implementation2, decorator, compound);

            var serviceLocator = CreateServiceLocator();

            serviceLocator.Register(serviceConfigurationEntry);

            var instance = serviceLocator.GetInstance(typeof(ITestType));

            Assert.That(instance, Is.TypeOf <TestDecorator1>());
            var decoratorInstance = (TestDecorator1)instance;

            Assert.That(decoratorInstance.DecoratedObject, Is.TypeOf <TestCompound>());
            var compoundInstance = (TestCompound)decoratorInstance.DecoratedObject;

            Assert.That(compoundInstance.InnerObjects, Is.Not.Empty);
            Assert.That(
                compoundInstance.InnerObjects.Select(i => i.GetType()),
                Is.EqualTo(new[] { typeof(TestImplementation1), typeof(TestImplementation2) }));
        }
Пример #2
0
        public void ToString_DebugInfo()
        {
            var implementation0 = new ServiceImplementationInfo(typeof(object), LifetimeKind.Singleton, RegistrationType.Compound);
            var implementation1 = new ServiceImplementationInfo(typeof(string), LifetimeKind.InstancePerDependency, RegistrationType.Multiple);

            Assert.That(implementation0.ToString(), Is.EqualTo("{System.Object, Singleton, Compound}"));
            Assert.That(implementation1.ToString(), Is.EqualTo("{System.String, InstancePerDependency, Multiple}"));
        }
        public void Initialize_WithSingleInfo()
        {
            var implementationInfo        = new ServiceImplementationInfo(typeof(TestConcreteImplementationAttributeType), LifetimeKind.Singleton);
            var serviceConfigurationEntry = new ServiceConfigurationEntry(typeof(ITestSingletonConcreteImplementationAttributeType), implementationInfo);

            Assert.That(serviceConfigurationEntry.ServiceType, Is.EqualTo(typeof(ITestSingletonConcreteImplementationAttributeType)));
            Assert.That(serviceConfigurationEntry.ImplementationInfos, Is.EqualTo(new[] { implementationInfo }));
        }
Пример #4
0
        public void CreateMultiple_WithLifetime()
        {
            Func <ServiceImplementationInfoTest> factory = () => new ServiceImplementationInfoTest();
            var implementationInfo = ServiceImplementationInfo.CreateSingle(factory, LifetimeKind.Singleton);

            Assert.That(implementationInfo.Factory, Is.Not.Null);
            Assert.That(implementationInfo.ImplementationType, Is.SameAs(typeof(ServiceImplementationInfoTest)));
            Assert.That(implementationInfo.Lifetime, Is.EqualTo(LifetimeKind.Singleton));
        }
Пример #5
0
        public void CreateMultiple_RetrievesImplementationType()
        {
            Func <ServiceImplementationInfoTest> factory = () => new ServiceImplementationInfoTest();
            var implementationInfo = ServiceImplementationInfo.CreateSingle(factory);

            Assert.That(implementationInfo.Factory, Is.Not.Null);
            Assert.That(implementationInfo.ImplementationType, Is.SameAs(typeof(ServiceImplementationInfoTest)));
            Assert.That(implementationInfo.Lifetime, Is.EqualTo(LifetimeKind.InstancePerDependency));
        }
        public void Initialize_IncompatibleType()
        {
            var implementationInfo = new ServiceImplementationInfo(typeof(object), LifetimeKind.Singleton);

            Assert.That(
                () => new ServiceConfigurationEntry(typeof(ITestSingletonConcreteImplementationAttributeType), implementationInfo),
                Throws.ArgumentException.With.Message.EqualTo(
                    "The implementation type 'System.Object' does not implement the service type.\r\nParameter name: implementationInfos"));
        }
Пример #7
0
        protected ServiceConfigurationEntry CreateSingleServiceConfigurationEntry(
            Type serviceType,
            Type implementationType,
            LifetimeKind lifetimeKind = LifetimeKind.InstancePerDependency)
        {
            var implementation = new ServiceImplementationInfo(implementationType, lifetimeKind, RegistrationType.Single);

            return(new ServiceConfigurationEntry(serviceType, implementation));
        }
        public void Initialize_WithEnumerable()
        {
            var info1 = new ServiceImplementationInfo(typeof(TestConcreteImplementationAttributeType), LifetimeKind.Singleton);
            var info2 = new ServiceImplementationInfo(typeof(TestConcreteImplementationAttributeType), LifetimeKind.Singleton);

            var entry = new ServiceConfigurationEntry(typeof(ITestSingletonConcreteImplementationAttributeType), new[] { info1, info2 });

            Assert.That(entry.ServiceType, Is.EqualTo(typeof(ITestSingletonConcreteImplementationAttributeType)));
            Assert.That(entry.ImplementationInfos, Is.EqualTo(new[] { info1, info2 }));
        }
Пример #9
0
        public void Setup()
        {
            codecContainer = new CodecContainer();
            factory        = new ServiceMethodHandlerFactory(codecContainer);
            service        = Substitute.For <IGlobalService>();
            var serviceDescriptionBuilder = new ServiceDescriptionBuilder(new MethodDescriptionBuilder());
            var globalServiceDescription  = serviceDescriptionBuilder.Build(typeof(IGlobalService));

            globalServiceImplementationInfo =
                new ServiceImplementationInfo(globalServiceDescription, service);
        }
        public void Trivial()
        {
            var implementationInfo = new ServiceImplementationInfo();
            var path    = new ServicePath("MyService", "MyMethod");
            var handler = (ServiceMethodHandler)((i, d) => new byte[0]);

            factory.CreateMethodHandler(implementationInfo, path).Returns(handler);

            var handler1 = container.GetMethodHandler(implementationInfo, path);
            var handler2 = container.GetMethodHandler(implementationInfo, path);

            Assert.That(handler1, Is.EqualTo(handler2));
        }
Пример #11
0
        public void Register_SingleAndMultipleImplementations_ThrowsInvalidOperationException()
        {
            var single   = new ServiceImplementationInfo(typeof(TestImplementation1), LifetimeKind.InstancePerDependency, RegistrationType.Single);
            var multiple = new ServiceImplementationInfo(typeof(TestImplementation2), LifetimeKind.InstancePerDependency, RegistrationType.Multiple);
            var serviceConfigurationEntry = new ServiceConfigurationEntry(typeof(ITestType), single, multiple);

            var serviceLocator = CreateServiceLocator();

            Assert.That(
                () => serviceLocator.Register(serviceConfigurationEntry),
                Throws.InvalidOperationException.With.Message.EqualTo(
                    "Service type 'Remotion.UnitTests.ServiceLocation.DefaultServiceLocatorTests.TestDomain.ITestType': "
                    + "Registrations of type 'Single' and 'Multiple' are mutually exclusive."));
        }
Пример #12
0
        public void Register_SingleWithMultipleRegistrations_ThrowsInvalidOperationException()
        {
            var implementation1           = new ServiceImplementationInfo(typeof(TestImplementation1), LifetimeKind.InstancePerDependency, RegistrationType.Single);
            var implementation2           = new ServiceImplementationInfo(typeof(TestImplementation2), LifetimeKind.InstancePerDependency, RegistrationType.Single);
            var serviceConfigurationEntry = new ServiceConfigurationEntry(typeof(ITestType), implementation1, implementation2);

            var serviceLocator = CreateServiceLocator();

            Assert.That(
                () => serviceLocator.Register(serviceConfigurationEntry),
                Throws.InvalidOperationException.With.Message.EqualTo(
                    "Cannot register multiple implementations with registration type 'Single' "
                    + "for service type 'Remotion.UnitTests.ServiceLocation.DefaultServiceLocatorTests.TestDomain.ITestType'."));
        }
Пример #13
0
        public void GetInstance_ServiceTypeWithFactoryReturningNull_ThrowsActivationException()
        {
            var implementation            = ServiceImplementationInfo.CreateSingle <ITestTypeWithErrors> (() => null);
            var serviceConfigurationEntry = new ServiceConfigurationEntry(typeof(ITestTypeWithErrors), implementation);

            var serviceLocator = CreateServiceLocator();

            serviceLocator.Register(serviceConfigurationEntry);

            Assert.That(
                () => serviceLocator.GetInstance(typeof(ITestTypeWithErrors)),
                Throws.TypeOf <ActivationException> ().With.Message.EqualTo(
                    "The registered factory returned null instead of an instance implementing the requested service type "
                    + "'Remotion.UnitTests.ServiceLocation.DefaultServiceLocatorTests.TestDomain.ITestTypeWithErrors'."));
        }
Пример #14
0
        public void GetInstance_CompoundIsSingleton_ImplementationIsInstancePerDependency_ImplementationsBehaveAsSingleton()
        {
            var compound                  = new ServiceImplementationInfo(typeof(TestCompound), LifetimeKind.Singleton, RegistrationType.Compound);
            var implementation            = new ServiceImplementationInfo(typeof(TestImplementation1), LifetimeKind.InstancePerDependency, RegistrationType.Multiple);
            var serviceConfigurationEntry = new ServiceConfigurationEntry(typeof(ITestType), compound, implementation);

            var serviceLocator = CreateServiceLocator();

            serviceLocator.Register(serviceConfigurationEntry);

            var instance1 = serviceLocator.GetInstance(typeof(ITestType));
            var instance2 = serviceLocator.GetInstance(typeof(ITestType));

            Assert.That(instance1, Is.SameAs(instance2));
            Assert.That(((TestCompound)instance1).InnerObjects, Is.EqualTo(((TestCompound)instance2).InnerObjects));
        }
        public void GetInstance_DecoratorIsInstance_ImplementationIsSingleton_DecoratorBehaveAsSingleton()
        {
            var implementation            = new ServiceImplementationInfo(typeof(TestImplementation1), LifetimeKind.Singleton, RegistrationType.Single);
            var decorator                 = new ServiceImplementationInfo(typeof(TestDecorator1), LifetimeKind.InstancePerDependency, RegistrationType.Decorator);
            var serviceConfigurationEntry = new ServiceConfigurationEntry(typeof(ITestType), implementation, decorator);

            var serviceLocator = CreateServiceLocator();

            serviceLocator.Register(serviceConfigurationEntry);

            var instance1 = serviceLocator.GetInstance(typeof(ITestType));
            var instance2 = serviceLocator.GetInstance(typeof(ITestType));

            Assert.That(instance1, Is.SameAs(instance2));
            Assert.That(((TestDecorator1)instance1).DecoratedObject, Is.SameAs(((TestDecorator1)instance2).DecoratedObject));
        }
        public void GetAllInstances_WithMixedLifetimeKind_ReturnsSingletonAsSingletonAndInstanceAsInstance()
        {
            var implementation1           = new ServiceImplementationInfo(typeof(TestImplementation1), LifetimeKind.Singleton, RegistrationType.Multiple);
            var implementation2           = new ServiceImplementationInfo(typeof(TestImplementation2), LifetimeKind.InstancePerDependency, RegistrationType.Multiple);
            var serviceConfigurationEntry = new ServiceConfigurationEntry(typeof(ITestType), implementation1, implementation2);

            var serviceLocator = CreateServiceLocator();

            serviceLocator.Register(serviceConfigurationEntry);

            var instances1 = serviceLocator.GetAllInstances(typeof(ITestType)).ToArray();
            var instances2 = serviceLocator.GetAllInstances(typeof(ITestType)).ToArray();

            Assert.That(instances1, Is.Not.SameAs(instances2));
            Assert.That(instances1[0], Is.SameAs(instances2[0]));
            Assert.That(instances1[1], Is.Not.SameAs(instances2[1]));
        }
Пример #17
0
        public void GetInstance_ImplementationIsRegisteredAsFactoryWithSingletonLifetime_AndSingletonIsLazyInitialized()
        {
            TestImplementation1 expectedInstance = null;
            var serviceImplementation            = ServiceImplementationInfo.CreateSingle(() => expectedInstance = new TestImplementation1(), LifetimeKind.Singleton);
            var serviceConfigurationEntry        = new ServiceConfigurationEntry(typeof(ITestType), serviceImplementation);

            var serviceLocator = CreateServiceLocator();

            serviceLocator.Register(serviceConfigurationEntry);

            Assert.That(expectedInstance, Is.Null);
            var instance1 = serviceLocator.GetInstance(typeof(ITestType));

            Assert.That(expectedInstance, Is.Not.Null);

            var instance2 = serviceLocator.GetInstance(typeof(ITestType));

            Assert.That(instance1, Is.SameAs(instance2));
        }
        public void GetInstance_DecoratorIsSingleton_ImplementationIsInstancePerDependency_DecoratorBehaveAsInstance()
        {
            // Integration test to ensure behavior even if ctor-check in ServiceImplementationInfo is removed.
            var implementation = new ServiceImplementationInfo(typeof(TestImplementation1), LifetimeKind.InstancePerDependency, RegistrationType.Single);
            var decorator      = new ServiceImplementationInfo(typeof(TestDecorator1), LifetimeKind.InstancePerDependency, RegistrationType.Decorator);

            PrivateInvoke.SetNonPublicField(decorator, "_lifetime", LifetimeKind.Singleton);
            var serviceConfigurationEntry = new ServiceConfigurationEntry(typeof(ITestType), implementation, decorator);

            var serviceLocator = CreateServiceLocator();

            serviceLocator.Register(serviceConfigurationEntry);

            var instance1 = serviceLocator.GetInstance(typeof(ITestType));
            var instance2 = serviceLocator.GetInstance(typeof(ITestType));

            Assert.That(instance1, Is.Not.SameAs(instance2));
            Assert.That(((TestDecorator1)instance1).DecoratedObject, Is.Not.SameAs(((TestDecorator1)instance2).DecoratedObject));
        }
Пример #19
0
        public void Register_CompoundWithMultipleRegistrations_ThrowsInvalidOperationException()
        {
            var compound1 = new ServiceImplementationInfo(
                typeof(TestCompoundWithMultipleRegistrations1),
                LifetimeKind.InstancePerDependency,
                RegistrationType.Compound);
            var compound2 = new ServiceImplementationInfo(
                typeof(TestCompoundWithMultipleRegistrations2),
                LifetimeKind.InstancePerDependency,
                RegistrationType.Compound);
            var serviceConfigurationEntry = new ServiceConfigurationEntry(typeof(ITestCompoundWithErrors), compound1, compound2);

            var serviceLocator = CreateServiceLocator();

            Assert.That(
                () => serviceLocator.Register(serviceConfigurationEntry),
                Throws.InvalidOperationException.With.Message.EqualTo(
                    "Cannot register multiple implementations with registration type 'Compound' "
                    + "for service type 'Remotion.UnitTests.ServiceLocation.DefaultServiceLocatorTests.TestDomain.ITestCompoundWithErrors'."));
        }
Пример #20
0
        public void GetInstance_WithFactoryReturningWrongType_ThrowsActivationException()
        {
            Func <ITestType> factory         = () => new TestImplementation1();
            Func <object>    factoryAsObject = factory;
            var implementation = ServiceImplementationInfo.CreateSingle <ITestTypeWithErrors> (() => null);

            PrivateInvoke.SetNonPublicField(implementation, "_factory", factoryAsObject);
            var serviceConfigurationEntry = new ServiceConfigurationEntry(typeof(ITestTypeWithErrors), implementation);

            var serviceLocator = CreateServiceLocator();

            serviceLocator.Register(serviceConfigurationEntry);

            Assert.That(
                () => serviceLocator.GetInstance(typeof(ITestTypeWithErrors)),
                Throws.TypeOf <ActivationException>().With.Message.EqualTo(
                    "The instance returned by the registered factory does not implement the requested type "
                    + "'Remotion.UnitTests.ServiceLocation.DefaultServiceLocatorTests.TestDomain.ITestTypeWithErrors'. "
                    + "(Instance type: 'Remotion.UnitTests.ServiceLocation.DefaultServiceLocatorTests.TestDomain.TestImplementation1'.)"));
        }
        public void GetAllInstances_ImplementationIsRegisteredAsFactoryWithInstanceLifetime()
        {
            TestImplementation1 expectedInstance = null;
            var serviceImplementation            = ServiceImplementationInfo.CreateMultiple(
                () => expectedInstance = new TestImplementation1(),
                LifetimeKind.InstancePerDependency);
            var serviceConfigurationEntry = new ServiceConfigurationEntry(typeof(ITestType), serviceImplementation);

            var serviceLocator = CreateServiceLocator();

            serviceLocator.Register(serviceConfigurationEntry);

            var instance1 = serviceLocator.GetAllInstances(typeof(ITestType)).SingleOrDefault();

            Assert.That(expectedInstance, Is.Not.Null);

            var instance2 = serviceLocator.GetAllInstances(typeof(ITestType)).SingleOrDefault();

            Assert.That(instance1, Is.Not.SameAs(instance2));
        }
        public void GetInstance_ImplementationIsRegisteredAsFactoryWithSingletonLifetime_DecoratedFactoryIsUsed()
        {
            TestImplementation1 expectedInstance = null;
            var implementation            = ServiceImplementationInfo.CreateSingle(() => expectedInstance = new TestImplementation1(), LifetimeKind.Singleton);
            var decorator                 = new ServiceImplementationInfo(typeof(TestDecorator1), LifetimeKind.InstancePerDependency, RegistrationType.Decorator);
            var serviceConfigurationEntry = new ServiceConfigurationEntry(typeof(ITestType), implementation, decorator);

            var serviceLocator = CreateServiceLocator();

            serviceLocator.Register(serviceConfigurationEntry);

            var instance1 = serviceLocator.GetInstance(typeof(ITestType));

            Assert.That(expectedInstance, Is.Not.Null);
            Assert.That(((TestDecorator1)instance1).DecoratedObject, Is.SameAs(expectedInstance));

            var instance2 = serviceLocator.GetInstance(typeof(ITestType));

            Assert.That(instance1, Is.SameAs(instance2));

            Assert.That(((TestDecorator1)instance1).DecoratedObject, Is.SameAs(((TestDecorator1)instance2).DecoratedObject));
        }
        public void GetAllInstances_DecoratesImplementations()
        {
            var implementation1           = new ServiceImplementationInfo(typeof(TestImplementation1), LifetimeKind.InstancePerDependency, RegistrationType.Multiple);
            var implementation2           = new ServiceImplementationInfo(typeof(TestImplementation2), LifetimeKind.InstancePerDependency, RegistrationType.Multiple);
            var decorator                 = new ServiceImplementationInfo(typeof(TestDecorator1), LifetimeKind.InstancePerDependency, RegistrationType.Decorator);
            var serviceConfigurationEntry = new ServiceConfigurationEntry(typeof(ITestType), implementation1, implementation2, decorator);

            var serviceLocator = CreateServiceLocator();

            serviceLocator.Register(serviceConfigurationEntry);

            var instances = serviceLocator.GetAllInstances(typeof(ITestType)).ToArray();

            Assert.That(instances, Has.All.TypeOf <TestDecorator1>());

            var decoratorInstance1 = (TestDecorator1)instances[0];

            Assert.That(decoratorInstance1.DecoratedObject, Is.TypeOf <TestImplementation1>());

            var decoratorInstance2 = (TestDecorator1)instances[1];

            Assert.That(decoratorInstance2.DecoratedObject, Is.TypeOf <TestImplementation2>());
        }