Exemplo n.º 1
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.º 2
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.º 3
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>>());
        }
        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 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 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 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.º 9
0
        public virtual void Configure(IEngine engine, EventBroker broker, ConfigurationManagerWrapper configuration)
        {
            engine.Container.AddComponentInstance("n2.configuration", typeof(ConfigurationManagerWrapper), configuration);
            engine.Container.AddComponentInstance("n2.engine", typeof(IEngine), engine);
            engine.Container.AddComponentInstance("n2.container", typeof(IServiceContainer), engine.Container);
            engine.Container.AddComponentInstance("n2.containerConfigurer", typeof(ContainerConfigurer), this);

            AddComponentInstance(engine.Container, configuration.GetConnectionStringsSection());
            AddComponentInstance(engine.Container, configuration.Sections.Engine);
            RegisterConfiguredComponents(engine.Container, configuration.Sections.Engine);
            AddComponentInstance(engine.Container, configuration.Sections.Web);
            InitializeEnvironment(engine.Container, configuration.Sections);
            AddComponentInstance(engine.Container, configuration.Sections.Database);
            AddComponentInstance(engine.Container, configuration.Sections.Management);
            AddComponentInstance(engine.Container, broker);

            var skipTypes = configuration.Sections.Engine.Components.GetConfiguredServiceTypes();
#if DEBUG_2
            var watf = new WebAppTypeFinder(new TypeCache(new BasicTemporaryFileHelper(null)), configuration.Sections.Engine);
            engine.Container.AddComponentInstance(watf.GetType().FullName, typeof(ITypeFinder), watf);
            var registrator = new ServiceRegistrator(watf, engine.Container);

#else
            AddComponentUnlessConfigured(engine.Container, typeof(BasicTemporaryFileHelper), typeof(BasicTemporaryFileHelper), skipTypes);
            AddComponentUnlessConfigured(engine.Container, typeof(TypeCache), typeof(TypeCache), skipTypes);
            AddComponentUnlessConfigured(engine.Container, typeof(ITypeFinder), typeof(WebAppTypeFinder), skipTypes);
            AddComponentUnlessConfigured(engine.Container, typeof(ServiceRegistrator), typeof(ServiceRegistrator), skipTypes);
            var registrator = engine.Container.Resolve<ServiceRegistrator>();
#endif
            var services = registrator.FindServices();
            var configurationKeys = configuration.GetComponentConfigurationKeys();
            services = registrator.FilterServices(services, configurationKeys);
            services = registrator.FilterServices(services, skipTypes);
            registrator.RegisterServices(services);

            // workaround for problem with open generic binding for IRepository<> - requires that [Service] attributes be removed
            var cir = engine.Container.Resolve<IContentItemRepository>();
            // register the signleton the hard way
            engine.Container.AddComponentInstance("n2.IRepository.ContentItem", typeof(IRepository<ContentItem>), cir);
#if DEBUG_2
            // double check container sanity
            var test = engine.Container.Resolve<IRepository<ContentItem>>();
            Logger.Debug(cir.GetType().FullName);
            Logger.Debug(test.GetType().FullName);
            if (!cir.Equals(test))
                throw new Exception("inconsistent repositories");
#endif
            InitializeUrlParser(engine.Container);
        }
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[]>());
        }
        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 NestedNewServices_AreFound()
        {
            var finder = TestSupport.TypeFinder();

            var registrator = new ServiceRegistrator(finder, container);
            registrator.RegisterServices(registrator.FindServices());

            container.Resolve<OuterService2.InnerService>().ShouldNotBe(null);
        }
 private void FindAndRegister(params Type[] types)
 {
     ITypeFinder finder = new Fakes.FakeTypeFinder(types);
     ServiceRegistrator registrator = new ServiceRegistrator(finder, container);
     registrator.RegisterServices(registrator.FindServices());
 }
        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 InternalServices_AreFound()
        {
            var finder = TestSupport.TypeFinder();

            var registrator = new ServiceRegistrator(finder, container);
            registrator.RegisterServices(registrator.FindServices());

			container.Resolve<IInternalService>().ShouldBeOfType<InternalService>();
        }
        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 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.º 18
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));
        }
        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 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
            }
        }
        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));
        }