Skip to content

Commit

Permalink
Use GetValueOrDefault and Remove (#34423)
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonCropp committed Aug 13, 2024
1 parent 5a0924f commit def432c
Show file tree
Hide file tree
Showing 24 changed files with 42 additions and 131 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -143,9 +143,7 @@ public virtual void AddNavigationBinding(INavigation navigation, StructuralTypeS
InMemoryStrings.UnableToBindMemberToEntityProjection("navigation", navigation.Name, EntityType.DisplayName()));
}

return _navigationExpressionsCache.TryGetValue(navigation, out var expression)
? expression
: null;
return _navigationExpressionsCache.GetValueOrDefault(navigation);
}

/// <summary>
Expand Down
6 changes: 1 addition & 5 deletions src/EFCore.Relational/Metadata/Internal/CheckConstraint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -122,11 +122,7 @@ public static IEnumerable<IReadOnlyCheckConstraint> GetCheckConstraints(IReadOnl
public static IReadOnlyCheckConstraint? FindDeclaredCheckConstraint(IReadOnlyEntityType entityType, string name)
{
var dataDictionary = GetConstraintsDictionary(entityType);
return dataDictionary == null
? null
: dataDictionary.TryGetValue(name, out var checkConstraint)
? checkConstraint
: null;
return dataDictionary?.GetValueOrDefault(name);
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,7 @@ public virtual void AddParameter(IStoreStoredProcedureParameter parameter)
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public virtual IStoreStoredProcedureParameter? FindParameter(string name)
=> _parametersSet.TryGetValue(name, out var parameter)
? parameter
: null;
=> _parametersSet.GetValueOrDefault(name);

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
Expand Down
12 changes: 3 additions & 9 deletions src/EFCore.Relational/Metadata/Internal/StoredProcedure.cs
Original file line number Diff line number Diff line change
Expand Up @@ -505,9 +505,7 @@ public virtual IReadOnlyList<StoredProcedureParameter> Parameters
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public virtual StoredProcedureParameter? FindParameter(string propertyName)
=> _currentValueParameters.TryGetValue(propertyName, out var parameter)
? parameter
: null;
=> _currentValueParameters.GetValueOrDefault(propertyName);

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
Expand Down Expand Up @@ -538,9 +536,7 @@ public virtual StoredProcedureParameter AddParameter(string propertyName)
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public virtual StoredProcedureParameter? FindOriginalValueParameter(string propertyName)
=> _originalValueParameters.TryGetValue(propertyName, out var parameter)
? parameter
: null;
=> _originalValueParameters.GetValueOrDefault(propertyName);

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
Expand Down Expand Up @@ -616,9 +612,7 @@ public virtual IReadOnlyList<StoredProcedureResultColumn> ResultColumns
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public virtual StoredProcedureResultColumn? FindResultColumn(string propertyName)
=> _propertyResultColumns.TryGetValue(propertyName, out var resultColumn)
? resultColumn
: null;
=> _propertyResultColumns.GetValueOrDefault(propertyName);

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
Expand Down
4 changes: 1 addition & 3 deletions src/EFCore.Relational/Metadata/Internal/Table.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,7 @@ public virtual UniqueConstraint? PrimaryKey
public virtual UniqueConstraint? FindUniqueConstraint(string name)
=> PrimaryKey != null && PrimaryKey.Name == name
? PrimaryKey
: UniqueConstraints.TryGetValue(name, out var constraint)
? constraint
: null;
: UniqueConstraints.GetValueOrDefault(name);

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
Expand Down
4 changes: 1 addition & 3 deletions src/EFCore.Relational/Metadata/Internal/TableBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,7 @@ public override bool IsReadOnly

/// <inheritdoc />
public virtual IColumnBase? FindColumn(string name)
=> Columns.TryGetValue(name, out var column)
? column
: null;
=> Columns.GetValueOrDefault(name);

/// <inheritdoc />
public virtual IColumnBase? FindColumn(IProperty property)
Expand Down
4 changes: 1 addition & 3 deletions src/EFCore.Relational/Metadata/StoreObjectDictionary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@ public class StoreObjectDictionary<T> : IReadOnlyStoreObjectDictionary<T>

/// <inheritdoc />
public virtual T? Find(in StoreObjectIdentifier storeObject)
=> _dictionary.TryGetValue(storeObject, out var value)
? value
: null;
=> _dictionary.GetValueOrDefault(storeObject);

/// <inheritdoc />
public virtual IEnumerable<T> GetValues()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -418,9 +418,7 @@ public virtual void AddNavigationBinding(INavigation navigation, StructuralTypeS
RelationalStrings.UnableToBindMemberToEntityProjection("navigation", navigation.Name, entityType.DisplayName()));
}

return _ownedNavigationMap.TryGetValue(navigation, out var expression)
? expression
: null;
return _ownedNavigationMap.GetValueOrDefault(navigation);
}

