public static void ConfigureContainer(IObjectContainer container, ApplicationConfiguration config) { if (config == null || config.ContainerConfiguration == null) return; if (container == null) return; foreach (object item in config.ContainerConfiguration.Items) { ObjectContainerAdd add = item as ObjectContainerAdd; if (item != null) { Type tKey = Type.GetType(add.KeyType, true, true); Type tObj = Type.GetType(add.ObjectType, true, true); container.Register(tKey, Activator.CreateInstance(tObj)); continue; } ObjectContainerRemove remove = item as ObjectContainerRemove; if (item != null) { throw new NotImplementedException("Removing an object is not supported yet."); } ObjectContainerClear clear = item as ObjectContainerClear; if (item != null) { container.Clear(); continue; } } }
public void ConfigureModulesWith2Modules() { ApplicationConfiguration config = new ApplicationConfiguration(); config.ModulesConfiguration.Modules.Add(new ModuleDefinition() { Type = GetTypeFullname("Module1") }); config.ModulesConfiguration.Modules.Add(new ModuleDefinition() { Type = GetTypeFullname("Module2") }); ObjectContainer container = new ObjectContainer(); ConfigManager.ConfigureModules(container, config); Assert.AreEqual(2, container.AllObjects.Length); }
public void ConfigureContainer() { ApplicationConfiguration config = new ApplicationConfiguration(); config.ContainerConfiguration.Items.Add(new ObjectContainerAdd() { KeyType = GetTypeFullname("IA"), ObjectType = GetTypeFullname("A") }); config.ContainerConfiguration.Items.Add(new ObjectContainerAdd() { KeyType = GetTypeFullname("IB"), ObjectType = GetTypeFullname("B") }); ObjectContainer container = new ObjectContainer(); ConfigManager.ConfigureContainer(container, config); Assert.AreEqual(2, container.AllObjects.Length); IA a = container.Get<IA>(); Assert.IsNotNull(a); Assert.AreSame(typeof(A), a.GetType()); }
internal static void LoadAndConfigureServices(ServiceModel.IServiceProvider container, ApplicationConfiguration config) { if (config == null) return; if (container == null) return; foreach (ServiceDefinition srvDef in config.ServicesConfiguration.ServiceDefinitions) { Type contractType = Type.GetType(srvDef.Contract, true, true); Type serviceType = Type.GetType(srvDef.Service, true, true); IService service = (IService)Activator.CreateInstance(serviceType); container.RegisterService(contractType, service); // configure any IConfigurable service. if (typeof(IConfigurable).IsAssignableFrom(serviceType) && srvDef.Config != null) ((IConfigurable)service).Configure(srvDef.Config); } }
internal static void LoadModules(IApplicationModuleManager container, ApplicationConfiguration config) { if (config == null) return; if (container == null) return; foreach (ModuleDefinition moduleDef in config.ModulesConfiguration.Modules) { Type moduleType = Type.GetType(moduleDef.Type, false, true); if (moduleType == null) throw new InvalidOperationException( string.Format("Cannot load type from string \"{0}\".", moduleDef.Type)); if (!typeof(IApplicationModule).IsAssignableFrom(moduleType)) throw new InvalidCastException(string.Format("Type {0} does not implements type {1}.", moduleType, typeof(IApplicationModule))); IApplicationModule module = (IApplicationModule)Activator.CreateInstance(moduleType); container.Load(module); } }
/// <summary> /// Starts the application. That method should be one of the first being called when the program starts. The method /// accepts an existing configuration object. See <see cref="StartApplication()"/> for default configuration. /// </summary> /// <param name="config">Loaded application object.</param> public void StartApplication(ApplicationConfiguration config) { Logger.Info("Starting application..."); _config = config; InitializeComponents(); // Load services and modules from the config file. Logger.Info("Loading services and modules from config."); if (_config != null) { ConfigManager.LoadAndConfigureServices(ServiceProvider, _config); ConfigManager.LoadModules(ApplicationModuleManager, _config); } // start services Logger.Info("Starting services."); foreach (IService service in ServiceProvider.AllServices) { IStartable startable; startable = service as IStartable; if (startable != null) startable.Start(); } // Display the Shell, if any. if (Shell != null) { Logger.Info("Displaying the Shell."); foreach (IViewController ctrl in Shell.CreateViewControllers()) { PresentationController.RegisterViewController(ctrl); } foreach (IPresenter presenter in Shell.CreatePresenters()) { PresentationController.RegisterPresenter(presenter); } Shell.Show(); } // Activate modules Logger.Info("Activating modules."); ApplicationModuleManager.ActivateAll(); _started = true; Logger.Info("Application started."); }
/// <summary> /// Starts the application. That method should be one of the first being called when the program starts. The method /// accepts an existing configuration object. See <see cref="StartApplication()"/> for default configuration. /// </summary> /// <param name="config">Loaded application object.</param> public void StartApplication(ApplicationConfiguration config) { _config = config; if (ServiceProvider == null) ServiceProvider = new ServiceProvider(); if (PresentationController == null) PresentationController = new PresentationController(); if (ObjectContainer == null) ObjectContainer = new ObjectContainer(); if (ExtensionPortManager == null) ExtensionPortManager = new ExtensionPortManager(ObjectContainer); if (_config != null) { ConfigManager.ConfigureContainer(ObjectContainer, _config); ConfigManager.ConfigureServices(ServiceProvider, _config); ConfigManager.ConfigureModules(ObjectContainer, _config); } //start services foreach (IService service in ServiceProvider.AllServices) { IStartable startable; startable = service as IStartable; if (startable != null) startable.Start(); } //Display the Shell if (Shell != null) { foreach (IViewController ctrl in Shell.CreateViewControllers()) { PresentationController.ViewControllers.Add(ctrl); } foreach (IPresenter presenter in Shell.CreatePresenters()) { PresentationController.RegisterPresenter(presenter); } Shell.Show(); } //Start modules foreach (object obj in ObjectContainer.AllObjects) { IModule module = obj as IModule; if (module != null) { module.PresentationController = PresentationController; IStartable start; start = module as IStartable; if (start != null) start.Start(); } } _started = true; }
/// <summary> /// Starts the application. That method should be one of the first being called when the program starts. The method /// loads the configuration from the AppDomain configuration file. /// </summary> /// <remarks> /// That method will look for application configuration. In the configuration file, the config section must /// be called "foundation.application", and must be in the namespace /// "http://schemas.omniscient.ca/foundation/applicationConfiguration.xsd". See the xsd schema file ApplicationConfiguration.xsd /// for the complete schema. /// </remarks> public void StartApplication() { //Get configuration from default config file. ApplicationConfiguration config; #if SILVERLIGHT config = new ApplicationConfiguration(); #else config = System.Configuration.ConfigurationManager.GetSection("foundation.application") as ApplicationConfiguration; #endif StartApplication(config); }
internal static void LoadAndConfigureServices(ServiceModel.IServiceProvider container, ApplicationConfiguration config) { if (config == null) { return; } if (container == null) { return; } foreach (ServiceDefinition srvDef in config.ServicesConfiguration.ServiceDefinitions) { Type contractType = Type.GetType(srvDef.Contract, true, true); Type serviceType = Type.GetType(srvDef.Service, true, true); IService service = (IService)Activator.CreateInstance(serviceType); container.RegisterService(contractType, service); // configure any IConfigurable service. if (typeof(IConfigurable).IsAssignableFrom(serviceType) && srvDef.Config != null) { ((IConfigurable)service).Configure(srvDef.Config); } } }