コード例 #1
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));
        }
コード例 #2
0
        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));
        }