Example #1
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));
        }
Example #2
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));
        }
Example #3
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'."));
        }
Example #4
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));
        }
Example #5
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 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));
        }