Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleanup of value generation visibility and namespaces #20457

Merged
merged 1 commit into from
Mar 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
using JetBrains.Annotations;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.ValueGeneration;
using Microsoft.EntityFrameworkCore.ValueGeneration.Internal;

namespace Microsoft.EntityFrameworkCore.Cosmos.ValueGeneration.Internal
{
Expand All @@ -23,7 +22,7 @@ public override ValueGenerator Create(IProperty property, IEntityType entityType
if (property.GetJsonPropertyName() == ""
&& type == typeof(int))
{
return new TemporaryIntValueGenerator();
return new TemporaryNumberValueGeneratorFactory().Create(property);
}

return base.Create(property, entityType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,15 @@ namespace Microsoft.EntityFrameworkCore.ValueGeneration.Internal
/// 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.
/// </summary>
public class BinaryValueGenerator : ValueGenerator<byte[]>
public class TemporaryBinaryValueGenerator : ValueGenerator<byte[]>
{
/// <summary>
/// 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.
/// </summary>
public BinaryValueGenerator(bool generateTemporaryValues)
{
GeneratesTemporaryValues = generateTemporaryValues;
}

/// <summary>
/// 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.
/// </summary>
public override bool GeneratesTemporaryValues { get; }
public override bool GeneratesTemporaryValues => true;

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,15 @@ namespace Microsoft.EntityFrameworkCore.ValueGeneration.Internal
/// 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.
/// </summary>
public class StringValueGenerator : ValueGenerator<string>
public class TemporaryStringValueGenerator : ValueGenerator<string>
{
/// <summary>
/// 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.
/// </summary>
public StringValueGenerator(bool generateTemporaryValues)
{
GeneratesTemporaryValues = generateTemporaryValues;
}

/// <summary>
/// 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.
/// </summary>
public override bool GeneratesTemporaryValues { get; }
public override bool GeneratesTemporaryValues => true;

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,7 @@ namespace Microsoft.EntityFrameworkCore.ValueGeneration
public class RelationalValueGeneratorSelector : ValueGeneratorSelector
{
private readonly TemporaryNumberValueGeneratorFactory _numberFactory
#pragma warning disable EF1001 // Internal EF Core API usage.
= new TemporaryNumberValueGeneratorFactory();
#pragma warning restore EF1001 // Internal EF Core API usage.

/// <summary>
/// Initializes a new instance of the <see cref="RelationalValueGeneratorSelector" /> class.
Expand Down Expand Up @@ -64,23 +62,17 @@ public override ValueGenerator Create(IProperty property, IEntityType entityType
|| propertyType == typeof(float)
|| propertyType == typeof(double))
{
#pragma warning disable EF1001 // Internal EF Core API usage.
return _numberFactory.Create(property);
#pragma warning restore EF1001 // Internal EF Core API usage.
}

if (propertyType == typeof(DateTime))
{
#pragma warning disable EF1001 // Internal EF Core API usage.
return new TemporaryDateTimeValueGenerator();
#pragma warning restore EF1001 // Internal EF Core API usage.
}

if (propertyType == typeof(DateTimeOffset))
{
#pragma warning disable EF1001 // Internal EF Core API usage.
return new TemporaryDateTimeOffsetValueGenerator();
#pragma warning restore EF1001 // Internal EF Core API usage.
}

if (property.GetDefaultValueSql() != null)
Expand All @@ -92,16 +84,12 @@ public override ValueGenerator Create(IProperty property, IEntityType entityType

if (propertyType == typeof(string))
{
#pragma warning disable EF1001 // Internal EF Core API usage.
return new StringValueGenerator(generateTemporaryValues: true);
#pragma warning restore EF1001 // Internal EF Core API usage.
return new TemporaryStringValueGenerator();
}

if (propertyType == typeof(byte[]))
{
#pragma warning disable EF1001 // Internal EF Core API usage.
return new BinaryValueGenerator(generateTemporaryValues: true);
#pragma warning restore EF1001 // Internal EF Core API usage.
return new TemporaryBinaryValueGenerator();
}
}
}
Expand Down
27 changes: 27 additions & 0 deletions src/EFCore/ValueGeneration/BinaryValueGenerator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// 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 Microsoft.EntityFrameworkCore.ChangeTracking;

namespace Microsoft.EntityFrameworkCore.ValueGeneration
{
/// <summary>
/// Generates an array bytes from <see cref="Guid.NewGuid()" />.
/// The generated values are non-temporary, meaning they will be saved to the database.
/// </summary>
public class BinaryValueGenerator : ValueGenerator<byte[]>
{
/// <summary>
/// Gets a value indicating whether the values generated are temporary or permanent. This implementation
/// always returns false, meaning the generated values will be saved to the database.
/// </summary>
public override bool GeneratesTemporaryValues => false;

/// <summary>
/// Gets a value to be assigned to a property.
/// </summary>
/// <returns> The value to be assigned to a property. </returns>
public override byte[] Next(EntityEntry entry) => Guid.NewGuid().ToByteArray();
}
}
2 changes: 1 addition & 1 deletion src/EFCore/ValueGeneration/GuidValueGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class GuidValueGenerator : ValueGenerator<Guid>
/// <summary>
/// Gets a value to be assigned to a property.
/// </summary>
/// <para>The change tracking entry of the entity for which the value is being generated.</para>
/// <param name="entry"> The change tracking entry of the entity for which the value is being generated. </param>
/// <returns> The value to be assigned to a property. </returns>
public override Guid Next(EntityEntry entry) => Guid.NewGuid();

Expand Down
5 changes: 3 additions & 2 deletions src/EFCore/ValueGeneration/HiLoValueGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,15 @@ protected HiLoValueGenerator([NotNull] HiLoValueGeneratorState generatorState)
/// <summary>
/// Gets a value to be assigned to a property.
/// </summary>
/// <para>The change tracking entry of the entity for which the value is being generated.</para>
/// <param name="entry"> The change tracking entry of the entity for which the value is being generated. </param>
/// <returns> The value to be assigned to a property. </returns>
public override TValue Next(EntityEntry entry) => _generatorState.Next<TValue>(GetNewLowValue);

/// <summary>
/// Gets a value to be assigned to a property.
/// </summary>
/// <para>The change tracking entry of the entity for which the value is being generated.</para>
/// <param name="entry"> The change tracking entry of the entity for which the value is being generated. </param>
/// <param name="cancellationToken">A <see cref="CancellationToken" /> to observe while waiting for the task to complete.</param>
/// <returns> The value to be assigned to a property. </returns>
public override ValueTask<TValue> NextAsync(
EntityEntry entry, CancellationToken cancellationToken = default)
Expand Down
2 changes: 1 addition & 1 deletion src/EFCore/ValueGeneration/SequentialGuidValueGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public class SequentialGuidValueGenerator : ValueGenerator<Guid>
/// <summary>
/// Gets a value to be assigned to a property.
/// </summary>
/// <para>The change tracking entry of the entity for which the value is being generated.</para>
/// <param name="entry"> The change tracking entry of the entity for which the value is being generated. </param>
/// <returns> The value to be assigned to a property. </returns>
public override Guid Next(EntityEntry entry)
{
Expand Down
27 changes: 27 additions & 0 deletions src/EFCore/ValueGeneration/StringValueGenerator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// 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 Microsoft.EntityFrameworkCore.ChangeTracking;

namespace Microsoft.EntityFrameworkCore.ValueGeneration
{
/// <summary>
/// Generates <see cref="string" /> values using a string representation of <see cref="Guid.NewGuid()" />.
/// The generated values are non-temporary, meaning they will be saved to the database.
/// </summary>
public class StringValueGenerator : ValueGenerator<string>
{
/// <summary>
/// Gets a value indicating whether the values generated are temporary or permanent. This implementation
/// always returns false, meaning the generated values will be saved to the database.
/// </summary>
public override bool GeneratesTemporaryValues => false;

/// <summary>
/// Gets a value to be assigned to a property.
/// </summary>
/// <returns> The value to be assigned to a property. </returns>
public override string Next(EntityEntry entry) => Guid.NewGuid().ToString();
}
}
5 changes: 2 additions & 3 deletions src/EFCore/ValueGeneration/TemporaryGuidValueGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,9 @@ namespace Microsoft.EntityFrameworkCore.ValueGeneration
public class TemporaryGuidValueGenerator : GuidValueGenerator
{
/// <summary>
/// Gets a value indicating whether the values generated are temporary or permanent. This implementation
/// always returns true, meaning the generated values will be replaced by database generated values when
/// the entity is saved
/// Gets a value to be assigned to a property.
/// </summary>
/// <returns> The value to be assigned to a property. </returns>
public override bool GeneratesTemporaryValues => true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,28 @@
using System;
using Microsoft.EntityFrameworkCore.Diagnostics;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.ValueGeneration.Internal;

namespace Microsoft.EntityFrameworkCore.ValueGeneration.Internal
namespace Microsoft.EntityFrameworkCore.ValueGeneration
{
/// <summary>
/// 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.
/// <para>
/// Factory for creation of temporary integer value generators appropriate
/// for the numeric type of the property.
/// </para>
/// <para>
/// Types supported are: <see cref="int" />, <see cref="long" />, <see cref="short" />, <see cref="byte" />,
/// <see cref="char" />, <see cref="ulong" />, <see cref="uint" />, <see cref="ushort" />, <see cref="sbyte" />,
/// <see cref="decimal" />, <see cref="float" />, <see cref="double" />
/// </para>
/// </summary>
public class TemporaryNumberValueGeneratorFactory : ValueGeneratorFactory
{
/// <summary>
/// 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.
/// Creates a new value generator.
/// </summary>
/// <param name="property"> The property to create the value generator for. </param>
/// <returns> The newly created value generator. </returns>
public override ValueGenerator Create(IProperty property)
{
var type = property.ClrType.UnwrapNullableType().UnwrapEnumType();
Expand Down
10 changes: 6 additions & 4 deletions src/EFCore/ValueGeneration/ValueGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,23 @@ public abstract class ValueGenerator
/// <summary>
/// Gets a value to be assigned to a property.
/// </summary>
/// <para>The change tracking entry of the entity for which the value is being generated.</para>
/// <param name="entry"> The change tracking entry of the entity for which the value is being generated. </param>
/// <returns> The value to be assigned to a property. </returns>
public virtual object Next([NotNull] EntityEntry entry)
=> NextValue(entry);

/// <summary>
/// Template method to be overridden by implementations to perform value generation.
/// </summary>
/// <para>The change tracking entry of the entity for which the value is being generated.</para>
/// <param name="entry"> The change tracking entry of the entity for which the value is being generated. </param>
/// <returns> The generated value. </returns>
protected abstract object NextValue([NotNull] EntityEntry entry);

/// <summary>
/// Gets a value to be assigned to a property.
/// </summary>
/// <para>The change tracking entry of the entity for which the value is being generated.</para>
/// <param name="entry"> The change tracking entry of the entity for which the value is being generated. </param>
/// <param name="cancellationToken">A <see cref="CancellationToken" /> to observe while waiting for the task to complete.</param>
/// <returns> The value to be assigned to a property. </returns>
public virtual ValueTask<object> NextAsync(
[NotNull] EntityEntry entry,
Expand All @@ -42,7 +43,8 @@ public virtual ValueTask<object> NextAsync(
/// <summary>
/// Template method to be overridden by implementations to perform value generation.
/// </summary>
/// <para>The change tracking entry of the entity for which the value is being generated.</para>
/// <param name="entry"> The change tracking entry of the entity for which the value is being generated. </param>
/// <param name="cancellationToken">A <see cref="CancellationToken" /> to observe while waiting for the task to complete.</param>
/// <returns> The generated value. </returns>
protected virtual ValueTask<object> NextValueAsync(
[NotNull] EntityEntry entry,
Expand Down
5 changes: 2 additions & 3 deletions src/EFCore/ValueGeneration/ValueGeneratorSelector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Utilities;
using Microsoft.EntityFrameworkCore.ValueGeneration.Internal;
using Microsoft.Extensions.DependencyInjection;

namespace Microsoft.EntityFrameworkCore.ValueGeneration
Expand Down Expand Up @@ -118,12 +117,12 @@ public virtual ValueGenerator Create([NotNull] IProperty property, [NotNull] IEn

if (propertyType == typeof(string))
{
return new StringValueGenerator(generateTemporaryValues: false);
return new StringValueGenerator();
}

if (propertyType == typeof(byte[]))
{
return new BinaryValueGenerator(generateTemporaryValues: false);
return new BinaryValueGenerator();
}

throw new NotSupportedException(
Expand Down
Loading