public void ConfigurableContainerIsUsed()
        {
            // --- Arrange
            var registry = new OverridableSericeRegistry();
            var settings = new ServiceContainerSettings(
                "config",
                null,
                new List<MappingSettings>
                    {
                        new MappingSettings(typeof (ISampleService2), typeof (SampleService2)),
                        new MappingSettings(typeof (ISampleService3), typeof (SampleService3A))
                    });
            registry.GetConfigurableContainer().ConfigureFrom(settings);

            // --- Act
            var service1 = registry.GetService<ISampleService1>();
            var service2 = registry.GetService<ISampleService2>();
            var service3 = registry.GetService<ISampleService3>();

            // --- Assert
            registry.GetPresetContainer().GetRegisteredServices().ShouldHaveCountOf(2);
            registry.GetConfigurableContainer().GetRegisteredServices().ShouldHaveCountOf(2);
            service1.ShouldBeOfType(typeof(SampleService1));
            service2.ShouldBeOfType(typeof(SampleService2));
            service3.ShouldBeOfType(typeof(SampleService3A));
        }
        public void LifetimeManagerResolutionWorksAsExpected()
        {
            // --- Arrange
            const string SOURCE = @"<Temp name='container' parent='parent'>
                             <Map from='Seemplest.Core.UnitTests.DependencyInjection.ISampleRepository, Seemplest.Core.UnitTests, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'
                                to='Seemplest.Core.UnitTests.DependencyInjection.SampleRepository, Seemplest.Core.UnitTests, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'
                                lifetime='PerCall'>
                             </Map>
                             <Map from='Seemplest.Core.UnitTests.DependencyInjection.ISampleRepository, Seemplest.Core.UnitTests, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'
                                to='Seemplest.Core.UnitTests.DependencyInjection.SampleRepository, Seemplest.Core.UnitTests, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'
                                lifetime='PerThread'>
                             </Map>
                             <Map from='Seemplest.Core.UnitTests.DependencyInjection.ISampleRepository, Seemplest.Core.UnitTests, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'
                                to='Seemplest.Core.UnitTests.DependencyInjection.SampleRepository, Seemplest.Core.UnitTests, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'
                                lifetime='Singleton'>
                             </Map>
                           </Temp>";
            var element = XElement.Parse(SOURCE);

            // --- Act
            var container1 = new ServiceContainerSettings(element);
            var container2 = new ServiceContainerSettings(element, TypeResolver.Current);

            // --- Assert
            container1.Mappings.ShouldHaveCountOf(3);
            container1.Mappings[0].Lifetime.ShouldEqual(typeof (PerCallLifetimeManager));
            container1.Mappings[1].Lifetime.ShouldEqual(typeof (PerThreadLifetimeManager));
            container1.Mappings[2].Lifetime.ShouldEqual(typeof (SingletonLifetimeManager));

            container2.Mappings.ShouldHaveCountOf(3);
            container2.Mappings[0].Lifetime.ShouldEqual(typeof(PerCallLifetimeManager));
            container2.Mappings[1].Lifetime.ShouldEqual(typeof(PerThreadLifetimeManager));
            container2.Mappings[2].Lifetime.ShouldEqual(typeof(SingletonLifetimeManager));
        }
 /// <summary>
 /// Configures the container from the specified settings
 /// </summary>
 /// <param name="settings">Service container settings</param>
 public void ConfigureFrom(ServiceContainerSettings settings)
 {
     _services.Clear();
     foreach (var mapping in settings.Mappings)
     {
         var ltType    = mapping.Lifetime ?? typeof(PerCallLifetimeManager);
         var ltManager = (ILifetimeManager)Activator.CreateInstance(ltType);
         Register(mapping.From, mapping.To, mapping.Parameters, mapping.Properties,
                  ltManager);
     }
 }
        /// <summary>
        /// Registers the specified container chain including parents
        /// </summary>
        /// <param name="containers">Collection of all containers to be registered</param>
        /// <param name="container"></param>
        /// <param name="visitedContainers"></param>
        // ReSharper disable ParameterTypeCanBeEnumerable.Local
        private void RegisterContainer(List <ServiceContainerSettings> containers, ServiceContainerSettings container,
                                       // ReSharper restore ParameterTypeCanBeEnumerable.Local
                                       ICollection <string> visitedContainers)
        {
            // --- Go back, if the container has already been registered.
            var name = container.Name;

            if (Containers.ContainsKey(name))
            {
                return;
            }

            // --- Observe circular container reference
            if (visitedContainers.Contains(name))
            {
                throw new CircularContainerReferenceException(name);
            }

            // --- Obtain parent container, if there is any
            var parentName = container.ParentName;
            IServiceContainer parentContainer = null;

            if (parentName != null)
            {
                // --- First register the parent container
                visitedContainers.Add(name);
                var parentSettings = containers.FirstOrDefault(c => c.Name == parentName);
                if (parentSettings == null)
                {
                    throw new ContainerNotFoundException(parentName);
                }
                RegisterContainer(containers, parentSettings, visitedContainers);
                parentContainer = Containers[parentName];
            }

            // --- Prepare the container and initialize its mappings
            var newContainer = new ServiceContainer(name, parentContainer);

            newContainer.ConfigureFrom(container);
            Containers.Add(name, newContainer);
        }
        public void ReadAndWriteWorksAsExpected()
        {
            // --- Arrange
            var settings = new ServiceContainerSettings("container", "parent",
                new List<MappingSettings>
                    {
                        new MappingSettings(typeof(ISampleService), typeof(SampleService)),
                        new MappingSettings(typeof(ISampleRepository), typeof(SampleRepository),
                            typeof(SingletonLifetimeManager),
                            new InjectedParameterSettingsCollection(new List<InjectedParameterSettings>
                                {
                                    new InjectedParameterSettings(typeof(int), "12"),
                                    new InjectedParameterSettings(typeof(string), "Hello")
                                }),
                             new PropertySettingsCollection(new List<PropertySettings>
                                 {
                                     new PropertySettings("Prop1", "value1"),
                                     new PropertySettings("Prop2", "value2")
                                 }))
                    });

            // --- Act
            var element = settings.WriteToXml("Temp");
            var newSettings = new ServiceContainerSettings(element);

            // --- Assert
            newSettings.Mappings.ShouldHaveCountOf(2);
            newSettings.Mappings[0].From.ShouldEqual(typeof (ISampleService));
            newSettings.Mappings[0].To.ShouldEqual(typeof(SampleService));
            newSettings.Mappings[0].Lifetime.ShouldBeNull();
            newSettings.Mappings[0].Parameters.ShouldBeNull();
            newSettings.Mappings[0].Properties.ShouldBeNull();
            newSettings.Mappings[1].From.ShouldEqual(typeof(ISampleRepository));
            newSettings.Mappings[1].To.ShouldEqual(typeof(SampleRepository));
            newSettings.Mappings[1].Lifetime.ShouldEqual(typeof(SingletonLifetimeManager));
            newSettings.Mappings[1].Parameters.ShouldHaveCountOf(2);
            newSettings.Mappings[1].Properties.ShouldHaveCountOf(2);
        }