You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
118 lines
2.8 KiB
C#
118 lines
2.8 KiB
C#
namespace DS.Module.Core.Data
|
|
{
|
|
/// <summary>
|
|
/// 任务模块之间用于传入、获取数据的容器
|
|
/// </summary>
|
|
public class TaskFlowDataContext
|
|
{
|
|
private readonly Dictionary<string, object?> dataContext = new();
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
public TaskFlowDataContext(string key, object? value)
|
|
{
|
|
dataContext.Add(key, value);
|
|
}
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
public TaskFlowDataContext(params (string key, object? value)[] keyValues)
|
|
{
|
|
foreach (var item in keyValues)
|
|
{
|
|
dataContext.Add(item.key, item.value);
|
|
}
|
|
}
|
|
|
|
public IReadOnlyCollection<string> Keys => dataContext.Keys;
|
|
|
|
public object? this[string key]
|
|
{
|
|
get { return dataContext[key]; }
|
|
set { dataContext[key] = value; }
|
|
}
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
public void Set(string key, object? value)
|
|
{
|
|
if (ContainsKey(key))
|
|
{
|
|
dataContext[key] = value;
|
|
}
|
|
else
|
|
{
|
|
dataContext.Add(key, value);
|
|
}
|
|
}
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
public T? Get<T>(string key)
|
|
{
|
|
if (dataContext.ContainsKey(key))
|
|
{
|
|
var value = dataContext[key];
|
|
if (value is T t)
|
|
{
|
|
return t;
|
|
}
|
|
else if (value != null)
|
|
{
|
|
return (T)Convert.ChangeType(value, typeof(T));
|
|
}
|
|
}
|
|
return default;
|
|
}
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
public T? GetOrDefault<T>(string key)
|
|
{
|
|
return GetOrDefault<T>(key, default);
|
|
}
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
public T GetOrDefault<T>(string key, T defaultValue)
|
|
{
|
|
if (dataContext.TryGetValue(key, out var value))
|
|
{
|
|
if (value is T t)
|
|
{
|
|
return t;
|
|
}
|
|
else if (value != null)
|
|
{
|
|
return (T)Convert.ChangeType(value, typeof(T));
|
|
}
|
|
}
|
|
|
|
return defaultValue;
|
|
}
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="key"></param>
|
|
/// <param name="value"></param>
|
|
/// <returns></returns>
|
|
public bool TryGetValue(string key, out object value)
|
|
{
|
|
return TryGetValue(key, out value);
|
|
}
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
public bool ContainsKey(string key) => dataContext.ContainsKey(key);
|
|
|
|
|
|
}
|
|
}
|