C# 具有自定义扩展名的字典或ConcurrentDictionary
我编写了一个方法,在整个过程中收集错误(而不是在第一个错误上失败)。这更多是为了验证。它是这样工作的C# 具有自定义扩展名的字典或ConcurrentDictionary,c#,dictionary,C#,Dictionary,我编写了一个方法,在整个过程中收集错误(而不是在第一个错误上失败)。这更多是为了验证。它是这样工作的 var errors = new Dictionary<string, string[]>(); // Do some work, and if error errors.AddOrUpdate(nameof(object.Property), "Specific error message."); 使用helper方法: public static strin
var errors = new Dictionary<string, string[]>();
// Do some work, and if error
errors.AddOrUpdate(nameof(object.Property), "Specific error message.");
使用helper方法:
public static string[] AddStringToArray(string[] values, string newValue)
{
var valuesList = values.ToList();
valuesList.Add(newValue);
return valuesList.ToArray();
}
虽然我看到了
ConcurrentDictionary
允许函数的强大功能,但我很难看到它在这种情况下是如何改进的。我觉得我的实现更直观
我错过什么了吗?或者在这种情况下这更像是一个偏好问题?ConcurrentDictionary是线程安全的,因此当多个线程同时尝试写入同一个字典时,不会出现异常。 您可以使用列表字典,这样就不需要从数组到列表再到列表进行转换。 以下是一个通用实现:
public static TList AddToList<TKey, TValue, TList>(this IDictionary<TKey, TList> target, TKey key, params TValue[] values)
where TList : ICollection<TValue>, new()
{
TList theReturn;
lock (target)
{
if (!target.TryGetValue(key, out theReturn))
{
theReturn = new TList();
target.Add(key, theReturn);
}
}
lock (theReturn)
{
foreach (var currentValue in values)
{
theReturn.Add(currentValue);
}
}
return theReturn;
}
publicstatictlistaddtolist(此IDictionary目标、TKey键、参数TValue[]值)
其中TList:ICollection,new()
{
t列出该报告;
锁定(目标)
{
如果(!target.TryGetValue(键,向外返回))
{
theReturn=new TList();
目标。添加(键,返回);
}
}
锁(返回)
{
foreach(值中的var currentValue)
{
theReturn.Add(当前值);
}
}
返回,返回;
}
如果您正在运行从不同线程添加/删除的操作,则
ConcurrentDictionary
非常有用。如果验证在单个线程中运行,则可以保留字典
建议:将字典类型更改为dictionary
,这样可以避免重复的错误(如果有),并且它的性能会更好(目前正在连接字符串数组)
这是一个扩展:
public static class DictionaryExtensions
{
public static void AddValidationError(
this IDictionary<string, HashSet<string>> dictionary,
[NotNull] string property,
[NotNull] string error)
{
if (!dictionary.TryGetValue(property, out var errors))
errors = new HashSet<string>();
errors.Add(error);
}
}
公共静态类字典扩展
{
公共静态无效AddValidationError(
这本词典,
[NotNull]字符串属性,
[NotNull]字符串错误)
{
if(!dictionary.TryGetValue(属性,输出变量错误))
errors=newhashset();
错误。添加(错误);
}
}
ConcurrentDictionary
是线程安全的,您有多线程应用程序吗?您没有遗漏任何内容。词典里有人叫嚣要改进词典。ConcurrentDictionary必须有这样一种方法来确保线程安全。但这不是免费的,联锁是昂贵的。所以不,这不是个好主意。
public static TList AddToList<TKey, TValue, TList>(this IDictionary<TKey, TList> target, TKey key, params TValue[] values)
where TList : ICollection<TValue>, new()
{
TList theReturn;
lock (target)
{
if (!target.TryGetValue(key, out theReturn))
{
theReturn = new TList();
target.Add(key, theReturn);
}
}
lock (theReturn)
{
foreach (var currentValue in values)
{
theReturn.Add(currentValue);
}
}
return theReturn;
}
public static class DictionaryExtensions
{
public static void AddValidationError(
this IDictionary<string, HashSet<string>> dictionary,
[NotNull] string property,
[NotNull] string error)
{
if (!dictionary.TryGetValue(property, out var errors))
errors = new HashSet<string>();
errors.Add(error);
}
}