Exemplo n.º 1
0
        private void FindAndRegister(params Type[] types)
        {
            ITypeFinder        finder      = new Fakes.FakeTypeFinder(types);
            ServiceRegistrator registrator = new ServiceRegistrator(finder, container);

            registrator.RegisterServices(registrator.FindServices());
        }
Exemplo n.º 2
0
        public void NestedNewServices_AreFound()
        {
            var finder = TestSupport.TypeFinder();

            var registrator = new ServiceRegistrator(finder, container);

            registrator.RegisterServices(registrator.FindServices());

            container.Resolve <OuterService2.InnerService>().ShouldNotBe(null);
        }
Exemplo n.º 3
0
        public void InternalServices_AreFound()
        {
            var finder = TestSupport.TypeFinder();

            var registrator = new ServiceRegistrator(finder, container);

            registrator.RegisterServices(registrator.FindServices());

            container.Resolve <IInternalService>().ShouldBeTypeOf <InternalService>();
        }
Exemplo n.º 4
0
        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> >());
        }
Exemplo n.º 5
0
        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));
        }
Exemplo n.º 6
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));
        }
Exemplo n.º 7
0
        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);
        }
Exemplo n.º 8
0
        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));
        }
Exemplo n.º 9
0
        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));
        }
Exemplo n.º 10
0
        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[]>());
        }
Exemplo n.º 11
0
        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));
        }
Exemplo n.º 12
0
        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));
        }
Exemplo n.º 13
0
        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));
        }
Exemplo n.º 14
0
        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));
        }
Exemplo n.º 15
0
        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>());
        }
Exemplo n.º 16
0
        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));
        }
Exemplo n.º 17
0
        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> >());
        }
Exemplo n.º 18
0
        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>());
        }
Exemplo n.º 19
0
        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>());
        }
Exemplo n.º 20
0
        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]));
        }
Exemplo n.º 21
0
        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
            }
        }