예제 #1
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);
        }
        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 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));
        }
        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));
        }