Skip to content

Commit

Permalink
Cleanup of value generation visibility and namespaces
Browse files Browse the repository at this point in the history
Fixes #20386

* Moved temp generators only used for relational corner cases to relational assembly.
* Made TemporaryNumberValueGeneratorFactory public because temp numbers are useful outside of relational. For example, Cosmnos.
* Made non-temp value and string and binary generators public since they can be generally useful and are used from providers.

No longer any use of internal code across boundaries.
  • Loading branch information
ajcvickers committed Mar 29, 2020
1 parent 759375c commit 9b1369c
Show file tree
Hide file tree
Showing 26 changed files with 312 additions and 232 deletions.
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
@@ -0,0 +1,114 @@
// 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.Threading;
using Microsoft.EntityFrameworkCore.ChangeTracking;

namespace Microsoft.EntityFrameworkCore.ValueGeneration.Internal
{
/// <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 class TemporaryDateTimeValueGenerator : ValueGenerator<DateTime>
{
private long _current;

/// <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 DateTime Next(EntityEntry entry) => new DateTime(Interlocked.Increment(ref _current));

/// <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 => true;
}

/// <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 class TemporaryDateTimeOffsetValueGenerator : ValueGenerator<DateTimeOffset>
{
private long _current;

/// <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 DateTimeOffset Next(EntityEntry entry)
=> new DateTimeOffset(Interlocked.Increment(ref _current), TimeSpan.Zero);

/// <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 => true;
}

/// <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 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 override bool GeneratesTemporaryValues => true;

/// <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 string Next(EntityEntry entry) => Guid.NewGuid().ToString();
}

/// <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 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 override bool GeneratesTemporaryValues => true;

/// <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 byte[] Next(EntityEntry entry) => Guid.NewGuid().ToByteArray();
}
}
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
44 changes: 0 additions & 44 deletions src/EFCore/ValueGeneration/Internal/BinaryValueGenerator.cs

This file was deleted.

44 changes: 0 additions & 44 deletions src/EFCore/ValueGeneration/Internal/StringValueGenerator.cs

This file was deleted.

Loading

0 comments on commit 9b1369c

Please sign in to comment.