From b2e3b9b038ba3dc56b8956924aafb4fd3464528f Mon Sep 17 00:00:00 2001 From: Mikhail Preyskurantov <5574159+mpreyskurantov@users.noreply.github.com> Date: Mon, 9 Sep 2024 17:53:03 +0300 Subject: [PATCH] Mitigate violation CA1854 (#636) --- net/DevExtreme.AspNet.Data/Aggregation/SumFix.cs | 7 ++++--- net/DevExtreme.AspNet.Data/GroupHelper.cs | 6 +++--- net/DevExtreme.AspNet.Data/Helpers/DefaultAccessor.cs | 10 +++++----- 3 files changed, 12 insertions(+), 11 deletions(-) diff --git a/net/DevExtreme.AspNet.Data/Aggregation/SumFix.cs b/net/DevExtreme.AspNet.Data/Aggregation/SumFix.cs index 7d11c0f2..ea7ea2a7 100644 --- a/net/DevExtreme.AspNet.Data/Aggregation/SumFix.cs +++ b/net/DevExtreme.AspNet.Data/Aggregation/SumFix.cs @@ -51,13 +51,14 @@ object GetDefaultValue(string selector) { if(_defaultValues == null) _defaultValues = new Dictionary(); - if(!_defaultValues.ContainsKey(selector)) { + object value; + if(!_defaultValues.TryGetValue(selector, out value)) { var expr = CompileAccessorExpression(CreateItemParam(), selector); var acc = AccumulatorFactory.Create(Utils.StripNullableType(expr.Type)); - _defaultValues[selector] = acc.GetValue(); + _defaultValues.Add(selector, value = acc.GetValue()); } - return _defaultValues[selector]; + return value; } } diff --git a/net/DevExtreme.AspNet.Data/GroupHelper.cs b/net/DevExtreme.AspNet.Data/GroupHelper.cs index c037d401..559e8402 100644 --- a/net/DevExtreme.AspNet.Data/GroupHelper.cs +++ b/net/DevExtreme.AspNet.Data/GroupHelper.cs @@ -40,13 +40,13 @@ List Group(IEnumerable data, GroupingInfo groupInfo) { var groupKey = GetKey(item, groupInfo); var groupIndexKey = groupKey ?? NULL_KEY; - if(!groupsIndex.ContainsKey(groupIndexKey)) { + Group group; + if(!groupsIndex.TryGetValue(groupIndexKey, out group)) { var newGroup = new Group { key = groupKey }; - groupsIndex[groupIndexKey] = newGroup; + groupsIndex.Add(groupIndexKey, group = newGroup); groups.Add(newGroup); } - var group = groupsIndex[groupIndexKey]; if(group.items == null) group.items = new List(); group.items.Add(item); diff --git a/net/DevExtreme.AspNet.Data/Helpers/DefaultAccessor.cs b/net/DevExtreme.AspNet.Data/Helpers/DefaultAccessor.cs index 72dadd1e..f3d29904 100644 --- a/net/DevExtreme.AspNet.Data/Helpers/DefaultAccessor.cs +++ b/net/DevExtreme.AspNet.Data/Helpers/DefaultAccessor.cs @@ -16,16 +16,16 @@ public object Read(T obj, string selector) { if(_accessors == null) _accessors = new Dictionary>(); - if(!_accessors.ContainsKey(selector)) { + Func func; + if(!_accessors.TryGetValue(selector, out func)) { var param = CreateItemParam(); - - _accessors[selector] = Expression.Lambda>( + _accessors.Add(selector, func = Expression.Lambda>( Expression.Convert(CompileAccessorExpression(param, selector), typeof(Object)), param - ).Compile(); + ).Compile()); } - return _accessors[selector](obj); + return func(obj); } }