#region License
// Copyright 2005-2019 Paul Kohler (https://github.com/paulkohler/minisqlquery). All rights reserved.
// This source code is made available under the terms of the GNU Lesser General Public License v3.0
// https://github.com/paulkohler/minisqlquery/blob/master/LICENSE
#endregion
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Ninject;
namespace MiniSqlQuery.Core
{
///
/// The core services of the application (singleton).
///
public class ApplicationServices : IApplicationServices
{
///
/// The _configuration objects.
///
private static readonly List _configurationObjects = new List();
///
/// The _container.
///
private static readonly IKernel _container;
///
/// The _plugins.
///
private readonly Dictionary _plugins = new Dictionary();
///
/// Initializes static members of the class.
///
static ApplicationServices()
{
_container = new StandardKernel();
// add self
_container
.Bind()
.To()
.InSingletonScope()
.Named("ApplicationServices");
}
///
/// Occurs when a system message is posted.
///
public event EventHandler SystemMessagePosted;
///
/// Gets a reference to the singleton instance of the services for this application.
///
/// The singleton instance of .
public static IApplicationServices Instance
{
get { return _container.Get(); }
}
///
/// Gets the Dependency Injection container.
///
/// The container.
public IKernel Container
{
get { return _container; }
}
///
/// Gets the application host window.
///
/// The host window - a .
public IHostWindow HostWindow
{
get { return _container.Get(); }
}
///
/// Gets a dictionary of the current plugins for this application.
///
/// A reference to the plugin dictionary.
public Dictionary Plugins
{
get { return _plugins; }
}
///
/// Gets the application settings instance.
///
/// A reference to the settings handler.
public IApplicationSettings Settings
{
get { return _container.Get(); }
}
///
/// The get configuration object types.
///
/// An array of configuration objects.
public Type[] GetConfigurationObjectTypes()
{
return _configurationObjects.ToArray();
}
///
/// Initializes the plugins that have been loaded during application startup.
///
public void InitializePlugIns()
{
foreach (IPlugIn plugIn in _plugins.Values)
{
try
{
if (HostWindow != null)
{
HostWindow.SetStatus(null, "Initializing " + plugIn.PluginName);
}
plugIn.InitializePlugIn();
}
catch (Exception exp)
{
if (HostWindow == null)
{
throw;
}
HostWindow.DisplayMessageBox(
null,
string.Format("Error Initializing {0}:{1}{2}", plugIn.PluginName, Environment.NewLine, exp),
"Plugin Error",
MessageBoxButtons.OK,
MessageBoxIcon.Warning,
MessageBoxDefaultButton.Button1,
0,
null,
null);
}
}
}
///
/// Loads the (calling its method) and
/// adds it to the dictionary for access by other services.
///
/// The plugin to load.
/// If is null.
public void LoadPlugIn(IPlugIn plugIn)
{
if (plugIn == null)
{
throw new ArgumentNullException("plugIn");
}
plugIn.LoadPlugIn(this);
var type = plugIn.GetType();
_plugins.Add(type, plugIn);
_container.Bind().To(type).InSingletonScope().Named(type.FullName);
}
///
/// Posts a system message for listeners.
///
/// A system message type.
/// The asssociated data.
public void PostMessage(SystemMessage message, object data)
{
OnSystemMessagePosted(new SystemMessageEventArgs(message, data));
}
///
/// Registers the component service type with and implemetation of type .
///
/// The contract type.
/// The implementing type.
/// The key or name of the service.
public void RegisterComponent(string key)
{
_container.Bind().To(typeof(TImp)).InTransientScope().Named(key);
}
///
/// Registers the component implemetation of type .
///
/// The implementing type.
/// The key or name of the service.
public void RegisterComponent(string key)
{
_container.Bind().ToSelf().InTransientScope().Named(key);
}
///
/// The register configuration object.
///
/// A configuration class.
public void RegisterConfigurationObject() where TConfig : IConfigurationObject
{
RegisterComponent(typeof(TConfig).FullName);
// haven't successfully been able to query this into out of castle container (yet)
_configurationObjects.Add(typeof(TConfig));
}
///
/// Registers the editor of type using the .
///
/// The editor type (e.g. "BasicXmlEditor").
/// The file extension descriiptor for this type.
public void RegisterEditor(FileEditorDescriptor fileEditorDescriptor) where TEditor : IEditor
{
RegisterComponent(fileEditorDescriptor.EditorKeyName);
// push the ext reg into the resolver....
IFileEditorResolver resolver = Resolve();
resolver.Register(fileEditorDescriptor);
}
///
/// Registers the component service type with and implemetation of type as a singleton.
///
/// The contract type.
/// The implementing type.
/// The key or name of the service.
public void RegisterSingletonComponent(string key)
{
_container.Bind().To(typeof(TImp)).InSingletonScope().Named(key);
}
///
/// Remove the component by name.
///
/// True on success.
public void RemoveComponent()
{
_container.Unbind();
}
///
/// Resolves an instance of from the container.
///
/// The type of object to resolve, can be an interface or class.
/// The key (can be null if not applicable).
/// An instance of the type depending on the containters configuration.
public T Resolve(string key)
{
if (key == null)
{
return _container.Get();
}
return _container.Get(key);
}
///
/// The resolve.
///
/// The type of object to resolve, can be an interface or class.
/// An instance of the type depending on the containters configuration.
public T Resolve()
{
return _container.Get();
}
///
/// The on system message posted.
///
/// The event args.
protected void OnSystemMessagePosted(SystemMessageEventArgs eventArgs)
{
EventHandler handler = SystemMessagePosted;
if (handler != null)
{
handler(this, eventArgs);
}
}
}
}