Skip to content

Commit

Permalink
Metadata for primitive collection mapping
Browse files Browse the repository at this point in the history
Part of #30730

Some stuff remaining, but wanted to get this out there for initial review.

Missing:
- ElementType in compiled model
- Negative cases
- More tests

Review updates

Change builder shape based on API review

Updated based on review.

More tests and refactoring of how ElementTypes are created.
  • Loading branch information
ajcvickers committed Aug 9, 2023
1 parent 0ee3ac8 commit d8a0276
Show file tree
Hide file tree
Showing 97 changed files with 12,468 additions and 2,143 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using Microsoft.EntityFrameworkCore.Cosmos.Metadata.Internal;

namespace Microsoft.EntityFrameworkCore;

/// <summary>
/// Cosmos-specific extension methods for <see cref="PrimitiveCollectionBuilder" />.
/// </summary>
/// <remarks>
/// See <see href="https://aka.ms/efcore-docs-modeling">Modeling entity types and relationships</see>, and
/// <see href="https://aka.ms/efcore-docs-cosmos">Accessing Azure Cosmos DB with EF Core</see> for more information and examples.
/// </remarks>
public static class CosmosPrimitiveCollectionBuilderExtensions
{
/// <summary>
/// Configures the property name that the property is mapped to when targeting Azure Cosmos.
/// </summary>
/// <remarks>
/// <para>
/// If an empty string is supplied, the property will not be persisted.
/// </para>
/// <para>
/// See <see href="https://aka.ms/efcore-docs-modeling">Modeling entity types and relationships</see>, and
/// <see href="https://aka.ms/efcore-docs-cosmos">Accessing Azure Cosmos DB with EF Core</see> for more information and examples.
/// </para>
/// </remarks>
/// <param name="primitiveCollectionBuilder">The builder for the property being configured.</param>
/// <param name="name">The name of the property.</param>
/// <returns>The same builder instance so that multiple calls can be chained.</returns>
public static PrimitiveCollectionBuilder ToJsonProperty(
this PrimitiveCollectionBuilder primitiveCollectionBuilder,
string name)
{
Check.NotNull(name, nameof(name));

primitiveCollectionBuilder.Metadata.SetJsonPropertyName(name);

return primitiveCollectionBuilder;
}

/// <summary>
/// Configures the property name that the property is mapped to when targeting Azure Cosmos.
/// </summary>
/// <remarks>
/// See <see href="https://aka.ms/efcore-docs-modeling">Modeling entity types and relationships</see>, and
/// <see href="https://aka.ms/efcore-docs-cosmos">Accessing Azure Cosmos DB with EF Core</see> for more information and examples.
/// </remarks>
/// <typeparam name="TProperty">The type of the property being configured.</typeparam>
/// <param name="primitiveCollectionBuilder">The builder for the property being configured.</param>
/// <param name="name">The name of the property.</param>
/// <returns>The same builder instance so that multiple calls can be chained.</returns>
public static PrimitiveCollectionBuilder<TProperty> ToJsonProperty<TProperty>(
this PrimitiveCollectionBuilder<TProperty> primitiveCollectionBuilder,
string name)
=> (PrimitiveCollectionBuilder<TProperty>)ToJsonProperty((PrimitiveCollectionBuilder)primitiveCollectionBuilder, name);

/// <summary>
/// Configures this property to be the etag concurrency token.
/// </summary>
/// <remarks>
/// See <see href="https://aka.ms/efcore-docs-modeling">Modeling entity types and relationships</see>, and
/// <see href="https://aka.ms/efcore-docs-cosmos">Accessing Azure Cosmos DB with EF Core</see> for more information and examples.
/// </remarks>
/// <param name="primitiveCollectionBuilder">The builder for the property being configured.</param>
/// <returns>The same builder instance so that multiple calls can be chained.</returns>
public static PrimitiveCollectionBuilder IsETagConcurrency(this PrimitiveCollectionBuilder primitiveCollectionBuilder)
{
primitiveCollectionBuilder
.IsConcurrencyToken()
.ToJsonProperty("_etag")
.ValueGeneratedOnAddOrUpdate();

return primitiveCollectionBuilder;
}

/// <summary>
/// Configures this property to be the etag concurrency token.
/// </summary>
/// <remarks>
/// See <see href="https://aka.ms/efcore-docs-modeling">Modeling entity types and relationships</see>, and
/// <see href="https://aka.ms/efcore-docs-cosmos">Accessing Azure Cosmos DB with EF Core</see> for more information and examples.
/// </remarks>
/// <typeparam name="TProperty">The type of the property being configured.</typeparam>
/// <param name="primitiveCollectionBuilder">The builder for the property being configured.</param>
/// <returns>The same builder instance so that multiple calls can be chained.</returns>
public static PrimitiveCollectionBuilder<TProperty> IsETagConcurrency<TProperty>(
this PrimitiveCollectionBuilder<TProperty> primitiveCollectionBuilder)
=> (PrimitiveCollectionBuilder<TProperty>)IsETagConcurrency((PrimitiveCollectionBuilder)primitiveCollectionBuilder);
}
Loading

0 comments on commit d8a0276

Please sign in to comment.