Skip to content

Commit

Permalink
API review changes
Browse files Browse the repository at this point in the history
Issue #15662
  • Loading branch information
ajcvickers committed Jun 30, 2019
1 parent 4d07e57 commit 63a152b
Show file tree
Hide file tree
Showing 11 changed files with 64 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public static string GetDefaultTableName([NotNull] this IEntityType entityType)
return ownership.PrincipalEntityType.GetTableName();
}

return IdentifierHelpers.Truncate(
return Uniquifier.Truncate(
entityType.HasDefiningNavigation()
? $"{entityType.DefiningEntityType.GetTableName()}_{entityType.DefiningNavigationName}"
: entityType.ShortName(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public static string GetDefaultName([NotNull] this IForeignKey foreignKey)
.AppendJoin(foreignKey.Properties.Select(p => p.GetColumnName()), "_")
.ToString();

return IdentifierHelpers.Truncate(baseName, foreignKey.DeclaringEntityType.Model.GetMaxIdentifierLength());
return Uniquifier.Truncate(baseName, foreignKey.DeclaringEntityType.Model.GetMaxIdentifierLength());
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public static string GetDefaultName([NotNull] this IIndex index)
.AppendJoin(index.Properties.Select(p => p.GetColumnName()), "_")
.ToString();

return IdentifierHelpers.Truncate(baseName, index.DeclaringEntityType.Model.GetMaxIdentifierLength());
return Uniquifier.Truncate(baseName, index.DeclaringEntityType.Model.GetMaxIdentifierLength());
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public static string GetDefaultName([NotNull] this IKey key)
.AppendJoin(key.Properties.Select(p => p.GetColumnName()), "_");
}

return IdentifierHelpers.Truncate(builder.ToString(), key.DeclaringEntityType.Model.GetMaxIdentifierLength());
return Uniquifier.Truncate(builder.ToString(), key.DeclaringEntityType.Model.GetMaxIdentifierLength());
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public static string GetDefaultColumnName([NotNull] this IProperty property)
baseName = builder.ToString();
}

return IdentifierHelpers.Truncate(baseName, property.DeclaringEntityType.Model.GetMaxIdentifierLength());
return Uniquifier.Truncate(baseName, property.DeclaringEntityType.Model.GetMaxIdentifierLength());
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ private static void TryUniquifyTableNames(
{
if (entityType[RelationalAnnotationNames.TableName] == null)
{
var uniqueName = IdentifierHelpers.Uniquify(
var uniqueName = Uniquifier.Uniquify(
tableName.TableName, tables, n => (tableName.Schema, n), maxLength);
if (entityType.Builder.ToTable(uniqueName) != null)
{
Expand All @@ -110,7 +110,7 @@ private static void TryUniquifyTableNames(
var otherEntityType = entityTypes.First();
if (otherEntityType[RelationalAnnotationNames.TableName] == null)
{
var uniqueName = IdentifierHelpers.Uniquify(
var uniqueName = Uniquifier.Uniquify(
tableName.TableName, tables, n => (tableName.Schema, n), maxLength);
if (otherEntityType.Builder.ToTable(uniqueName) != null)
{
Expand Down Expand Up @@ -213,7 +213,7 @@ private static string TryUniquify(
}
}

columnName = IdentifierHelpers.Uniquify(columnName, properties, maxLength);
columnName = Uniquifier.Uniquify(columnName, properties, maxLength);
property.Builder.HasColumnName(columnName);
properties[columnName] = property;
return columnName;
Expand Down Expand Up @@ -260,7 +260,7 @@ private static string TryUniquify<T>(
{
if (key.Builder.CanSetName(null))
{
keyName = IdentifierHelpers.Uniquify(keyName, keys, maxLength);
keyName = Uniquifier.Uniquify(keyName, keys, maxLength);
key.Builder.HasName(keyName);
return keyName;
}
Expand Down Expand Up @@ -316,7 +316,7 @@ private static string TryUniquify<T>(
{
if (index.Builder.CanSetName(null))
{
indexName = IdentifierHelpers.Uniquify(indexName, indexes, maxLength);
indexName = Uniquifier.Uniquify(indexName, indexes, maxLength);
index.Builder.HasName(indexName);
return indexName;
}
Expand Down Expand Up @@ -375,7 +375,7 @@ private static string TryUniquify<T>(
{
if (foreignKey.Builder.CanSetConstraintName(null))
{
foreignKeyName = IdentifierHelpers.Uniquify(foreignKeyName, foreignKeys, maxLength);
foreignKeyName = Uniquifier.Uniquify(foreignKeyName, foreignKeys, maxLength);
foreignKey.Builder.HasConstraintName(foreignKeyName);
return foreignKeyName;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace Microsoft.EntityFrameworkCore.Infrastructure
/// <summary>
/// Provides methods for manipulating string identifiers.
/// </summary>
public static class IdentifierHelpers
public static class Uniquifier
{
/// <summary>
/// Creates a unique identifier by appending a number to the given string.
Expand Down
16 changes: 16 additions & 0 deletions src/EFCore/Metadata/IConstructorBindingFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,21 @@ bool TryBindConstructor(
[NotNull] ConstructorInfo constructor,
[CanBeNull] out InstantiationBinding binding,
[CanBeNull] out IEnumerable<ParameterInfo> unboundParameters);

/// <summary>
/// Attempts to create a <see cref="InstantiationBinding" /> for the given <see cref="IEntityType" /> and
/// <see cref="ConstructorInfo" />
/// </summary>
/// <param name="entityType"> The entity type. </param>
/// <param name="constructor"> The constructor to use. </param>
/// <param name="binding"> The binding, or <c>null</c> if <c>null</c> could be created. </param>
/// <param name="unboundParameters"> The parameters that could not be bound. </param>
/// <returns> <c>true</c> if a binding was created; <c>false</c> otherwise. </returns>
[ContractAnnotation("=>true, binding:notnull, failedBindings:null; =>false, binding:null, failedBindings:notnull")]
bool TryBindConstructor(
[NotNull] IMutableEntityType entityType,
[NotNull] ConstructorInfo constructor,
[CanBeNull] out InstantiationBinding binding,
[CanBeNull] out IEnumerable<ParameterInfo> unboundParameters);
}
}
2 changes: 1 addition & 1 deletion src/EFCore/Metadata/IPropertyParameterBindingFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public interface IPropertyParameterBindingFactory
/// <param name="parameterType"> The parameter name. </param>
/// <param name="parameterName"> The parameter type. </param>
/// <returns> The parameter binding, or <c>null</c> if none was found. </returns>
ParameterBinding TryBindParameter(
ParameterBinding FindParameter(
[NotNull] IEntityType entityType,
[NotNull] Type parameterType,
[NotNull] string parameterName);
Expand Down
36 changes: 34 additions & 2 deletions src/EFCore/Metadata/Internal/ConstructorBindingFactory.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;
using System.Reflection;
Expand Down Expand Up @@ -41,6 +42,24 @@ public ConstructorBindingFactory(
_factories = factories;
}

/// <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 virtual bool TryBindConstructor(
IMutableEntityType entityType,
ConstructorInfo constructor,
out InstantiationBinding binding,
out IEnumerable<ParameterInfo> unboundParameters)
=> TryBindConstructor(
entityType,
constructor,
(f, e, p, n) => f?.Bind((IMutableEntityType)e, p, n),
out binding,
out unboundParameters);

/// <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
Expand All @@ -52,11 +71,24 @@ public virtual bool TryBindConstructor(
ConstructorInfo constructor,
out InstantiationBinding binding,
out IEnumerable<ParameterInfo> unboundParameters)
=> TryBindConstructor(
entityType,
constructor,
(f, e, p, n) => f?.Bind((IConventionEntityType)e, p, n),
out binding,
out unboundParameters);

private bool TryBindConstructor(
IEntityType entityType,
ConstructorInfo constructor,
Func<IParameterBindingFactory, IEntityType, Type, string, ParameterBinding> bind,
out InstantiationBinding binding,
out IEnumerable<ParameterInfo> unboundParameters)
{
IEnumerable<(ParameterInfo Parameter, ParameterBinding Binding)> bindings
= constructor.GetParameters().Select(
p => (p, _propertyFactory.TryBindParameter(entityType, p.ParameterType, p.Name)
?? _factories.FindFactory(p.ParameterType, p.Name)?.Bind(entityType, p.ParameterType, p.Name)))
p => (p, _propertyFactory.FindParameter(entityType, p.ParameterType, p.Name)
?? bind(_factories.FindFactory(p.ParameterType, p.Name), entityType, p.ParameterType, p.Name)))
.ToList();

if (bindings.Any(b => b.Binding == null))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public class PropertyParameterBindingFactory : IPropertyParameterBindingFactory
/// 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 virtual ParameterBinding TryBindParameter(
public virtual ParameterBinding FindParameter(
IEntityType entityType,
Type parameterType,
string parameterName)
Expand Down

0 comments on commit 63a152b

Please sign in to comment.