BaseFactory: Generic base factory.

This commit is contained in:
2020-06-08 01:13:36 +02:00
parent db7df7123f
commit 7634feebb9
8 changed files with 85 additions and 95 deletions

View File

@@ -0,0 +1,73 @@
using System;
using System.Collections.Generic;
using System.Linq;
namespace VAR.Toolbox.Code
{
public abstract class BaseFactory<T> where T : INamed
{
private static Dictionary<string, Type> _dictTypes = null;
private static Dictionary<string, Type> GetDict()
{
if (_dictTypes != null)
{
return _dictTypes;
}
Type iType = typeof(T);
IEnumerable<Type> types = ReflectionUtils.GetTypesOfInterface(iType);
_dictTypes = types.ToDictionary(t =>
{
T type = (T)System.Runtime.Serialization.FormatterServices.GetUninitializedObject(t);
return type.Name;
});
return _dictTypes;
}
public static string[] GetNames()
{
Dictionary<string, Type> dict = GetDict();
return dict.Select(p => p.Key).ToArray();
}
public static T CreateFromName(string name)
{
Dictionary<string, Type> dict = GetDict();
if (dict.ContainsKey(name) == false)
{
throw new NotImplementedException(string.Format("Cant create {1} with this name: {0}", name, typeof(T).Name));
}
Type type = dict[name];
T instance = (T)Activator.CreateInstance(type);
return instance;
}
public static T CreateFromConfig(string config)
{
Dictionary<string, Type> dict = GetDict();
int indexOfColon = config.IndexOf(':');
string name = config.Substring(0, indexOfColon < 0 ? config.Length : indexOfColon);
string nextConfig = config.Substring(indexOfColon + 1);
if (string.IsNullOrEmpty(name))
{
return (T)(object)null;
}
if (dict.ContainsKey(name) == false)
{
throw new NotImplementedException(string.Format("Cant create {1} with this config: {0}", config, typeof(T).Name));
}
Type type = dict[name];
T instance = (T)Activator.CreateInstance(type, new object[] { nextConfig });
return instance;
}
}
public interface INamed
{
string Name { get; }
}
}

View File

@@ -1,9 +1,7 @@
namespace VAR.Toolbox.Code.ProxyCmdExecutors
{
public interface IProxyCmdExecutor
public interface IProxyCmdExecutor : INamed
{
string Name { get; }
bool ExecuteCmd(string cmd, IOutputHandler outputHandler);
}
}

View File

@@ -1,50 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
namespace VAR.Toolbox.Code.ProxyCmdExecutors
namespace VAR.Toolbox.Code.ProxyCmdExecutors
{
public class ProxyCmdExecutorFactory
public class ProxyCmdExecutorFactory : BaseFactory<IProxyCmdExecutor>
{
private static Dictionary<string, Type> _dictProxyCmdExecutors = null;
private static Dictionary<string, Type> GetDict()
{
if (_dictProxyCmdExecutors != null)
{
return _dictProxyCmdExecutors;
}
Type iTextCoder = typeof(IProxyCmdExecutor);
IEnumerable<Type> toolFormTypes = ReflectionUtils.GetTypesOfInterface(iTextCoder);
_dictProxyCmdExecutors = toolFormTypes.ToDictionary(t =>
{
IProxyCmdExecutor proxyCmdExecutor = System.Runtime.Serialization.FormatterServices.GetUninitializedObject(t) as IProxyCmdExecutor;
return proxyCmdExecutor.Name;
});
return _dictProxyCmdExecutors;
}
public static IProxyCmdExecutor CreateFromConfig(string config)
{
Dictionary<string, Type> dict = GetDict();
int indexOfColon = config.IndexOf(':');
string name = config.Substring(0, indexOfColon < 0 ? config.Length : indexOfColon);
string nextConfig = config.Substring(indexOfColon + 1);
if (string.IsNullOrEmpty(name))
{
return new ProxyCmdExecutorDummy(string.Empty);
}
if (dict.ContainsKey(name) == false)
{
throw new NotImplementedException(string.Format("Cant create IProxyCmdExecutor with this config: {0}", config));
}
Type proxyCmdExecutorType = dict[name];
IProxyCmdExecutor proxyCmdExecutor = Activator.CreateInstance(proxyCmdExecutorType, new object[] { nextConfig }) as IProxyCmdExecutor;
return proxyCmdExecutor;
}
}
}

View File

@@ -1,9 +1,7 @@
namespace VAR.Toolbox.Code.TextCoders
{
public interface ITextCoder
public interface ITextCoder : INamed
{
string Name { get; }
bool NeedsKey { get; }
string Encode(string input, string key);

View File

@@ -1,46 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
namespace VAR.Toolbox.Code.TextCoders
namespace VAR.Toolbox.Code.TextCoders
{
public class TextCoderFactory
public class TextCoderFactory : BaseFactory<ITextCoder>
{
private static Dictionary<string, Type> _dictTextCoderTypes = null;
private static Dictionary<string, Type> GetDict()
{
if (_dictTextCoderTypes != null)
{
return _dictTextCoderTypes;
}
Type iTextCoder = typeof(ITextCoder);
IEnumerable<Type> toolFormTypes = ReflectionUtils.GetTypesOfInterface(iTextCoder);
_dictTextCoderTypes = toolFormTypes.ToDictionary(t =>
{
ITextCoder textCoder = System.Runtime.Serialization.FormatterServices.GetUninitializedObject(t) as ITextCoder;
return textCoder.Name;
});
return _dictTextCoderTypes;
}
public static string[] GetSupportedCoders()
{
Dictionary<string, Type> dict = GetDict();
return dict.Select(p => p.Key).ToArray();
}
public static ITextCoder CreateFromName(string name)
{
Dictionary<string, Type> dict = GetDict();
if (dict.ContainsKey(name) == false)
{
throw new NotImplementedException(string.Format("Cant create ITextCoder with this name: {0}", name));
}
Type textCoder = dict[name];
return Activator.CreateInstance(textCoder) as ITextCoder;
}
}
}

View File

@@ -14,7 +14,7 @@ namespace VAR.Toolbox.UI
{
InitializeComponent();
cboCode.Items.AddRange(TextCoderFactory.GetSupportedCoders());
cboCode.Items.AddRange(TextCoderFactory.GetNames());
cboCode.SelectedIndex = 1;
}

View File

@@ -129,6 +129,10 @@ namespace VAR.Toolbox.UI
if (_proxyCmdExecutor == null)
{
_proxyCmdExecutor = ProxyCmdExecutorFactory.CreateFromConfig(GetCurrentConfig());
if (_proxyCmdExecutor == null)
{
_proxyCmdExecutor = new ProxyCmdExecutorDummy(string.Empty);
}
}
}
private void CleanProxyCmdExecutor()

View File

@@ -73,6 +73,7 @@
<Compile Include="..\VAR.Json\VAR.Json\ParserContext.cs">
<Link>Code\Json\ParserContext.cs</Link>
</Compile>
<Compile Include="Code\BaseFactory.cs" />
<Compile Include="Code\EventDispatcher.cs" />
<Compile Include="Code\HexUtils.cs" />
<Compile Include="Code\IEventListener.cs" />