Skip to content

Commit

Permalink
Add IConventionEntityTypeBuilder.GetTargetEntityTypeBuilder
Browse files Browse the repository at this point in the history
Make all IConvention* extension methods that set annotations return the value from the annotation that was set

Part of #22414
  • Loading branch information
AndriySvyryd authored Aug 10, 2022
1 parent a861d8b commit 1e122ac
Show file tree
Hide file tree
Showing 27 changed files with 323 additions and 414 deletions.
62 changes: 39 additions & 23 deletions src/EFCore.Cosmos/Extensions/CosmosEntityTypeExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,14 @@ public static void SetContainer(this IMutableEntityType entityType, string? name
/// <param name="entityType">The entity type to set the container name for.</param>
/// <param name="name">The name to set.</param>
/// <param name="fromDataAnnotation">Indicates whether the configuration was specified using a data annotation.</param>
public static void SetContainer(
public static string? SetContainer(
this IConventionEntityType entityType,
string? name,
bool fromDataAnnotation = false)
=> entityType.SetOrRemoveAnnotation(
=> (string?)entityType.SetOrRemoveAnnotation(
CosmosAnnotationNames.ContainerName,
Check.NullButNotEmpty(name, nameof(name)),
fromDataAnnotation);
fromDataAnnotation)?.Value;

/// <summary>
/// Gets the <see cref="ConfigurationSource" /> for the container to which the entity type is mapped.
Expand Down Expand Up @@ -96,14 +96,14 @@ public static void SetContainingPropertyName(this IMutableEntityType entityType,
/// <param name="entityType">The entity type to set the containing property name for.</param>
/// <param name="name">The name to set.</param>
/// <param name="fromDataAnnotation">Indicates whether the configuration was specified using a data annotation.</param>
public static void SetContainingPropertyName(
public static string? SetContainingPropertyName(
this IConventionEntityType entityType,
string? name,
bool fromDataAnnotation = false)
=> entityType.SetOrRemoveAnnotation(
=> (string?)entityType.SetOrRemoveAnnotation(
CosmosAnnotationNames.PropertyName,
Check.NullButNotEmpty(name, nameof(name)),
fromDataAnnotation);
fromDataAnnotation)?.Value;

/// <summary>
/// Gets the <see cref="ConfigurationSource" /> for the parent property to which the entity type is mapped.
Expand Down Expand Up @@ -138,14 +138,14 @@ public static void SetPartitionKeyPropertyName(this IMutableEntityType entityTyp
/// <param name="entityType">The entity type to set the partition key property name for.</param>
/// <param name="name">The name to set.</param>
/// <param name="fromDataAnnotation">Indicates whether the configuration was specified using a data annotation.</param>
public static void SetPartitionKeyPropertyName(
public static string? SetPartitionKeyPropertyName(
this IConventionEntityType entityType,
string? name,
bool fromDataAnnotation = false)
=> entityType.SetOrRemoveAnnotation(
=> (string?)entityType.SetOrRemoveAnnotation(
CosmosAnnotationNames.PartitionKeyName,
Check.NullButNotEmpty(name, nameof(name)),
fromDataAnnotation);
fromDataAnnotation)?.Value;

/// <summary>
/// Gets the <see cref="ConfigurationSource" /> for the property that is used to store the partition key.
Expand Down Expand Up @@ -232,14 +232,14 @@ public static void SetETagPropertyName(this IMutableEntityType entityType, strin
/// <param name="entityType">The entity type to set the ETag property name for.</param>
/// <param name="name">The name to set.</param>
/// <param name="fromDataAnnotation">Indicates whether the configuration was specified using a data annotation.</param>
public static void SetETagPropertyName(
public static string? SetETagPropertyName(
this IConventionEntityType entityType,
string? name,
bool fromDataAnnotation = false)
=> entityType.SetOrRemoveAnnotation(
=> (string?)entityType.SetOrRemoveAnnotation(
CosmosAnnotationNames.ETagName,
Check.NullButNotEmpty(name, nameof(name)),
fromDataAnnotation);
fromDataAnnotation)?.Value;

/// <summary>
/// Gets the <see cref="ConfigurationSource" /> for the property that is used to store the etag.
Expand All @@ -251,7 +251,7 @@ public static void SetETagPropertyName(
?.GetConfigurationSource();

/// <summary>
/// Gets the property on this entity that is mapped to cosmos ETag, if it exists.
/// Gets the property on this entity that is mapped to Cosmos ETag, if it exists.
/// </summary>
/// <param name="entityType">The entity type to get the ETag property for.</param>
/// <returns>The property mapped to ETag, or <see langword="null" /> if no property is mapped to ETag.</returns>
Expand All @@ -263,7 +263,23 @@ public static void SetETagPropertyName(
}

/// <summary>
/// Gets the property on this entity that is mapped to cosmos ETag, if it exists.
/// Gets the property on this entity that is mapped to Cosmos ETag, if it exists.
/// </summary>
/// <param name="entityType">The entity type to get the ETag property for.</param>
/// <returns>The property mapped to etag, or <see langword="null" /> if no property is mapped to ETag.</returns>
public static IMutableProperty? GetETagProperty(this IMutableEntityType entityType)
=> (IMutableProperty?)((IReadOnlyEntityType)entityType).GetETagProperty();

/// <summary>
/// Gets the property on this entity that is mapped to Cosmos ETag, if it exists.
/// </summary>
/// <param name="entityType">The entity type to get the ETag property for.</param>
/// <returns>The property mapped to etag, or <see langword="null" /> if no property is mapped to ETag.</returns>
public static IConventionProperty? GetETagProperty(this IConventionEntityType entityType)
=> (IConventionProperty?)((IReadOnlyEntityType)entityType).GetETagProperty();

/// <summary>
/// Gets the property on this entity that is mapped to Cosmos ETag, if it exists.
/// </summary>
/// <param name="entityType">The entity type to get the ETag property for.</param>
/// <returns>The property mapped to etag, or <see langword="null" /> if no property is mapped to ETag.</returns>
Expand Down Expand Up @@ -296,14 +312,14 @@ public static void SetAnalyticalStoreTimeToLive(this IMutableEntityType entityTy
/// <param name="entityType">The entity type.</param>
/// <param name="seconds">The time to live to set.</param>
/// <param name="fromDataAnnotation">Indicates whether the configuration was specified using a data annotation.</param>
public static void SetAnalyticalStoreTimeToLive(
public static int? SetAnalyticalStoreTimeToLive(
this IConventionEntityType entityType,
int? seconds,
bool fromDataAnnotation = false)
=> entityType.SetOrRemoveAnnotation(
=> (int?)entityType.SetOrRemoveAnnotation(
CosmosAnnotationNames.AnalyticalStoreTimeToLive,
seconds,
fromDataAnnotation);
fromDataAnnotation)?.Value;

/// <summary>
/// Gets the <see cref="ConfigurationSource" /> for the time to live for analytical store in seconds at container scope.
Expand Down Expand Up @@ -340,14 +356,14 @@ public static void SetDefaultTimeToLive(this IMutableEntityType entityType, int?
/// <param name="entityType">The entity type.</param>
/// <param name="seconds">The time to live to set.</param>
/// <param name="fromDataAnnotation">Indicates whether the configuration was specified using a data annotation.</param>
public static void SetDefaultTimeToLive(
public static int? SetDefaultTimeToLive(
this IConventionEntityType entityType,
int? seconds,
bool fromDataAnnotation = false)
=> entityType.SetOrRemoveAnnotation(
=> (int?)entityType.SetOrRemoveAnnotation(
CosmosAnnotationNames.DefaultTimeToLive,
seconds,
fromDataAnnotation);
fromDataAnnotation)?.Value;

/// <summary>
/// Gets the <see cref="ConfigurationSource" /> for the default time to live in seconds at container scope.
Expand Down Expand Up @@ -390,19 +406,19 @@ public static void SetThroughput(this IMutableEntityType entityType, int? throug
/// <param name="throughput">The throughput to set.</param>
/// <param name="autoscale">Whether autoscale is enabled.</param>
/// <param name="fromDataAnnotation">Indicates whether the configuration was specified using a data annotation.</param>
public static void SetThroughput(
public static int? SetThroughput(
this IConventionEntityType entityType,
int? throughput,
bool? autoscale,
bool fromDataAnnotation = false)
=> entityType.SetOrRemoveAnnotation(
=> (int?)entityType.SetOrRemoveAnnotation(
CosmosAnnotationNames.Throughput,
throughput == null || autoscale == null
? null
: autoscale.Value
? ThroughputProperties.CreateAutoscaleThroughput(throughput.Value)
: ThroughputProperties.CreateManualThroughput(throughput.Value),
fromDataAnnotation);
fromDataAnnotation)?.Value;

/// <summary>
/// Gets the <see cref="ConfigurationSource" /> for the provisioned throughput at container scope.
Expand Down
8 changes: 2 additions & 6 deletions src/EFCore.Cosmos/Extensions/CosmosModelExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,10 @@ public static void SetDefaultContainer(this IMutableModel model, string? name)
this IConventionModel model,
string? name,
bool fromDataAnnotation = false)
{
model.SetOrRemoveAnnotation(
=> (string?)model.SetOrRemoveAnnotation(
CosmosAnnotationNames.ContainerName,
Check.NullButNotEmpty(name, nameof(name)),
fromDataAnnotation);

return name;
}
fromDataAnnotation)?.Value;

/// <summary>
/// Returns the configuration source for the default container name.
Expand Down
8 changes: 2 additions & 6 deletions src/EFCore.Cosmos/Extensions/CosmosPropertyExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,10 @@ public static void SetJsonPropertyName(this IMutableProperty property, string? n
this IConventionProperty property,
string? name,
bool fromDataAnnotation = false)
{
property.SetOrRemoveAnnotation(
=> (string?)property.SetOrRemoveAnnotation(
CosmosAnnotationNames.PropertyName,
name,
fromDataAnnotation);

return name;
}
fromDataAnnotation)?.Value;

/// <summary>
/// Gets the <see cref="ConfigurationSource" /> the property name that the property is mapped to when targeting Cosmos.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,11 @@ public CosmosInversePropertyAttributeConvention(ProviderConventionSetBuilderDepe
Type targetClrType,
MemberInfo navigationMemberInfo,
bool shouldCreate = true)
=> ((InternalEntityTypeBuilder)entityTypeBuilder)
#pragma warning disable EF1001 // Internal EF Core API usage.
=> entityTypeBuilder
.GetTargetEntityTypeBuilder(
targetClrType,
navigationMemberInfo,
shouldCreate ? ConfigurationSource.DataAnnotation : null,
CosmosRelationshipDiscoveryConvention.ShouldBeOwnedType(targetClrType, entityTypeBuilder.Metadata.Model));
#pragma warning restore EF1001 // Internal EF Core API usage.
shouldCreate,
CosmosRelationshipDiscoveryConvention.ShouldBeOwnedType(targetClrType, entityTypeBuilder.Metadata.Model),
fromDataAnnotation: true);
}
41 changes: 12 additions & 29 deletions src/EFCore.Relational/Extensions/RelationalEntityTypeExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -112,14 +112,10 @@ public static void SetTableName(this IMutableEntityType entityType, string? name
this IConventionEntityType entityType,
string? name,
bool fromDataAnnotation = false)
{
entityType.SetAnnotation(
=> (string?)entityType.SetAnnotation(
RelationalAnnotationNames.TableName,
Check.NullButNotEmpty(name, nameof(name)),
fromDataAnnotation);

return name;
}
fromDataAnnotation)?.Value;

/// <summary>
/// Gets the <see cref="ConfigurationSource" /> for the table name.
Expand Down Expand Up @@ -201,14 +197,10 @@ public static void SetSchema(this IMutableEntityType entityType, string? value)
this IConventionEntityType entityType,
string? value,
bool fromDataAnnotation = false)
{
entityType.SetAnnotation(
=> (string?)entityType.SetAnnotation(
RelationalAnnotationNames.Schema,
Check.NullButNotEmpty(value, nameof(value)),
fromDataAnnotation);

return value;
}
fromDataAnnotation)?.Value;

/// <summary>
/// Gets the <see cref="ConfigurationSource" /> for the database schema.
Expand Down Expand Up @@ -342,14 +334,10 @@ public static void SetViewName(this IMutableEntityType entityType, string? name)
this IConventionEntityType entityType,
string? name,
bool fromDataAnnotation = false)
{
entityType.SetAnnotation(
=> (string?)entityType.SetAnnotation(
RelationalAnnotationNames.ViewName,
Check.NullButNotEmpty(name, nameof(name)),
fromDataAnnotation);

return name;
}
fromDataAnnotation)?.Value;

/// <summary>
/// Gets the <see cref="ConfigurationSource" /> for the view name.
Expand Down Expand Up @@ -416,14 +404,10 @@ public static void SetViewSchema(this IMutableEntityType entityType, string? val
this IConventionEntityType entityType,
string? value,
bool fromDataAnnotation = false)
{
entityType.SetAnnotation(
=> (string?)entityType.SetAnnotation(
RelationalAnnotationNames.ViewSchema,
Check.NullButNotEmpty(value, nameof(value)),
fromDataAnnotation);

return value;
}
fromDataAnnotation)?.Value;

/// <summary>
/// Gets the <see cref="ConfigurationSource" /> for the view schema.
Expand Down Expand Up @@ -1082,11 +1066,10 @@ public static void SetComment(this IMutableEntityType entityType, string? commen
this IConventionEntityType entityType,
string? comment,
bool fromDataAnnotation = false)
{
entityType.SetOrRemoveAnnotation(RelationalAnnotationNames.Comment, comment, fromDataAnnotation);

return comment;
}
=> (string?)entityType.SetOrRemoveAnnotation(
RelationalAnnotationNames.Comment,
comment,
fromDataAnnotation)?.Value;

/// <summary>
/// Gets the <see cref="ConfigurationSource" /> for the table comment.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,14 +114,10 @@ public static void SetConstraintName(this IMutableForeignKey foreignKey, string?
this IConventionForeignKey foreignKey,
string? value,
bool fromDataAnnotation = false)
{
foreignKey.SetOrRemoveAnnotation(
=> (string?)foreignKey.SetOrRemoveAnnotation(
RelationalAnnotationNames.Name,
Check.NullButNotEmpty(value, nameof(value)),
fromDataAnnotation);

return value;
}
fromDataAnnotation)?.Value;

/// <summary>
/// Gets the <see cref="ConfigurationSource" /> for the constraint name.
Expand Down
16 changes: 4 additions & 12 deletions src/EFCore.Relational/Extensions/RelationalIndexExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,14 +89,10 @@ public static void SetDatabaseName(this IMutableIndex index, string? name)
this IConventionIndex index,
string? name,
bool fromDataAnnotation = false)
{
index.SetOrRemoveAnnotation(
=> (string?)index.SetOrRemoveAnnotation(
RelationalAnnotationNames.Name,
Check.NullButNotEmpty(name, nameof(name)),
fromDataAnnotation);

return name;
}
fromDataAnnotation)?.Value;

/// <summary>
/// Gets the <see cref="ConfigurationSource" /> for the name of the index in the database.
Expand Down Expand Up @@ -157,14 +153,10 @@ public static void SetFilter(this IMutableIndex index, string? value)
/// <param name="fromDataAnnotation">Indicates whether the configuration was specified using a data annotation.</param>
/// <returns>The configured value.</returns>
public static string? SetFilter(this IConventionIndex index, string? value, bool fromDataAnnotation = false)
{
index.SetAnnotation(
=> (string?)index.SetAnnotation(
RelationalAnnotationNames.Filter,
Check.NullButNotEmpty(value, nameof(value)),
fromDataAnnotation);

return value;
}
fromDataAnnotation)?.Value;

/// <summary>
/// Gets the <see cref="ConfigurationSource" /> for the index filter expression.
Expand Down
8 changes: 2 additions & 6 deletions src/EFCore.Relational/Extensions/RelationalKeyExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,14 +87,10 @@ public static void SetName(this IMutableKey key, string? name)
/// <param name="fromDataAnnotation">Indicates whether the configuration was specified using a data annotation.</param>
/// <returns>The configured name.</returns>
public static string? SetName(this IConventionKey key, string? name, bool fromDataAnnotation = false)
{
key.SetOrRemoveAnnotation(
=> (string?)key.SetOrRemoveAnnotation(
RelationalAnnotationNames.Name,
Check.NullButNotEmpty(name, nameof(name)),
fromDataAnnotation);

return name;
}
fromDataAnnotation)?.Value;

/// <summary>
/// Gets the <see cref="ConfigurationSource" /> for the constraint name.
Expand Down
Loading

0 comments on commit 1e122ac

Please sign in to comment.