#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;
namespace MiniSqlQuery.Core
{
///
/// Stores instances of commands by type.
///
public class CommandManager
{
///
/// The command cache.
///
private static readonly Dictionary CommandCache = new Dictionary();
///
/// Gets the command instance by .
///
/// Name of the command, e.g. "OpenFileCommand".
/// The first command by that name or null if not found.
public static ICommand GetCommandInstance(string commandTypeName)
{
foreach (Type cmdType in CommandCache.Keys)
{
if (cmdType.Name == commandTypeName)
{
return CommandCache[cmdType];
}
}
return null;
}
///
/// Gets or creates an instance of a command by type.
///
/// The type of command to get or create.
/// An instance of .
public static ICommand GetCommandInstance() where TCommand : ICommand, new()
{
ICommand cmd;
if (CommandCache.ContainsKey(typeof(TCommand)))
{
cmd = CommandCache[typeof(TCommand)];
}
else
{
cmd = new TCommand();
cmd.Services = ApplicationServices.Instance;
cmd.Settings = ApplicationServices.Instance.Settings;
CommandCache[typeof(TCommand)] = cmd;
}
return cmd;
}
///
/// Gets command instance by it's partial name, e.g. "OpenFile".
///
/// Name partial of the command.
/// The first command by that name or null if not found.
public static ICommand GetCommandInstanceByPartialName(string commandName)
{
string cmdName = commandName + "Command";
foreach (Type cmdType in CommandCache.Keys)
{
if (cmdType.Name.EndsWith(commandName) || cmdType.Name.EndsWith(cmdName))
{
return CommandCache[cmdType];
}
}
return null;
}
}
}