/// <inheritdoc />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -473,9 +473,8 @@ private static void GenerateSimpleFluentApiCall(
MethodInfo methodInfo,
List<MethodCallCodeFragment> methodCallCodeFragments)
{
if (annotations.TryGetValue(annotationName, out var annotation))
if (annotations.Remove(annotationName, out var annotation))
{
annotations.Remove(annotationName);
if (annotation.Value is object annotationValue)
{
methodCallCodeFragments.Add(
Expand Down
6 changes: 2 additions & 4 deletions src/EFCore/ChangeTracking/Internal/IdentityMap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public virtual IEnumerable<InternalEntityEntry> All()
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public virtual InternalEntityEntry? TryGetEntryTyped(TKey keyValue)
=> _identityMap.TryGetValue(keyValue, out var entry) ? entry : null;
=> _identityMap.GetValueOrDefault(keyValue);

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
Expand Down Expand Up @@ -141,9 +141,7 @@ public virtual IEnumerable<InternalEntityEntry> All()

try
{
return _identityMap.TryGetValue((TKey)key, out var entry)
? entry
: null;
return _identityMap.GetValueOrDefault((TKey)key);
}
catch (InvalidCastException e)
{
Expand Down
4 changes: 1 addition & 3 deletions src/EFCore/ChangeTracking/Internal/StateManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -812,10 +812,8 @@ public virtual void UpdateReferencedUntrackedEntity(
InternalEntityEntry referencedFromEntry)
{
if (_referencedUntrackedEntities != null
&& _referencedUntrackedEntities.TryGetValue(referencedEntity, out var danglers))
&& _referencedUntrackedEntities.Remove(referencedEntity, out var danglers))
{
_referencedUntrackedEntities.Remove(referencedEntity);

if (!_referencedUntrackedEntities.TryGetValue(newReferencedEntity, out var newDanglers))
{
newDanglers = new List<Tuple<INavigationBase, InternalEntityEntry>>();
Expand Down
12 changes: 2 additions & 10 deletions src/EFCore/Infrastructure/AnnotatableBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -178,11 +178,7 @@ public virtual void SetAnnotation(string name, object? value)
{
Check.NotEmpty(name, nameof(name));

return _annotations == null
? null
: _annotations.TryGetValue(name, out var annotation)
? annotation
: null;
return _annotations?.GetValueOrDefault(name);
}

/// <summary>
Expand Down Expand Up @@ -389,11 +385,7 @@ public virtual TValue GetOrAddRuntimeAnnotationValue<TValue, TArg>(
{
Check.NotEmpty(name, nameof(name));

return _runtimeAnnotations == null
? null
: _runtimeAnnotations.TryGetValue(name, out var annotation)
? annotation
: null;
return _runtimeAnnotations?.GetValueOrDefault(name);
}

/// <summary>
Expand Down
3 changes: 1 addition & 2 deletions src/EFCore/Metadata/Conventions/Internal/MetadataTracker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,8 @@ public virtual void Update(IConventionForeignKey oldForeignKey, IConventionForei
!oldForeignKey.IsInModel && newForeignKey.IsInModel,
$"{nameof(oldForeignKey)} is in the model or {nameof(newForeignKey)} isn't");

if (_trackedForeignKeys.TryGetValue(oldForeignKey, out var reference))
if (_trackedForeignKeys.Remove(oldForeignKey, out var reference))
{
_trackedForeignKeys.Remove(oldForeignKey);
reference.Object = newForeignKey;
_trackedForeignKeys.Add(newForeignKey, reference);
}
Expand Down
32 changes: 8 additions & 24 deletions src/EFCore/Metadata/Internal/EntityType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -817,9 +817,7 @@ public virtual IEnumerable<Key> GetDeclaredKeys()
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public virtual Key? FindDeclaredKey(IReadOnlyList<IReadOnlyProperty> properties)
=> _keys.TryGetValue(Check.NotEmpty(properties, nameof(properties)), out var key)
? key
: null;
=> _keys.GetValueOrDefault(Check.NotEmpty(properties, nameof(properties)));

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
Expand Down Expand Up @@ -1529,9 +1527,7 @@ public virtual Navigation AddNavigation(MemberIdentity navigationMember, Foreign
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public virtual Navigation? FindDeclaredNavigation(string name)
=> _navigations.TryGetValue(Check.NotEmpty(name, nameof(name)), out var navigation)
? navigation
: null;
=> _navigations.GetValueOrDefault(Check.NotEmpty(name, nameof(name)));

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
Expand Down Expand Up @@ -1720,9 +1716,7 @@ public virtual IEnumerable<Navigation> GetNavigations()
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public virtual SkipNavigation? FindDeclaredSkipNavigation(string name)
=> _skipNavigations.TryGetValue(Check.NotEmpty(name, nameof(name)), out var navigation)
? navigation
: null;
=> _skipNavigations.GetValueOrDefault(Check.NotEmpty(name, nameof(name)));

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
Expand Down Expand Up @@ -2084,9 +2078,7 @@ public virtual IEnumerable<Index> GetDerivedIndexes()
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public virtual Index? FindDeclaredIndex(IReadOnlyList<IReadOnlyProperty> properties)
=> _unnamedIndexes.TryGetValue(Check.NotEmpty(properties, nameof(properties)), out var index)
? index
: null;
=> _unnamedIndexes.GetValueOrDefault(Check.NotEmpty(properties, nameof(properties)));

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
Expand All @@ -2095,9 +2087,7 @@ public virtual IEnumerable<Index> GetDerivedIndexes()
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public virtual Index? FindDeclaredIndex(string name)
=> _namedIndexes.TryGetValue(Check.NotEmpty(name, nameof(name)), out var index)
? index
: null;
=> _namedIndexes.GetValueOrDefault(Check.NotEmpty(name, nameof(name)));

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
Expand Down Expand Up @@ -2455,9 +2445,7 @@ public virtual ServiceProperty AddServiceProperty(
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public virtual ServiceProperty? FindDeclaredServiceProperty(string name)
=> _serviceProperties.TryGetValue(Check.NotEmpty(name, nameof(name)), out var property)
? property
: null;
=> _serviceProperties.GetValueOrDefault(Check.NotEmpty(name, nameof(name)));

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
Expand Down Expand Up @@ -2627,9 +2615,7 @@ public virtual IEnumerable<ServiceProperty> GetDerivedServiceProperties()
{
Check.NotEmpty(modelName, nameof(modelName));

return _triggers.TryGetValue(modelName, out var trigger)
? trigger
: null;
return _triggers.GetValueOrDefault(modelName);
}

/// <summary>
Expand All @@ -2653,13 +2639,11 @@ public virtual IEnumerable<Trigger> GetDeclaredTriggers()
Check.DebugAssert(IsInModel, "The entity type has been removed from the model");
EnsureMutable();

if (!_triggers.TryGetValue(modelName, out var trigger))
if (!_triggers.Remove(modelName, out var trigger))
{
return null;
}

_triggers.Remove(modelName);

trigger.SetRemovedFromModel();

return (Trigger?)Model.ConventionDispatcher.OnTriggerRemoved(Builder, trigger);
Expand Down
7 changes: 1 addition & 6 deletions src/EFCore/Metadata/Internal/Model.cs
Original file line number Diff line number Diff line change
Expand Up @@ -845,12 +845,7 @@ public virtual void RemoveComplexType(ComplexType complexType)
return null;
}

if (_propertiesByType.TryGetValue(unwrappedType, out var properties))
{
return properties;
}

return null;
return _propertiesByType.GetValueOrDefault(unwrappedType);
}

/// <summary>
Expand Down
12 changes: 3 additions & 9 deletions src/EFCore/Metadata/Internal/ModelConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -273,9 +273,7 @@ public virtual PropertyConfiguration GetOrAddProperty(Type type)
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public virtual PropertyConfiguration? FindProperty(Type type)
=> _properties.TryGetValue(type, out var property)
? property
: null;
=> _properties.GetValueOrDefault(type);

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
Expand Down Expand Up @@ -322,9 +320,7 @@ public virtual PropertyConfiguration GetOrAddTypeMapping(Type type)
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public virtual PropertyConfiguration? FindTypeMapping(Type type)
=> _typeMappings.TryGetValue(type, out var property)
? property
: null;
=> _typeMappings.GetValueOrDefault(type);

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
Expand Down Expand Up @@ -353,9 +349,7 @@ public virtual ComplexPropertyConfiguration GetOrAddComplexProperty(Type type)
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public virtual ComplexPropertyConfiguration? FindComplexProperty(Type type)
=> _complexProperties.TryGetValue(type, out var property)
? property
: null;
=> _complexProperties.GetValueOrDefault(type);

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
Expand Down
8 changes: 2 additions & 6 deletions src/EFCore/Metadata/Internal/TypeBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -516,9 +516,7 @@ public virtual void UpdateConfigurationSource(ConfigurationSource configurationS
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public virtual Property? FindDeclaredProperty(string name)
=> _properties.TryGetValue(Check.NotEmpty(name, nameof(name)), out var property)
? property
: null;
=> _properties.GetValueOrDefault(Check.NotEmpty(name, nameof(name)));

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
Expand Down Expand Up @@ -956,9 +954,7 @@ public virtual IReadOnlyDictionary<string, FieldInfo> GetRuntimeFields()
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public virtual ComplexProperty? FindDeclaredComplexProperty(string name)
=> _complexProperties.TryGetValue(Check.NotEmpty(name, nameof(name)), out var property)
? property
: null;
=> _complexProperties.GetValueOrDefault(Check.NotEmpty(name, nameof(name)));

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
Expand Down
6 changes: 1 addition & 5 deletions src/EFCore/Metadata/RuntimeAnnotatableBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -318,11 +318,7 @@ public virtual TValue GetOrAddRuntimeAnnotationValue<TValue, TArg>(
{
Check.NotEmpty(name, nameof(name));

return _runtimeAnnotations == null
? null
: _runtimeAnnotations.TryGetValue(name, out var annotation)
? annotation
: null;
return _runtimeAnnotations?.GetValueOrDefault(name);
}

/// <summary>
Expand Down
4 changes: 1 addition & 3 deletions src/EFCore/Metadata/RuntimeEntityType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -397,9 +397,7 @@ public virtual RuntimeNavigation AddNavigation(
=> (RuntimeNavigation?)((IReadOnlyEntityType)this).FindNavigation(name);

private RuntimeNavigation? FindDeclaredNavigation(string name)
=> _navigations.TryGetValue(name, out var navigation)
? navigation
: null;
=> _navigations.GetValueOrDefault(name);

private IEnumerable<RuntimeNavigation> GetDeclaredNavigations()
=> _navigations.Values;
Expand Down
8 changes: 2 additions & 6 deletions src/EFCore/Metadata/RuntimeModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -212,9 +212,7 @@ public virtual IEnumerable<IReadOnlyEntityType> GetAdHocEntityTypes()
/// <param name="name">The name of the entity type to find.</param>
/// <returns>The entity type, or <see langword="null" /> if none is found.</returns>
public virtual RuntimeEntityType? FindEntityType(string name)
=> _entityTypes.TryGetValue(name, out var entityType)
? entityType
: null;
=> _entityTypes.GetValueOrDefault(name);

/// <summary>
/// Gets the entity type with the given name. Returns <see langword="null" /> if no entity type with the given name has been
Expand All @@ -223,9 +221,7 @@ public virtual IEnumerable<IReadOnlyEntityType> GetAdHocEntityTypes()
/// <param name="clrType">The CLR type of the entity type to find.</param>
/// <returns>The entity type, or <see langword="null" /> if none is found.</returns>
public virtual RuntimeEntityType? FindAdHocEntityType(Type clrType)
=> _adHocEntityTypes.TryGetValue(clrType, out var entityType)
? entityType
: null;
=> _adHocEntityTypes.GetValueOrDefault(clrType);

private RuntimeEntityType? FindEntityType(Type type)
=> FindEntityType(GetDisplayName(type));
Expand Down
Loading

0 comments on commit def432c

Please sign in to comment.