public void CircularContainerReferencesAreRecognized1() { // --- Arrange var settings = new ServiceRegistrySettings("other", new List<ServiceContainerSettings> { new ServiceContainerSettings("default", "other", new List<MappingSettings>()), new ServiceContainerSettings("other", "default", new List<MappingSettings>()) }); // --- Act // ReSharper disable ObjectCreationAsStatement new ServiceRegistry(settings); // ReSharper restore ObjectCreationAsStatement }
public void ReadAndWriteWorksAsExpected2() { const string ROOT = "Root"; // --- Arrange var settings = new ServiceRegistrySettings(null, null); // --- Act var element = settings.WriteToXml(ROOT); var newSetting = new ServiceRegistrySettings(element); // --- Assert newSetting.Resolver.ShouldBeNull(); newSetting.DefaultContainer.ShouldBeNull(); newSetting.Containers.ShouldHaveCountOf(0); }
public void ReadAndWriteWorksAsExpected3() { const string CONTAINER_NAME = "containerName"; const string ROOT = "Root"; // --- Arrange var settings = new ServiceRegistrySettings(CONTAINER_NAME, null, new DefaultTypeResolver()); // --- Act var element = settings.WriteToXml(ROOT); var newSetting = new ServiceRegistrySettings(element); // --- Assert newSetting.Resolver.ShouldNotBeNull(); newSetting.DefaultContainer.ShouldEqual(CONTAINER_NAME); newSetting.Containers.ShouldHaveCountOf(0); }
/// <summary> /// Configures the service registry from the specified settings. /// </summary> /// <param name="settings">Service registry settings</param> public ServiceRegistry(ServiceRegistrySettings settings) { // --- Check for duplicate container names var dupNames = (from container in settings.Containers group container by container.Name into g where g.Count() > 1 select g.Key).ToList(); if (dupNames.Count > 0) { throw new DuplicatedContainerNameException(dupNames); } // --- Register containers foreach (var container in settings.Containers) { RegisterContainer(settings.Containers.ToList(), container, new List <string>()); } // --- Set the default container var defaultName = settings.DefaultContainer; if (string.IsNullOrWhiteSpace(defaultName)) { // --- The first container is the default, or an empty container, if the is not one defined if (Containers.Count == 0) { DefaultContainer = new ServiceContainer(); Containers.Add(DefaultContainer.Name, DefaultContainer); } else { DefaultContainer = Containers[settings.Containers[0].Name]; } } else { // --- Find the container by the provided name DefaultContainer = this[defaultName]; if (DefaultContainer == null) { throw new ContainerNotFoundException(defaultName); } } }
public void UnknownDefaultContainerFails3() { // --- Arrange var settings = new ServiceRegistrySettings(null, new List<ServiceContainerSettings> { new ServiceContainerSettings("other", null, new List<MappingSettings>()), new ServiceContainerSettings("services", "default", new List<MappingSettings>()), }); // --- Act // ReSharper disable ObjectCreationAsStatement new ServiceRegistry(settings); // ReSharper restore ObjectCreationAsStatement }
public void RemoveServiceWorks() { // --- Arrange var settings = new ServiceRegistrySettings("default", new List<ServiceContainerSettings> { new ServiceContainerSettings("default", null, new List<MappingSettings> { new MappingSettings(typeof (ISampleRepository), typeof (SampleRepository)), new MappingSettings(typeof (ISampleService), typeof (ISampleService)) }) }); var registry = new ServiceRegistry(settings); // --- Act var before = registry.DefaultContainer.GetRegisteredServices(); registry.RemoveService(typeof(ISampleRepository)); var after = registry.DefaultContainer.GetRegisteredServices(); // --- Assert before.ShouldHaveCountOf(2); after.ShouldHaveCountOf(1); }
public void GetServiceWorks2() { // --- Arrange var settings = new ServiceRegistrySettings("default", new List<ServiceContainerSettings> { new ServiceContainerSettings("default", null, new List<MappingSettings> { new MappingSettings(typeof (ISampleRepository), typeof (SampleRepository)), new MappingSettings(typeof (ISampleService), typeof (ISampleService)) }) }); var registry = new ServiceRegistry(settings); // --- Act var service = registry.GetService<ISampleRepository>(); // --- Assert service.ShouldBeOfType(typeof (SampleRepository)); }
public void DuplicateContainerNamesFail() { // --- Arrange var settings = new ServiceRegistrySettings("other", new List<ServiceContainerSettings> { new ServiceContainerSettings("other", null, new List<MappingSettings>()), new ServiceContainerSettings("other", null, new List<MappingSettings>()), }); // --- Act // ReSharper disable ObjectCreationAsStatement new ServiceRegistry(settings); // ReSharper restore ObjectCreationAsStatement }
public void DuplicateContainerNamesAreInTheExceptionMessage2() { // --- Arrange var settings = new ServiceRegistrySettings(null, new List<ServiceContainerSettings> { new ServiceContainerSettings("doublecontainer", null, new List<MappingSettings>()), new ServiceContainerSettings("doublecontainer", null, new List<MappingSettings>()), }); // --- Act try { // ReSharper disable ObjectCreationAsStatement new ServiceRegistry(settings); // ReSharper restore ObjectCreationAsStatement Assert.Fail("Exception was expected"); } catch (DuplicatedContainerNameException ex) { ex.Message.ShouldContain("doublecontainer"); } }
public void ConstructionWorskWithMultipleContainerAndParents2() { // --- Arrange var settings = new ServiceRegistrySettings("other", new List<ServiceContainerSettings> { new ServiceContainerSettings("default", "other", new List<MappingSettings> { new MappingSettings(typeof (ISampleService), typeof (ISampleService)) }), new ServiceContainerSettings("other", null, new List<MappingSettings> { new MappingSettings(typeof (ISampleRepository), typeof (SampleRepository)), new MappingSettings(typeof (ISampleService), typeof (ISampleService)) }) }); // --- Act var registry = new ServiceRegistry(settings); // --- Assert registry.ContainerCount.ShouldEqual(2); registry["default"].ShouldNotBeNull(); registry["default"].GetRegisteredServices().ShouldHaveCountOf(1); registry["other"].ShouldNotBeNull(); registry["default"].Parent.ShouldBeSameAs(registry["other"]); registry["other"].GetRegisteredServices().ShouldHaveCountOf(2); registry["other"].ShouldBeSameAs(registry.DefaultContainer); }
public void ConstructionWorksWithSimpleContainer() { // --- Arrange var settings = new ServiceRegistrySettings("default", new List<ServiceContainerSettings> { new ServiceContainerSettings("default", null, new List<MappingSettings> { new MappingSettings(typeof (ISampleRepository), typeof (SampleRepository)), new MappingSettings(typeof (ISampleService), typeof (ISampleService)) }) }); // --- Act var registry = new ServiceRegistry(settings); // --- Assert registry.ContainerCount.ShouldEqual(1); registry.HasContainer("default").ShouldBeTrue(); registry["default"].ShouldNotBeNull(); registry["default"].ShouldBeSameAs(registry.DefaultContainer); registry[null].ShouldBeSameAs(registry.DefaultContainer); registry[""].ShouldBeSameAs(registry.DefaultContainer); }
public void ConstructionWorksWithNoContainer() { // --- Arrange var settings = new ServiceRegistrySettings(null, new List<ServiceContainerSettings>()); // --- Act var registry = new ServiceRegistry(settings); // --- Assert registry.ContainerCount.ShouldEqual(1); registry.DefaultContainer.GetRegisteredServices().ShouldHaveCountOf(0); }