Skip to content

Commit d45ebdd

Browse files
author
Bart Koelman
committed
Prefer IDictionary<,>.TryGetValue() over .Contains() followed by indexer lookup
1 parent fc1a719 commit d45ebdd

File tree

6 files changed

+21
-13
lines changed

6 files changed

+21
-13
lines changed

src/JsonApiDotNetCore/Queries/Internal/Parsing/QueryTokenizer.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ private bool IsMinusInsideText(TokenKind kind)
124124

125125
private static TokenKind? TryGetSingleCharacterTokenKind(char ch)
126126
{
127-
return SingleCharacterToTokenKinds.ContainsKey(ch) ? SingleCharacterToTokenKinds[ch] : null;
127+
return SingleCharacterToTokenKinds.TryGetValue(ch, out TokenKind tokenKind) ? tokenKind : null;
128128
}
129129

130130
private Token? ProduceTokenFromTextBuffer(bool isQuotedText)

src/JsonApiDotNetCore/Queries/Internal/SparseFieldSetCache.cs

+8-6
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,10 @@ public IImmutableSet<ResourceFieldAttribute> GetSparseFieldSetForQuery(ResourceT
7575

7676
if (!_visitedTable.ContainsKey(resourceType))
7777
{
78-
SparseFieldSetExpression? inputExpression = _lazySourceTable.Value.ContainsKey(resourceType)
79-
? new SparseFieldSetExpression(_lazySourceTable.Value[resourceType])
80-
: null;
78+
SparseFieldSetExpression? inputExpression =
79+
_lazySourceTable.Value.TryGetValue(resourceType, out IImmutableSet<ResourceFieldAttribute>? inputFields)
80+
? new SparseFieldSetExpression(inputFields)
81+
: null;
8182

8283
SparseFieldSetExpression? outputExpression = _resourceDefinitionAccessor.OnApplySparseFieldSet(resourceType, inputExpression);
8384

@@ -117,9 +118,10 @@ public IImmutableSet<ResourceFieldAttribute> GetSparseFieldSetForSerializer(Reso
117118

118119
if (!_visitedTable.ContainsKey(resourceType))
119120
{
120-
IImmutableSet<ResourceFieldAttribute> inputFields = _lazySourceTable.Value.ContainsKey(resourceType)
121-
? _lazySourceTable.Value[resourceType]
122-
: GetResourceFields(resourceType);
121+
IImmutableSet<ResourceFieldAttribute> inputFields =
122+
_lazySourceTable.Value.TryGetValue(resourceType, out IImmutableSet<ResourceFieldAttribute>? fields)
123+
? fields
124+
: GetResourceFields(resourceType);
123125

124126
var inputExpression = new SparseFieldSetExpression(inputFields);
125127
SparseFieldSetExpression? outputExpression = _resourceDefinitionAccessor.OnApplySparseFieldSet(resourceType, inputExpression);

src/JsonApiDotNetCore/Resources/ResourceChangeTracker.cs

+1-2
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,8 @@ public bool HasImplicitChanges()
7272
{
7373
foreach (string key in _initiallyStoredAttributeValues.Keys)
7474
{
75-
if (_requestAttributeValues.ContainsKey(key))
75+
if (_requestAttributeValues.TryGetValue(key, out string? requestValue))
7676
{
77-
string requestValue = _requestAttributeValues[key];
7877
string actualValue = _finallyStoredAttributeValues[key];
7978

8079
if (requestValue != actualValue)

src/JsonApiDotNetCore/Resources/ResourceDefinitionAccessor.cs

+9-1
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,15 @@ public IImmutableSet<IncludeElementExpression> OnApplyIncludes(ResourceType reso
8282
dynamic resourceDefinition = ResolveResourceDefinition(resourceClrType);
8383
dynamic handlers = resourceDefinition.OnRegisterQueryableHandlersForQueryStringParameters();
8484

85-
return handlers != null && handlers!.ContainsKey(parameterName) ? handlers![parameterName] : null;
85+
if (handlers != null)
86+
{
87+
if (handlers.ContainsKey(parameterName))
88+
{
89+
return handlers[parameterName];
90+
}
91+
}
92+
93+
return null;
8694
}
8795

8896
/// <inheritdoc />

test/JsonApiDotNetCoreTests/IntegrationTests/InputValidation/RequestBody/WorkflowDefinition.cs

+1-2
Original file line numberDiff line numberDiff line change
@@ -102,9 +102,8 @@ private static void AssertCanTransitionToStage(WorkflowStage fromStage, Workflow
102102

103103
private static bool CanTransitionToStage(WorkflowStage fromStage, WorkflowStage toStage)
104104
{
105-
if (StageTransitionTable.ContainsKey(fromStage))
105+
if (StageTransitionTable.TryGetValue(fromStage, out ICollection<WorkflowStage>? possibleNextStages))
106106
{
107-
ICollection<WorkflowStage> possibleNextStages = StageTransitionTable[fromStage];
108107
return possibleNextStages.Contains(toStage);
109108
}
110109

test/JsonApiDotNetCoreTests/IntegrationTests/MultiTenancy/RouteTenantProvider.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public Guid TenantId
2525
}
2626

2727
string? countryCode = (string?)_httpContextAccessor.HttpContext.Request.RouteValues["countryCode"];
28-
return countryCode != null && TenantRegistry.ContainsKey(countryCode) ? TenantRegistry[countryCode] : Guid.Empty;
28+
return countryCode != null && TenantRegistry.TryGetValue(countryCode, out Guid tenantId) ? tenantId : Guid.Empty;
2929
}
3030
}
3131

0 commit comments

Comments
 (0)