diff --git a/src/EFCore.Relational/Query/Internal/RelationalCommandCache.cs b/src/EFCore.Relational/Query/Internal/RelationalCommandCache.cs index 383e21048ec..7a891bc16b4 100644 --- a/src/EFCore.Relational/Query/Internal/RelationalCommandCache.cs +++ b/src/EFCore.Relational/Query/Internal/RelationalCommandCache.cs @@ -1,6 +1,7 @@ // Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. +using System; using System.Collections; using System.Collections.Concurrent; using System.Collections.Generic; @@ -117,7 +118,7 @@ void IPrintableExpression.Print(ExpressionPrinter expressionPrinter) } } - private readonly struct CommandCacheKey + private readonly struct CommandCacheKey : IEquatable { private readonly SelectExpression _selectExpression; private readonly IReadOnlyDictionary _parameterValues; @@ -129,11 +130,10 @@ public CommandCacheKey(SelectExpression selectExpression, IReadOnlyDictionary obj != null - && obj is CommandCacheKey commandCacheKey + => obj is CommandCacheKey commandCacheKey && Equals(commandCacheKey); - private bool Equals(CommandCacheKey commandCacheKey) + public bool Equals(CommandCacheKey commandCacheKey) { if (!ReferenceEquals(_selectExpression, commandCacheKey._selectExpression)) { diff --git a/src/EFCore.Relational/Query/RelationalCompiledQueryCacheKeyGenerator.cs b/src/EFCore.Relational/Query/RelationalCompiledQueryCacheKeyGenerator.cs index 1af8f827c6e..dd2aa4ec272 100644 --- a/src/EFCore.Relational/Query/RelationalCompiledQueryCacheKeyGenerator.cs +++ b/src/EFCore.Relational/Query/RelationalCompiledQueryCacheKeyGenerator.cs @@ -84,7 +84,7 @@ public override object GenerateCacheKey(Expression query, bool async) /// not used in application code. /// /// - protected readonly struct RelationalCompiledQueryCacheKey + protected readonly struct RelationalCompiledQueryCacheKey : IEquatable { private readonly CompiledQueryCacheKey _compiledQueryCacheKey; private readonly bool _useRelationalNulls; @@ -119,11 +119,19 @@ public RelationalCompiledQueryCacheKey( /// otherwise . /// public override bool Equals(object obj) - => !(obj is null) - && obj is RelationalCompiledQueryCacheKey key + => obj is RelationalCompiledQueryCacheKey key && Equals(key); - private bool Equals(RelationalCompiledQueryCacheKey other) + /// + /// Indicates whether the current object is equal to another object of the same type. + /// + /// + /// An object to compare with this object. + /// + /// + /// if the current object is equal to the parameter; otherwise, . + /// + public bool Equals(RelationalCompiledQueryCacheKey other) => _compiledQueryCacheKey.Equals(other._compiledQueryCacheKey) && _useRelationalNulls == other._useRelationalNulls && _querySplittingBehavior == other._querySplittingBehavior diff --git a/src/EFCore.Relational/Storage/TypedRelationalValueBufferFactoryFactory.cs b/src/EFCore.Relational/Storage/TypedRelationalValueBufferFactoryFactory.cs index 5333dec2268..e01255310b6 100644 --- a/src/EFCore.Relational/Storage/TypedRelationalValueBufferFactoryFactory.cs +++ b/src/EFCore.Relational/Storage/TypedRelationalValueBufferFactoryFactory.cs @@ -72,7 +72,7 @@ public TypedRelationalValueBufferFactoryFactory([NotNull] RelationalValueBufferF /// protected virtual RelationalValueBufferFactoryDependencies Dependencies { get; } - private readonly struct CacheKey + private readonly struct CacheKey : IEquatable { public CacheKey(IReadOnlyList materializationInfo) => TypeMaterializationInfo = materializationInfo; @@ -82,7 +82,7 @@ public CacheKey(IReadOnlyList materializationInfo) public override bool Equals(object obj) => obj is CacheKey cacheKey && Equals(cacheKey); - private bool Equals(CacheKey other) + public bool Equals(CacheKey other) => TypeMaterializationInfo.SequenceEqual(other.TypeMaterializationInfo); public override int GetHashCode() diff --git a/src/EFCore.SqlServer/Query/Internal/SqlServerCompiledQueryCacheKeyGenerator.cs b/src/EFCore.SqlServer/Query/Internal/SqlServerCompiledQueryCacheKeyGenerator.cs index b59acb174d6..e82baa3c1c7 100644 --- a/src/EFCore.SqlServer/Query/Internal/SqlServerCompiledQueryCacheKeyGenerator.cs +++ b/src/EFCore.SqlServer/Query/Internal/SqlServerCompiledQueryCacheKeyGenerator.cs @@ -48,7 +48,7 @@ public override object GenerateCacheKey(Expression query, bool async) GenerateCacheKeyCore(query, async), _sqlServerConnection.IsMultipleActiveResultSetsEnabled); - private readonly struct SqlServerCompiledQueryCacheKey + private readonly struct SqlServerCompiledQueryCacheKey : IEquatable { private readonly RelationalCompiledQueryCacheKey _relationalCompiledQueryCacheKey; private readonly bool _multipleActiveResultSetsEnabled; @@ -61,11 +61,10 @@ public SqlServerCompiledQueryCacheKey( } public override bool Equals(object obj) - => !(obj is null) - && obj is SqlServerCompiledQueryCacheKey sqlServerCompiledQueryCacheKey + => obj is SqlServerCompiledQueryCacheKey sqlServerCompiledQueryCacheKey && Equals(sqlServerCompiledQueryCacheKey); - private bool Equals(SqlServerCompiledQueryCacheKey other) + public bool Equals(SqlServerCompiledQueryCacheKey other) => _relationalCompiledQueryCacheKey.Equals(other._relationalCompiledQueryCacheKey) && _multipleActiveResultSetsEnabled == other._multipleActiveResultSetsEnabled; diff --git a/src/EFCore/Query/CompiledQueryCacheKeyGenerator.cs b/src/EFCore/Query/CompiledQueryCacheKeyGenerator.cs index bf90df47950..5efd37c3dcb 100644 --- a/src/EFCore/Query/CompiledQueryCacheKeyGenerator.cs +++ b/src/EFCore/Query/CompiledQueryCacheKeyGenerator.cs @@ -76,7 +76,7 @@ protected CompiledQueryCacheKey GenerateCacheKeyCore([NotNull] Expression query, /// not used in application code. /// /// - protected readonly struct CompiledQueryCacheKey + protected readonly struct CompiledQueryCacheKey : IEquatable { private readonly Expression _query; private readonly IModel _model; @@ -112,15 +112,19 @@ public CompiledQueryCacheKey( /// if the object is a and is for the same query, otherwise . /// public override bool Equals(object obj) - { - if (obj is null - || !(obj is CompiledQueryCacheKey)) - { - return false; - } - - var other = (CompiledQueryCacheKey)obj; + => obj is CompiledQueryCacheKey other && Equals(other); + /// + /// Indicates whether the current object is equal to another object of the same type. + /// + /// + /// An object to compare with this object. + /// + /// + /// if the current object is equal to the parameter; otherwise, . + /// + public bool Equals(CompiledQueryCacheKey other) + { return ReferenceEquals(_model, other._model) && _queryTrackingBehavior == other._queryTrackingBehavior && _async == other._async diff --git a/src/EFCore/Storage/ValueBuffer.cs b/src/EFCore/Storage/ValueBuffer.cs index 885cbcd460e..720c9cdc267 100644 --- a/src/EFCore/Storage/ValueBuffer.cs +++ b/src/EFCore/Storage/ValueBuffer.cs @@ -19,7 +19,7 @@ namespace Microsoft.EntityFrameworkCore.Storage /// not used in application code. /// /// - public readonly struct ValueBuffer + public readonly struct ValueBuffer : IEquatable { /// /// A buffer with no values in it. @@ -79,7 +79,16 @@ public override bool Equals(object obj) && obj is ValueBuffer buffer && Equals(buffer); - private bool Equals(ValueBuffer other) + /// + /// Indicates whether the current object is equal to another object of the same type. + /// + /// + /// An object to compare with this object. + /// + /// + /// if the current object is equal to the parameter; otherwise, . + /// + public bool Equals(ValueBuffer other) { if (_values.Length != other._values.Length) { diff --git a/src/EFCore/ValueGeneration/ValueGeneratorCache.cs b/src/EFCore/ValueGeneration/ValueGeneratorCache.cs index a67274f2bae..a13269a0755 100644 --- a/src/EFCore/ValueGeneration/ValueGeneratorCache.cs +++ b/src/EFCore/ValueGeneration/ValueGeneratorCache.cs @@ -38,7 +38,7 @@ public ValueGeneratorCache([NotNull] ValueGeneratorCacheDependencies dependencie private readonly ConcurrentDictionary _cache = new ConcurrentDictionary(); - private readonly struct CacheKey + private readonly struct CacheKey : IEquatable { public CacheKey(IProperty property, IEntityType entityType, Func factory) { @@ -53,13 +53,11 @@ public CacheKey(IProperty property, IEntityType entityType, Func Factory { get; } - private bool Equals(CacheKey other) + public bool Equals(CacheKey other) => Property.Equals(other.Property) && EntityType.Equals(other.EntityType); public override bool Equals(object obj) - { - return obj is null ? false : obj is CacheKey cacheKey && Equals(cacheKey); - } + => obj is CacheKey cacheKey && Equals(cacheKey); public override int GetHashCode() => HashCode.Combine(Property, EntityType); }