From 67c8e9bc0e9528d0a38ba837ff7cddeb265ba5d2 Mon Sep 17 00:00:00 2001 From: Neil Bostrom Date: Tue, 14 May 2019 22:44:35 +0100 Subject: [PATCH] Refactored GetConstraints to use IEnumerable to reduce allocations Also made configurationSource a private field --- .../RelationalEntityTypeExtensions.cs | 2 +- .../Metadata/Internal/CheckConstraint.cs | 22 +++++++------------ 2 files changed, 9 insertions(+), 15 deletions(-) diff --git a/src/EFCore.Relational/Extensions/RelationalEntityTypeExtensions.cs b/src/EFCore.Relational/Extensions/RelationalEntityTypeExtensions.cs index bf06d89607b..313b98e7318 100644 --- a/src/EFCore.Relational/Extensions/RelationalEntityTypeExtensions.cs +++ b/src/EFCore.Relational/Extensions/RelationalEntityTypeExtensions.cs @@ -411,7 +411,7 @@ public static bool RemoveCheckConstraint( /// Returns all contained in the entity type. /// /// The entity type to get the check constraints for. - public static IReadOnlyList GetCheckConstraints([NotNull] this IEntityType entityType) + public static IEnumerable GetCheckConstraints([NotNull] this IEntityType entityType) => CheckConstraint.GetCheckConstraints(entityType); } } diff --git a/src/EFCore.Relational/Metadata/Internal/CheckConstraint.cs b/src/EFCore.Relational/Metadata/Internal/CheckConstraint.cs index face3b93b7a..63d2fce7615 100644 --- a/src/EFCore.Relational/Metadata/Internal/CheckConstraint.cs +++ b/src/EFCore.Relational/Metadata/Internal/CheckConstraint.cs @@ -19,6 +19,8 @@ namespace Microsoft.EntityFrameworkCore.Metadata.Internal /// public class CheckConstraint : IConventionCheckConstraint { + private ConfigurationSource _configurationSource; + /// /// This is an internal API that supports the Entity Framework Core infrastructure and not subject to /// the same compatibility standards as public APIs. It may be changed or removed without notice in @@ -38,7 +40,7 @@ public CheckConstraint( EntityType = entityType; Name = name; Sql = sql; - ConfigurationSource = configurationSource; + _configurationSource = configurationSource; var dataDictionary = GetAnnotationsDictionary(EntityType); if (dataDictionary == null) @@ -63,11 +65,11 @@ public CheckConstraint( /// any release. You should only use it directly in your code with extreme caution and knowing that /// doing so can result in application failures when updating to a new Entity Framework Core release. /// - public static IReadOnlyList GetCheckConstraints([NotNull] IEntityType entityType) + public static IEnumerable GetCheckConstraints([NotNull] IEntityType entityType) { Check.NotNull(entityType, nameof(entityType)); - return GetAnnotationsDictionary(entityType)?.Values.ToList() ?? new List(); + return GetAnnotationsDictionary(entityType)?.Values ?? Enumerable.Empty(); } /// @@ -133,15 +135,7 @@ public static bool RemoveCheckConstraint( /// any release. You should only use it directly in your code with extreme caution and knowing that /// doing so can result in application failures when updating to a new Entity Framework Core release. /// - public virtual ConfigurationSource ConfigurationSource { get; private set; } - - /// - /// This is an internal API that supports the Entity Framework Core infrastructure and not subject to - /// the same compatibility standards as public APIs. It may be changed or removed without notice in - /// any release. You should only use it directly in your code with extreme caution and knowing that - /// doing so can result in application failures when updating to a new Entity Framework Core release. - /// - public virtual ConfigurationSource GetConfigurationSource() => ConfigurationSource; + public virtual ConfigurationSource GetConfigurationSource() => _configurationSource; /// /// This is an internal API that supports the Entity Framework Core infrastructure and not subject to @@ -150,8 +144,8 @@ public static bool RemoveCheckConstraint( /// doing so can result in application failures when updating to a new Entity Framework Core release. /// public virtual void UpdateConfigurationSource(ConfigurationSource configurationSource) - { - ConfigurationSource = configurationSource.Max(ConfigurationSource); + { + _configurationSource = configurationSource.Max(_configurationSource); } private static Dictionary GetAnnotationsDictionary(IEntityType entityType)