using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using DTS.Common;
using DTS.Common.Base;
using DTS.Common.Classes;
using DTS.Common.Core.PluginLib;
using DTS.Common.Interface;
using Microsoft.Practices.Prism.Events;
using Microsoft.Practices.Prism.Modularity;
using Microsoft.Practices.Prism.Regions;
using Microsoft.Practices.ServiceLocation;
using Microsoft.Practices.Unity;
using System.IO;
namespace DTS.Viewer.Loader
{
///
/// I think we need it...
///
public class ViewerLoaderSession
{
///
/// Custom Bootstrapper
///
///
/// Do not try to re-create the bootstrapper. Once modules are loaded, they will be be loaded into the DirectoryModuleCatalog and each
/// Module initialization will not be called (thus no registered types).
/// It appears that the current bootstrapper loads around 40 MB into memory.
/// To completely unload the bootstrapper would take same research and effort.
///
private Bootstrapper _bootstrapper;
public IUnityContainer _unityContainer { get; private set; }
public IServiceLocator ServiceLocator { get; private set; }
public IEventAggregator EventAggregator { get; private set; }
public IRegionManager RegionManager { get; private set; }
public ViewerLoaderSession()
{
}
public void CreateSession()
{
CreateBootstrapper();
if (_bootstrapper == null) return;
_unityContainer = _bootstrapper.Container;
EventAggregator = _unityContainer.Resolve();
ServiceLocator = _unityContainer.Resolve();
RegionManager = _unityContainer.Resolve();
var shellVviewmodel = _unityContainer.Resolve();
var viewerModule = _unityContainer.Resolve();
viewerModule.StartSession(true, @"C:\devDTS\RunTimeModules\DTS.Viewer.dll.config");
var viewModel = _unityContainer.Resolve();
shellVviewmodel.ContextMainRegion = viewModel.View;
//var assemblyList = LoadPlugins();
//if (assemblyList.Count == 1)
//{
// var assembly = assemblyList[0];
// if (assembly.ManifestModule.Name.StartsWith("DTS.Viewer"))
// {
// }
//}
//var viewDefinition = new ViewDefinition(RegionNames.MainRegion, typeof(IMainView), typeof(IMainViewModel));
//var viewModel = (IBaseViewModel)Container.Resolve(viewDefinition.ViewModelInterfaceType);
//var view = (IBaseView)Container.Resolve(viewDefinition.ViewInterfaceType);
//view.DataContext = viewModel;
//RegionManager.Regions[RegionNames.MainRegion].Add(view);
//RegionManager.ActivateViewIfExists(viewDefinition.RegionName, viewDefinition.ViewInterfaceType);
}
///
/// Return type of View/ViewModel registered with Unity Container
///
/// Name of the selected assembly (plug-in)
/// interface type
private Type GetRegisteredViewType(string name)
{
return (from v in _unityContainer.Registrations where v.RegisteredType.Name.EndsWith(name) select v.RegisteredType).FirstOrDefault();
}
private List LoadPlugins()
{
var pluginManager = PluginManager.GetPluginManager(Path.Combine(Environment.CurrentDirectory, @"DTS.Viewer.Loader.exe.config"));
return pluginManager.GetPluginList();
}
///
/// Create bootstrapper for prism-based modules and views.
///
///
/// Currently, there is no course of action for destroying the bootstrapper.
///
private void CreateBootstrapper()
{
if (_bootstrapper == null)
{
try
{
_bootstrapper = new Bootstrapper();
_bootstrapper.Run();
}
catch (ApplicationException ex)
{
throw new Exception("Failed to create bootstrapper ", ex);
}
}
if (_bootstrapper == null)
{
throw new Exception("Failed to create Bootstrapper!");
}
}
///
/// The Terminate method is called only when JMPS is in the process of shutting down.
/// If a component is just being closed, only the class destructor will be called.
/// Part of IComponent implementation.
///
public void Terminate()
{
}
}
}