Пример #1
0
        protected override void OnStartup(StartupEventArgs e)
        {
            base.OnStartup(e);

            // Initialize the IOC container and some of the default services
            var container = new WindsorContainer();
            container.Kernel.Resolver.AddSubResolver(new ArrayResolver(container.Kernel));
            container.Register(Component.For<IWindsorContainer>().Instance(container));
            container.Register(Component.For<ILog>().ImplementedBy<DwightsLog>());
            container.Register(Component.For<IDispatcher>().Instance(new WpfDispatcher(Dispatcher)));
            container.Register(Component.For<ShellWindow>().LifeStyle.Transient);
            container.Register(Component.For<IEventAggregator>().ImplementedBy<EventAggregator>().LifeStyle.Singleton);
            container.Register(Component.For<IRegionManager>().ImplementedBy<RegionManager>().LifeStyle.Singleton);
            container.Register(Component.For<IApplicationResourcesManager>().ImplementedBy<ApplicationResourcesManager>().LifeStyle.Singleton);

            // Setup the region adapter mappings
            var regionMappings = new RegionAdapterMappings();
            regionMappings.RegisterMapping(typeof(ContentControl), new ContentControlRegionAdapter());
            regionMappings.RegisterMapping(typeof(ItemsControl), new ItemsControlRegionAdapter());
            container.Register(Component.For<RegionAdapterMappings>().Instance(regionMappings));

            // Setup the main window
            MainWindow = container.Resolve<ShellWindow>();
            MainWindow.Show();

            // Define the modules to load
            var modules = new StaticModuleEnumerator()
                .AddModule(typeof(SearchModule))
                .AddModule(typeof(AdvertisingModule))
                .AddModule(typeof(AdventureWorksSearchModule));
            
            // Load the modules
            var moduleLoader = new ModuleLoader(new WindsorContainerAdapter(container), new TraceLogger());
            moduleLoader.Initialize(modules.GetModules());
        }
        public void InitializationExceptionsAreWrapped()
        {
            Assembly asm = CompilerHelper.CompileFileAndLoadAssembly("Microsoft.Practices.Composite.Tests.Mocks.Modules.MockModuleThrowingException.cs",
                                                                     @".\MocksModulesThwrowing\MockModuleThrowingException.dll");

            ModuleLoader loader = new ModuleLoader(new MockContainerAdapter(), new MockLogger());

            ModuleInfo info = new ModuleInfo(asm.CodeBase.Replace(@"file:///", ""),
                                             "Microsoft.Practices.Composite.Tests.Mocks.Modules.MockModuleThrowingException", "MockModuleThrowingException");

            loader.Initialize(new[] { info });
        }
        public void FailWhenLoadingModulesWithCyclicDependencies()
        {
            List<string> assemblies = new List<string>();

            // Create several modules with this dependency graph (X->Y meaning Y depends on X)
            // 1->2, 2->3, 3->4, 4->5, 4->2
            assemblies.Add(CompilerHelper.GenerateDynamicModule("Module1", "Module1"));
            assemblies.Add(CompilerHelper.GenerateDynamicModule("Module2", "Module2", "Module1", "Module4"));
            assemblies.Add(CompilerHelper.GenerateDynamicModule("Module3", "Module3", "Module2"));
            assemblies.Add(CompilerHelper.GenerateDynamicModule("Module4", "Module4", "Module3"));
            assemblies.Add(CompilerHelper.GenerateDynamicModule("Module5", "Module5", "Module4"));

            List<ModuleInfo> modules = new List<ModuleInfo>();
            modules.Add(new ModuleInfo(assemblies[0], "Module1.TestModules.Module1Class", "Module1"));
            modules.Add(new ModuleInfo(assemblies[1], "Module2.TestModules.Module2Class", "Module2", "Module1", "Module4"));
            modules.Add(new ModuleInfo(assemblies[2], "Module3.TestModules.Module3Class", "Module3", "Module2"));
            modules.Add(new ModuleInfo(assemblies[3], "Module4.TestModules.Module4Class", "Module4", "Module3"));
            modules.Add(new ModuleInfo(assemblies[4], "Module5.TestModules.Module5Class", "Module5", "Module4"));

            ModuleLoader loader = new ModuleLoader(new MockContainerAdapter(), new MockLogger());
            loader.Initialize(modules.ToArray());
        }
 public void ShouldResolveModuleAndInitializeSingleModule()
 {
     IContainerFacade containerFacade = new MockContainerAdapter();
     var service = new ModuleLoader(containerFacade, new MockLogger());
     FirstTestModule.wasInitializedOnce = false;
     var info = new ModuleInfo(typeof(FirstTestModule).Assembly.Location, typeof(FirstTestModule).FullName, "FirstTestModule");
     service.Initialize(new[] { info });
     Assert.IsTrue(FirstTestModule.wasInitializedOnce);
 }
        public void FailWhenDependingOnMissingModule()
        {
            string assembly = CompilerHelper.GenerateDynamicModule("ModuleK", null, "ModuleL");
            ModuleInfo module = new ModuleInfo(assembly, "ModuleK.TestsModules.ModuleKClass", "ModuleK", "ModuleL");

            ModuleLoader loader = new ModuleLoader(new MockContainerAdapter(), new MockLogger());
            loader.Initialize(new[] { module });
        }
        public void ShouldNotInitializeIfAlreadyLoaded()
        {
            ModuleLoadTracker.ModuleLoadStack.Clear();
            IContainerFacade containerFacade = new MockContainerAdapter();
            var service = new ModuleLoader(containerFacade, new MockLogger());
            FirstTestModule.wasInitializedOnce = false;
            SecondTestModule.wasInitializedOnce = false;
            var firstModuleInfo = new ModuleInfo(typeof(FirstTestModule).Assembly.Location, typeof(FirstTestModule).FullName, "FirstTestModule");

            service.Initialize(new[] { firstModuleInfo });
            Assert.AreEqual(1, ModuleLoadTracker.ModuleLoadStack.Count, "ModuleLoadStack should only contain 1 module");
            service.Initialize(new[] { firstModuleInfo });
            Assert.AreEqual(1, ModuleLoadTracker.ModuleLoadStack.Count, "ModuleLoadStack should not have module that has already been loaded");

        }
        public void ShouldLogModuleInitializationError()
        {
            IContainerFacade containerFacade = new MockContainerAdapter();
            var logger = new MockLogger();
            var service = new ModuleLoader(containerFacade, logger);
            ExceptionThrowingModule.wasInitializedOnce = false;
            var exceptionModule = new ModuleInfo(typeof(ExceptionThrowingModule).Assembly.Location, typeof(ExceptionThrowingModule).FullName, "ExceptionThrowingModule");

            try
            {
                service.Initialize(new[] { exceptionModule });
            }
            catch (ModuleLoadException)
            {
            }
            Assert.IsNotNull(logger.LastMessage);
            StringAssert.Contains(logger.LastMessage, "ExceptionThrowingModule");
        }
        public void ShouldInitializeModulesInOrder()
        {
            IContainerFacade containerFacade = new MockContainerAdapter();
            var service = new ModuleLoader(containerFacade, new MockLogger());

            ModuleLoadTracker.ModuleLoadStack.Clear();
            var dependencyModule = new ModuleInfo(typeof(DependencyModule).Assembly.Location, typeof(DependencyModule).FullName, "DependencyModule");
            var dependantModule = new ModuleInfo(typeof(DependantModule).Assembly.Location, typeof(DependantModule).FullName, "DependantModule", new[] { "DependencyModule" });
            service.Initialize(new[] { dependantModule, dependencyModule });

            Assert.AreEqual(typeof(DependantModule), ModuleLoadTracker.ModuleLoadStack.Pop());
            Assert.AreEqual(typeof(DependencyModule), ModuleLoadTracker.ModuleLoadStack.Pop());

        }
        public void ShouldResolveAndInitializeMultipleModules()
        {
            IContainerFacade containerFacade = new MockContainerAdapter();
            var service = new ModuleLoader(containerFacade, new MockLogger());
            FirstTestModule.wasInitializedOnce = false;
            SecondTestModule.wasInitializedOnce = false;

            var firstModuleInfo = new ModuleInfo(typeof(FirstTestModule).Assembly.FullName, typeof(FirstTestModule).FullName, "FirstTestModule");
            var secondModuleInfo = new ModuleInfo(typeof(SecondTestModule).Assembly.FullName, typeof(SecondTestModule).FullName, "SecondTestModule");
            //var firstModuleInfo = new ModuleInfo(typeof(FirstTestModule).Assembly.Location, typeof(FirstTestModule).FullName, "FirstTestModule");
            //var secondModuleInfo = new ModuleInfo(typeof(SecondTestModule).Assembly.Location, typeof(SecondTestModule).FullName, "SecondTestModule");

            service.Initialize(new[] { firstModuleInfo, secondModuleInfo });
            Assert.IsTrue(FirstTestModule.wasInitializedOnce);
            Assert.IsTrue(SecondTestModule.wasInitializedOnce);
        }