Beispiel #1
0
        public void CanLoadConfigurationFromGlobalAndUserRepositoryUsingShortAndLongName()
        {
            var userConfigurationDirectory   = "user-configuration".AsNewDirectoryInfo();
            var systemConfigurationDirectory = "temp-configuration".AsNewDirectoryInfo();

            var systemConfiguration = new SampleConfigurationWithDefaultValues()
            {
                Value = 10, SecondValue = 200
            };
            var userConfiguration = new { Value = 20 };

            WriteConfiguration <SampleConfigurationWithDefaultValues>(userConfigurationDirectory, userConfiguration, true);
            WriteConfiguration <SampleConfigurationWithDefaultValues>(systemConfigurationDirectory, systemConfiguration);

            var container            = new CompositionContainer();
            var configurationCatalog = new LoadConfiguration(systemConfigurationDirectory, container);

            configurationCatalog.AddConfigurationLocation(userConfigurationDirectory);

            configurationCatalog.Load();
            var conf = container.GetExportedValue <SampleConfigurationWithDefaultValues>();

            Assert.Equal(20, conf.Value);
            Assert.Equal(200, conf.SecondValue);
        }
Beispiel #2
0
        protected override void ConfigureContainer(ContainerBuilder builder)
        {
            base.ConfigureContainer(builder);

            var assemblyDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);

            var catalogs = new List <ComposablePartCatalog>
            {
                new AggregateCatalog(AssemblySource.Instance.Select(x => new AssemblyCatalog(x))),
                new DirectoryCatalog(assemblyDirectory, "Plugins.*.dll"),
                new DirectoryCatalog(assemblyDirectory, "Plugins.dll"),
                new AssemblyCatalog(typeof(IItem).Assembly)
            };

            var pluginsPath = Path.Combine(assemblyDirectory, "Plugins");

            if (Directory.Exists(pluginsPath))
            {
                catalogs.Add(new DirectoryCatalog(pluginsPath, "Plugins.*.dll"));
                catalogs.Add(new DirectoryCatalog(pluginsPath, "Plugins.dll"));
            }
            else
            {
                pluginsPath = assemblyDirectory;
            }

            MefContainer =
                CompositionHost.Initialize(catalogs.ToArray());

            var loadConfiguration =
                new LoadConfiguration(new DirectoryInfo(Path.Combine(assemblyDirectory, "Configuration")), MefContainer);
            var localConfigurationDirectory = new DirectoryInfo(Path.Combine(assemblyDirectory, "Local.Configuration"));

            if (localConfigurationDirectory.Exists)
            {
                loadConfiguration.AddConfigurationLocation(localConfigurationDirectory);
            }
            loadConfiguration.Load();

            var dataDirectory     = Path.Combine(assemblyDirectory, "Data");
            var coreConfiguration = new CoreConfiguration(dataDirectory, pluginsPath);

            var learningStorageLocation = new DirectoryInfo(Path.Combine(coreConfiguration.DataDirectory, "Learnings"));
            var indexStorageLocation    = new DirectoryInfo(Path.Combine(coreConfiguration.DataDirectory, "Index"));

            var updateManagerAdapter = new UpdateManagerAdapter();

            var scheduler = new StdSchedulerFactory().GetScheduler();

            scheduler.JobFactory = new MefJobFactory(new SimpleJobFactory(), MefContainer);

            var batch = new CompositionBatch();

            batch.AddExportedValue(MefContainer);
            batch.AddExportedValue <ILoadConfiguration>(loadConfiguration);
            batch.AddExportedValue <ILog>(new NLogAdapterToCoreILog(NLog.LogManager.GetLogger("mef")));
            batch.AddExportedValue(coreConfiguration);
            batch.AddExportedValue(updateManagerAdapter);
            batch.AddExportedValue <IScheduler>(scheduler);
            MefContainer.Compose(batch);

            MefContainer.SatisfyImportsOnce(updateManagerAdapter);

            builder.RegisterInstance(MefContainer).AsSelf();
            builder.RegisterInstance(coreConfiguration).AsSelf();
            builder.RegisterInstance(updateManagerAdapter).AsSelf();

            builder.RegisterInstance(scheduler).As <IScheduler>();
            builder.RegisterInstance <IWindowManager>(new WindowManager());
            builder.RegisterInstance <IEventAggregator>(new EventAggregator());

            builder.RegisterModule(new LoggingModule(t => new NLogAdapterToCoreILog(NLog.LogManager.GetLogger(t.FullName)),
                                                     t => NLog.LogManager.GetLogger(t.FullName)));
            builder.RegisterModule(new SatisfyMefImports(MefContainer));

            builder.RegisterType <MainWindowViewModel>().AsSelf();
            builder.RegisterType <AutoCompleteBasedOnLucene>().AsSelf();
            builder.RegisterType <GetActionsForItem>().As <IGetActionsForItem>();

            builder.RegisterType <ConverterRepository>().As <IConverterRepository>();

            builder.RegisterType <SourceStorageFactory>().AsSelf().SingleInstance();

            builder.RegisterType <FileSystemLearningRepository>().As <ILearningRepository>().WithParameter("input", learningStorageLocation);
            builder.RegisterType <ScheduleIndexJobs>().As <IStartupTask>();
            builder.RegisterType <ScheduleUpdateCheckJob>().As <IStartupTask>();
            builder.RegisterType <IronPythonCommandsMefExport>().As <IStartupTask>();
            builder.RegisterType <LogScheduledJobs>().As <IStartupTask>();
            builder.RegisterType <ProcessMonitor>().As <IStartupTask>();

            builder.RegisterType <Shutdown>().AsSelf();

            builder.RegisterType <SeparateIndexesDirectoryFactory>()
            .As <IDirectoryFactory>().WithParameter("root", indexStorageLocation)
            .SingleInstance();
        }