Skip to content

Commit

Permalink
Consolidate AddProperty methods
Browse files Browse the repository at this point in the history
React to API review feedback
Add a way to obsolete event definitions

Part of dotnet#15662
  • Loading branch information
AndriySvyryd committed Jun 6, 2019
1 parent 4aba774 commit 9e0fb47
Show file tree
Hide file tree
Showing 20 changed files with 445 additions and 272 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
using Microsoft.EntityFrameworkCore.Utilities;
using Microsoft.Extensions.DependencyInjection;

// ReSharper disable once CheckNamespace
namespace Microsoft.EntityFrameworkCore.Migrations
{
/// <summary>
Expand Down
1 change: 1 addition & 0 deletions src/EFCore.SqlServer/SqlServerRetryingExecutionStrategy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal;
using Microsoft.EntityFrameworkCore.Storage;

// ReSharper disable once CheckNamespace
namespace Microsoft.EntityFrameworkCore
{
/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -349,8 +349,7 @@ private IReadOnlyList<MigrationCommand> CreateDropCommands()
}
};

var masterCommands = Dependencies.MigrationsSqlGenerator.Generate(operations);
return masterCommands;
return Dependencies.MigrationsSqlGenerator.Generate(operations);
}

// Clear connection pools in case there are active connections that are pooled
Expand Down
53 changes: 45 additions & 8 deletions src/EFCore/Extensions/ConventionEntityTypeExtensions.cs
Original file line number Diff line number Diff line change
@@ -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.Generic;
using System.Linq.Expressions;
using System.Reflection;
Expand Down Expand Up @@ -441,20 +442,56 @@ public static IConventionProperty FindDeclaredProperty([NotNull] this IConventio
/// Adds a property to this entity type.
/// </summary>
/// <param name="entityType"> The entity type to add the property to. </param>
/// <param name="propertyInfo"> The corresponding property in the entity class. </param>
/// <param name="memberInfo"> The corresponding member on the entity class. </param>
/// <param name="fromDataAnnotation"> Indicates whether the configuration was specified using a data annotation. </param>
/// <returns> The newly created property. </returns>
public static IConventionProperty AddProperty(
[NotNull] this IConventionEntityType entityType,
[NotNull] PropertyInfo propertyInfo,
[NotNull] MemberInfo memberInfo,
bool fromDataAnnotation = false)
{
Check.NotNull(entityType, nameof(entityType));
Check.NotNull(propertyInfo, nameof(propertyInfo));
=> Check.NotNull(entityType, nameof(entityType)).AddProperty(memberInfo.GetSimpleMemberName(), memberInfo.GetMemberType(),
memberInfo, setTypeConfigurationSource: true, fromDataAnnotation);

return ((EntityType)entityType).AddProperty(
propertyInfo, fromDataAnnotation ? ConfigurationSource.DataAnnotation : ConfigurationSource.Convention);
}
/// <summary>
/// Adds a property to this entity type.
/// </summary>
/// <param name="entityType"> The entity type to add the property to. </param>
/// <param name="name"> The name of the property to add. </param>
/// <param name="fromDataAnnotation"> Indicates whether the configuration was specified using a data annotation. </param>
/// <returns> The newly created property. </returns>
public static IConventionProperty AddProperty(
[NotNull] this IConventionEntityType entityType, [NotNull] string name,
bool fromDataAnnotation = false)
=> ((EntityType)entityType).AddProperty(name, fromDataAnnotation ? ConfigurationSource.DataAnnotation : ConfigurationSource.Convention);

/// <summary>
/// Adds a property to this entity type.
/// </summary>
/// <param name="entityType"> The entity type to add the property to. </param>
/// <param name="name"> The name of the property to add. </param>
/// <param name="propertyType"> The type of value the property will hold. </param>
/// <param name="setTypeConfigurationSource"> Indicates whether the type configuration source should be set. </param>
/// <param name="fromDataAnnotation"> Indicates whether the configuration was specified using a data annotation. </param>
/// <returns> The newly created property. </returns>
public static IConventionProperty AddProperty(
[NotNull] this IConventionEntityType entityType, [NotNull] string name, [NotNull] Type propertyType,
bool setTypeConfigurationSource = true, bool fromDataAnnotation = false)
=> entityType.AddProperty(name, propertyType, null, setTypeConfigurationSource, fromDataAnnotation);

/// <summary>
/// Adds a property based on an indexer to this entity type.
/// </summary>
/// <param name="entityType"> The entity type to add the property to. </param>
/// <param name="name"> The name of the property to add. </param>
/// <param name="propertyType"> The type of value the property will hold. </param>
/// <param name="setTypeConfigurationSource"> Indicates whether the type configuration source should be set. </param>
/// <param name="fromDataAnnotation"> Indicates whether the configuration was specified using a data annotation. </param>
/// <returns> The newly created property. </returns>
public static IConventionProperty AddIndexedProperty(
[NotNull] this IConventionEntityType entityType, [NotNull] string name, [NotNull] Type propertyType,
bool setTypeConfigurationSource = true, bool fromDataAnnotation = false)
=> Check.NotNull(entityType, nameof(entityType))
.AddProperty(name, propertyType, entityType.GetIndexerProperty(), setTypeConfigurationSource, fromDataAnnotation);

/// <summary>
/// Gets the index defined on the given property. Returns null if no index is defined.
Expand Down
44 changes: 37 additions & 7 deletions src/EFCore/Extensions/MutableEntityTypeExtensions.cs
Original file line number Diff line number Diff line change
@@ -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.Generic;
using System.Linq.Expressions;
using System.Reflection;
Expand Down Expand Up @@ -434,16 +435,45 @@ public static IMutableProperty FindDeclaredProperty([NotNull] this IMutableEntit
/// Adds a property to this entity type.
/// </summary>
/// <param name="entityType"> The entity type to add the property to. </param>
/// <param name="propertyInfo"> The corresponding property in the entity class. </param>
/// <param name="memberInfo"> The corresponding member on the entity class. </param>
/// <returns> The newly created property. </returns>
public static IMutableProperty AddProperty(
[NotNull] this IMutableEntityType entityType, [NotNull] PropertyInfo propertyInfo)
{
Check.NotNull(entityType, nameof(entityType));
Check.NotNull(propertyInfo, nameof(propertyInfo));
[NotNull] this IMutableEntityType entityType, [NotNull] MemberInfo memberInfo)
=> Check.NotNull(entityType, nameof(entityType))
.AddProperty(memberInfo.GetSimpleMemberName(), memberInfo.GetMemberType(), memberInfo);

return entityType.AsEntityType().AddProperty(propertyInfo, ConfigurationSource.Explicit);
}
/// <summary>
/// Adds a property to this entity type.
/// </summary>
/// <param name="entityType"> The entity type to add the property to. </param>
/// <param name="name"> The name of the property to add. </param>
/// <returns> The newly created property. </returns>
public static IMutableProperty AddProperty(
[NotNull] this IMutableEntityType entityType, [NotNull] string name)
=> ((EntityType)entityType).AddProperty(name, ConfigurationSource.Explicit);

/// <summary>
/// Adds a property to this entity type.
/// </summary>
/// <param name="entityType"> The entity type to add the property to. </param>
/// <param name="name"> The name of the property to add. </param>
/// <param name="propertyType"> The type of value the property will hold. </param>
/// <returns> The newly created property. </returns>
public static IMutableProperty AddProperty(
[NotNull] this IMutableEntityType entityType, [NotNull] string name, [NotNull] Type propertyType)
=> entityType.AddProperty(name, propertyType, null);

/// <summary>
/// Adds a property based on an indexer to this entity type.
/// </summary>
/// <param name="entityType"> The entity type to add the property to. </param>
/// <param name="name"> The name of the property to add. </param>
/// <param name="propertyType"> The type of value the property will hold. </param>
/// <returns> The newly created property. </returns>
public static IMutableProperty AddIndexedProperty(
[NotNull] this IMutableEntityType entityType, [NotNull] string name, [NotNull] Type propertyType)
=> Check.NotNull(entityType, nameof(entityType))
.AddProperty(name, propertyType, entityType.GetIndexerProperty());

/// <summary>
/// Gets the index defined on the given property. Returns null if no index is defined.
Expand Down
3 changes: 2 additions & 1 deletion src/EFCore/Metadata/Builders/IConventionEntityTypeBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,8 @@ IReadOnlyList<IConventionProperty> GetOrCreateProperties(
/// </summary>
/// <param name="properties"> The properties to remove. </param>
/// <param name="fromDataAnnotation"> Indicates whether the configuration was specified using a data annotation. </param>
void RemoveUnusedShadowProperties([NotNull] IReadOnlyList<IConventionProperty> properties, bool fromDataAnnotation = false);
IConventionEntityTypeBuilder RemoveUnusedShadowProperties(
[NotNull] IReadOnlyList<IConventionProperty> properties, bool fromDataAnnotation = false);

/// <summary>
/// Returns an object that can be used to configure the service property with the given member info.
Expand Down
23 changes: 9 additions & 14 deletions src/EFCore/Metadata/IConventionEntityType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -194,26 +194,21 @@ IConventionForeignKey AddForeignKey(
/// </summary>
/// <param name="name"> The name of the property to add. </param>
/// <param name="propertyType"> The type of value the property will hold. </param>
/// <param name="memberInfo">
/// <para>
/// The corresponding CLR type member or <c>null</c> for a shadow property.
/// </para>
/// <para>
/// An indexer with a <c>string</c> parameter and <c>object</c> return type can be used.
/// </para>
/// </param>
/// <param name="setTypeConfigurationSource"> Indicates whether the type configuration source should be set. </param>
/// <param name="fromDataAnnotation"> Indicates whether the configuration was specified using a data annotation. </param>
/// <returns> The newly created property. </returns>
IConventionProperty AddProperty(
[NotNull] string name,
[CanBeNull] Type propertyType,
bool setTypeConfigurationSource = true,
bool fromDataAnnotation = false);

/// <summary>
/// Adds a property based on an indexer to this entity type.
/// </summary>
/// <param name="name"> The name of the property to add. </param>
/// <param name="propertyType"> The type of value the property will hold. </param>
/// <param name="setTypeConfigurationSource"> Indicates whether the type configuration source should be set. </param>
/// <param name="fromDataAnnotation"> Indicates whether the configuration was specified using a data annotation. </param>
/// <returns> The newly created property. </returns>
IConventionProperty AddIndexedProperty(
[NotNull] string name,
[NotNull] Type propertyType,
[CanBeNull] MemberInfo memberInfo,
bool setTypeConfigurationSource = true,
bool fromDataAnnotation = false);

Expand Down
18 changes: 9 additions & 9 deletions src/EFCore/Metadata/IMutableEntityType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -151,16 +151,16 @@ IMutableForeignKey AddForeignKey(
/// </summary>
/// <param name="name"> The name of the property to add. </param>
/// <param name="propertyType"> The type of value the property will hold. </param>
/// <param name="memberInfo">
/// <para>
/// The corresponding CLR type member or <c>null</c> for a shadow property.
/// </para>
/// <para>
/// An indexer with a <c>string</c> parameter and <c>object</c> return type can be used.
/// </para>
/// </param>
/// <returns> The newly created property. </returns>
IMutableProperty AddProperty([NotNull] string name, [CanBeNull] Type propertyType);

/// <summary>
/// Adds a property based on an indexer to this entity type.
/// </summary>
/// <param name="name"> The name of the property to add. </param>
/// <param name="propertyType"> The type of value the property will hold. </param>
/// <returns> The newly created property. </returns>
IMutableProperty AddIndexedProperty([NotNull] string name, [NotNull] Type propertyType);
IMutableProperty AddProperty([NotNull] string name, [NotNull] Type propertyType, [CanBeNull] MemberInfo memberInfo);

/// <summary>
/// <para>
Expand Down
Loading

0 comments on commit 9e0fb47

Please sign in to comment.