private void FindAndRegister(params Type[] types) { ITypeFinder finder = new Fakes.FakeTypeFinder(types); ServiceRegistrator registrator = new ServiceRegistrator(finder, container); registrator.RegisterServices(registrator.FindServices()); }
public void InternalServices_AreFound() { var finder = TestSupport.TypeFinder(); var registrator = new ServiceRegistrator(finder, container); registrator.RegisterServices(registrator.FindServices()); container.Resolve <IInternalService>().ShouldBeTypeOf <InternalService>(); }
public void NestedNewServices_AreFound() { var finder = TestSupport.TypeFinder(); var registrator = new ServiceRegistrator(finder, container); registrator.RegisterServices(registrator.FindServices()); container.Resolve <OuterService2.InnerService>().ShouldNotBe(null); }
public void Requesting_NoConfigurations_DoesntResolveServices_ThatUsesConfigurations() { ITypeFinder finder = new Fakes.FakeTypeFinder(typeof(HighService), typeof(LowService)); ServiceRegistrator registrator = new ServiceRegistrator(finder, container); var services = registrator.FilterServices(registrator.FindServices()); registrator.RegisterServices(services); Assert.That(container.ResolveAll <IBarometer>().Count(), Is.EqualTo(0)); }
public void CanResolve_MultipleServices() { ITypeFinder finder = new Fakes.FakeTypeFinder(typeof(HighService), typeof(LowService)); ServiceRegistrator registrator = new ServiceRegistrator(finder, container); registrator.RegisterServices(registrator.FindServices()); var services = container.ResolveAll <IBarometer>(); Assert.That(services.Count(), Is.EqualTo(2)); }
public void Services_CanOverride_OtherServices_ByServiceType() { ITypeFinder finder = new Fakes.FakeTypeFinder(typeof(ReplacedService), typeof(ReplacingReplacedService)); ServiceRegistrator registrator = new ServiceRegistrator(finder, container); registrator.RegisterServices(registrator.FilterServices(registrator.FindServices())); Assert.That(container.Resolve <IReplacedInterface>(), Is.InstanceOf <ReplacingReplacedService>()); Assert.That(container.ResolveAll <IReplacedInterface>().Count(), Is.EqualTo(1)); }
public void GenericServices_CanBeResolved_ByServiceInterface() { ITypeFinder finder = new Fakes.FakeTypeFinder(typeof(GenericInterfacedService <>)); ServiceRegistrator registrator = new ServiceRegistrator(finder, container); registrator.RegisterServices(registrator.FindServices()); Assert.That(container.Resolve <IGenericService <int> >(), Is.InstanceOf <GenericInterfacedService <int> >()); Assert.That(container.Resolve <IGenericService <string> >(), Is.InstanceOf <GenericInterfacedService <string> >()); }
public void Services_AreAdded_ToTheContainer() { ITypeFinder finder = new Fakes.FakeTypeFinder(typeof(SelfService), typeof(NonAttributed)); ServiceRegistrator registrator = new ServiceRegistrator(finder, container); registrator.RegisterServices(registrator.FindServices()); Assert.That(container.Resolve <SelfService>(), Is.InstanceOf <SelfService>()); Assert.That(new TestDelegate(() => container.Resolve <NonAttributed>()), Throws.Exception); }
public void ResolveAll_WithTypeArgument_GivesAnArray_OfTheRequestedArrayType() { ITypeFinder finder = new Fakes.FakeTypeFinder(typeof(HighService), typeof(LowService)); ServiceRegistrator registrator = new ServiceRegistrator(finder, container); registrator.RegisterServices(registrator.FindServices()); var services = container.ResolveAll(typeof(IBarometer)); Assert.That(services, Is.InstanceOf <IBarometer[]>()); }
public void Requesting_MultipleConfigurations_GivesAllMatchingServices() { ITypeFinder finder = new Fakes.FakeTypeFinder(typeof(HighService), typeof(LowService)); ServiceRegistrator registrator = new ServiceRegistrator(finder, container); var services = registrator.FilterServices(registrator.FindServices(), "High", "Medium", "Low"); registrator.RegisterServices(services); Assert.That(container.ResolveAll <IBarometer>().Count(), Is.EqualTo(2)); }
public void Resolves_OnlyRequestedConfiguration() { ITypeFinder finder = new Fakes.FakeTypeFinder(typeof(HighService), typeof(LowService)); ServiceRegistrator registrator = new ServiceRegistrator(finder, container); var services = registrator.FilterServices(registrator.FindServices(), "High"); registrator.RegisterServices(services); Assert.That(container.Resolve <IBarometer>(), Is.InstanceOf <HighService>()); Assert.That(container.ResolveAll <IBarometer>().Count(), Is.EqualTo(1)); }
public void CanDependOn_MultipleServices() { ITypeFinder finder = new Fakes.FakeTypeFinder(typeof(HighService), typeof(LowService)); ServiceRegistrator registrator = new ServiceRegistrator(finder, container); registrator.RegisterServices(registrator.FindServices()); container.AddComponent("bw", typeof(BarometerWatcher), typeof(BarometerWatcher)); var watcher = container.Resolve <BarometerWatcher>(); Assert.That(watcher.Barometers.Count(), Is.EqualTo(2)); }
public void RequstingConfiguration_AlsoRegisterd_ServicesWithoutConfiguration() { ITypeFinder finder = new Fakes.FakeTypeFinder(typeof(SelfService), typeof(HighService), typeof(LowService)); ServiceRegistrator registrator = new ServiceRegistrator(finder, container); registrator.RegisterServices(registrator.FilterServices(registrator.FindServices(), "High")); Assert.That(container.Resolve <SelfService>(), Is.InstanceOf <SelfService>()); Assert.That(container.Resolve <IBarometer>(), Is.InstanceOf <HighService>()); Assert.That(container.ResolveAll <IBarometer>().Count(), Is.EqualTo(1)); }
public ICoreServiceLocator GetContainer(DotlessConfiguration configuration) { var builder = new Autofac.ContainerBuilder(); var registrar = new ServiceRegistrator(); registrar.RegisterServices(new CompilerServiceRegistrar(builder), configuration); var container = builder.Build(); return(new CompilerServiceLocator(container)); }
public void Services_CanDepend_OnEachOther() { ITypeFinder finder = new Fakes.FakeTypeFinder(typeof(SelfService), typeof(DependingService)); ServiceRegistrator registrator = new ServiceRegistrator(finder, container); registrator.RegisterServices(registrator.FindServices()); var service = container.Resolve <DependingService>(); Assert.That(service, Is.InstanceOf <DependingService>()); Assert.That(service.service, Is.InstanceOf <SelfService>()); }
public void Services_AreSingletons() { ITypeFinder finder = new Fakes.FakeTypeFinder(typeof(SelfService)); ServiceRegistrator registrator = new ServiceRegistrator(finder, container); registrator.RegisterServices(registrator.FindServices()); var one = container.Resolve <SelfService>(); var two = container.Resolve <SelfService>(); Assert.That(object.ReferenceEquals(one, two)); }
public void Services_DependingOn_MultipleServices_CanBeSingletons() { ITypeFinder finder = new Fakes.FakeTypeFinder(typeof(HighService), typeof(LowService)); ServiceRegistrator registrator = new ServiceRegistrator(finder, container); registrator.RegisterServices(registrator.FindServices()); container.AddComponent("bw", typeof(BarometerWatcher), typeof(BarometerWatcher)); var watcher = container.Resolve <BarometerWatcher>(); var watcher2 = container.Resolve <BarometerWatcher>(); Assert.That(watcher, Is.SameAs(watcher)); }
public void Services_CanDepend_OnGenericServiceInterface() { ITypeFinder finder = new Fakes.FakeTypeFinder(typeof(GenericInterfaceDependingService), typeof(GenericInterfacedService <>)); ServiceRegistrator registrator = new ServiceRegistrator(finder, container); registrator.RegisterServices(registrator.FindServices()); var service = container.Resolve <GenericInterfaceDependingService>(); Assert.That(service, Is.InstanceOf <GenericInterfaceDependingService>()); Assert.That(service.service, Is.InstanceOf <GenericInterfacedService <int> >()); }
public void Service_CanDecorate_ServiceOfSameType() { ITypeFinder finder = new Fakes.FakeTypeFinder(typeof(DecoratingService), typeof(InterfacedService)); ServiceRegistrator registrator = new ServiceRegistrator(finder, container); registrator.RegisterServices(registrator.FindServices()); var service = container.Resolve <IService>(); Assert.That(service, Is.InstanceOf <DecoratingService>()); Assert.That(((DecoratingService)service).decorated, Is.InstanceOf <InterfacedService>()); }
public void CanResolve_ServiceWithDependency_OnComponentInstance() { ITypeFinder finder = new Fakes.FakeTypeFinder(typeof(DependingServiceWithMissingDependency).Assembly.GetTypes()); ServiceRegistrator registrator = new ServiceRegistrator(finder, container); registrator.RegisterServices(registrator.FindServices()); container.AddComponentInstance("ud", typeof(UnregisteredDependency), new UnregisteredDependency()); var service = container.Resolve <DependingServiceWithMissingDependency>(); Assert.That(service, Is.InstanceOf <DependingServiceWithMissingDependency>()); }
public void CanRegister_MultipleServices_DependingOnSame_ServiceArray() { ITypeFinder finder = new Fakes.FakeTypeFinder(typeof(HighService), typeof(LowService)); ServiceRegistrator registrator = new ServiceRegistrator(finder, container); registrator.RegisterServices(registrator.FindServices()); container.AddComponent("bw", typeof(BarometerWatcher), typeof(BarometerWatcher)); container.AddComponent("ac", typeof(AltitudeComparer), typeof(AltitudeComparer)); var watcher = container.Resolve <BarometerWatcher>(); var comparer = container.Resolve <AltitudeComparer>(); Assert.That(watcher.Barometers[0], Is.SameAs(comparer.Barometers[0])); Assert.That(watcher.Barometers[1], Is.SameAs(comparer.Barometers[1])); }
public void Services_AreAdded_ToTheContainer_WithServiceType() { ITypeFinder finder = new Fakes.FakeTypeFinder(typeof(InterfacedService), typeof(NonAttributed)); ServiceRegistrator registrator = new ServiceRegistrator(finder, container); registrator.RegisterServices(registrator.FindServices()); Assert.That(container.Resolve <IService>(), Is.Not.Null); Assert.That(container.Resolve <IService>(), Is.InstanceOf <InterfacedService>()); try { var instance = container.Resolve <NonAttributed>(); Assert.Fail(); } catch (Exception) { // expected } }