Skip to content

Commit

Permalink
Allow TypeMappingSource to use the pre-convention configuration
Browse files Browse the repository at this point in the history
Part of #25084
  • Loading branch information
AndriySvyryd authored Aug 11, 2021
1 parent 68cfaf9 commit 4f93561
Show file tree
Hide file tree
Showing 24 changed files with 1,117 additions and 351 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -545,8 +545,7 @@ private void GenerateIndex(IIndex index)

var lines = new List<string>
{
$".{nameof(EntityTypeBuilder.HasIndex)}({_code.Lambda(index.Properties, "e")}, "
+ $"{_code.Literal(index.GetDatabaseName())})"
$".{nameof(EntityTypeBuilder.HasIndex)}({_code.Lambda(index.Properties, "e")}, {_code.Literal(index.GetDatabaseName())})"
};
annotations.Remove(RelationalAnnotationNames.Name);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.Linq;
using System.Text;
using Microsoft.EntityFrameworkCore.Diagnostics;
using Microsoft.EntityFrameworkCore.Infrastructure;
Expand Down Expand Up @@ -833,7 +830,7 @@ public static void SetDefaultValue(this IMutableProperty property, object? value
/// </summary>
/// <param name="property"> The property. </param>
/// <param name="storeObject"> The identifier of the table-like store object containing the column. </param>
/// <returns> The maximum length, or <see langword="null" /> if none if defined. </returns>
/// <returns> The maximum length, or <see langword="null" /> if none is defined. </returns>
public static int? GetMaxLength(this IReadOnlyProperty property, in StoreObjectIdentifier storeObject)
{
Check.NotNull(property, nameof(property));
Expand Down
21 changes: 17 additions & 4 deletions src/EFCore.Relational/Storage/IRelationalTypeMappingSource.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Reflection;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.Extensions.DependencyInjection;
Expand All @@ -10,7 +9,7 @@ namespace Microsoft.EntityFrameworkCore.Storage
{
/// <summary>
/// <para>
/// The relational type mapping interface for EF Core, starting with version 2.1. Type mappings describe how a
/// The relational type mapping source. Type mappings describe how a
/// provider maps CLR types/values to database types/values.
/// </para>
/// <para>
Expand Down Expand Up @@ -55,14 +54,28 @@ public interface IRelationalTypeMappingSource : ITypeMappingSource
/// </para>
/// <para>
/// Note: Only call this method if there is no <see cref="IProperty" />
/// or <see cref="MemberInfo" /> available, otherwise call <see cref="FindMapping(IProperty)" />
/// or <see cref="FindMapping(MemberInfo)" />
/// or <see cref="IModel" /> available, otherwise call <see cref="FindMapping(IProperty)" />
/// or <see cref="FindMapping(Type, IModel)" />
/// </para>
/// </summary>
/// <param name="type"> The CLR type. </param>
/// <returns> The type mapping, or <see langword="null" /> if none was found. </returns>
new RelationalTypeMapping? FindMapping(Type type);

/// <summary>
/// <para>
/// Finds the type mapping for a given <see cref="Type" />, taking pre-convention configuration into the account.
/// </para>
/// <para>
/// Note: Only call this method if there is no <see cref="IProperty" />,
/// otherwise call <see cref="FindMapping(IProperty)" />.
/// </para>
/// </summary>
/// <param name="type"> The CLR type. </param>
/// <param name="model"> The model. </param>
/// <returns> The type mapping, or <see langword="null" /> if none was found. </returns>
new RelationalTypeMapping? FindMapping(Type type, IModel model);

/// <summary>
/// <para>
/// Finds the type mapping for a given database type name.
Expand Down
72 changes: 32 additions & 40 deletions src/EFCore.Relational/Storage/RelationalTypeMappingInfo.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Collections.Generic;
using System.Reflection;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
Expand All @@ -14,7 +12,7 @@ namespace Microsoft.EntityFrameworkCore.Storage
/// Describes metadata needed to decide on a relational type mapping for
/// a property, type, or provider-specific relational type name.
/// </summary>
public readonly struct RelationalTypeMappingInfo : IEquatable<RelationalTypeMappingInfo>
public readonly record struct RelationalTypeMappingInfo : IEquatable<RelationalTypeMappingInfo>
{
private readonly TypeMappingInfo _coreTypeMappingInfo;

Expand Down Expand Up @@ -181,59 +179,80 @@ public RelationalTypeMappingInfo(
/// <summary>
/// The provider-specific relational type name for which mapping is needed.
/// </summary>
public string? StoreTypeName { get; }
public string? StoreTypeName { get; init; }

/// <summary>
/// The provider-specific relational type name, with any facets removed.
/// </summary>
public string? StoreTypeNameBase { get; }
public string? StoreTypeNameBase { get; init; }

/// <summary>
/// Indicates the store-size to use for the mapping, or null if none.
/// </summary>
public int? Size
=> _coreTypeMappingInfo.Size;
{
get => _coreTypeMappingInfo.Size;
init => _coreTypeMappingInfo = _coreTypeMappingInfo with { Size = value };
}

/// <summary>
/// The suggested precision of the mapped data type.
/// </summary>
public int? Precision
=> _coreTypeMappingInfo.Precision;
{
get => _coreTypeMappingInfo.Precision;
init => _coreTypeMappingInfo = _coreTypeMappingInfo with { Precision = value };
}

/// <summary>
/// The suggested scale of the mapped data type.
/// </summary>
public int? Scale
=> _coreTypeMappingInfo.Scale;
{
get => _coreTypeMappingInfo.Scale;
init => _coreTypeMappingInfo = _coreTypeMappingInfo with { Scale = value };
}

/// <summary>
/// Whether or not the mapped data type is fixed length.
/// </summary>
public bool? IsFixedLength { get; }
public bool? IsFixedLength { get; init; }

/// <summary>
/// Indicates whether or not the mapping is part of a key or index.
/// </summary>
public bool IsKeyOrIndex
=> _coreTypeMappingInfo.IsKeyOrIndex;
{
get => _coreTypeMappingInfo.IsKeyOrIndex;
init => _coreTypeMappingInfo = _coreTypeMappingInfo with { IsKeyOrIndex = value };
}

/// <summary>
/// Indicates whether or not the mapping supports Unicode, or null if not defined.
/// </summary>
public bool? IsUnicode
=> _coreTypeMappingInfo.IsUnicode;
{
get => _coreTypeMappingInfo.IsUnicode;
init => _coreTypeMappingInfo = _coreTypeMappingInfo with { IsUnicode = value };
}

/// <summary>
/// Indicates whether or not the mapping will be used for a row version, or null if not defined.
/// </summary>
public bool? IsRowVersion
=> _coreTypeMappingInfo.IsRowVersion;
{
get => _coreTypeMappingInfo.IsRowVersion;
init => _coreTypeMappingInfo = _coreTypeMappingInfo with { IsRowVersion = value };
}

/// <summary>
/// The CLR type in the model.
/// </summary>
public Type? ClrType
=> _coreTypeMappingInfo.ClrType;
{
get => _coreTypeMappingInfo.ClrType;
init => _coreTypeMappingInfo = _coreTypeMappingInfo with { ClrType = value };
}

/// <summary>
/// Returns a new <see cref="TypeMappingInfo" /> with the given converter applied.
Expand All @@ -242,32 +261,5 @@ public Type? ClrType
/// <returns> The new mapping info. </returns>
public RelationalTypeMappingInfo WithConverter(in ValueConverterInfo converterInfo)
=> new(this, converterInfo);

/// <summary>
/// Compares this <see cref="RelationalTypeMappingInfo" /> to another to check if they represent the same mapping.
/// </summary>
/// <param name="other"> The other object. </param>
/// <returns> <see langword="true" /> if they represent the same mapping; <see langword="false" /> otherwise. </returns>
public bool Equals(RelationalTypeMappingInfo other)
=> _coreTypeMappingInfo.Equals(other._coreTypeMappingInfo)
&& IsFixedLength == other.IsFixedLength
&& StoreTypeName == other.StoreTypeName;

/// <summary>
/// Compares this <see cref="RelationalTypeMappingInfo" /> to another to check if they represent the same mapping.
/// </summary>
/// <param name="obj"> The other object. </param>
/// <returns> <see langword="true" /> if they represent the same mapping; <see langword="false" /> otherwise. </returns>
public override bool Equals(object? obj)
=> obj != null
&& obj.GetType() == GetType()
&& Equals((RelationalTypeMappingInfo)obj);

/// <summary>
/// Returns a hash code for this object.
/// </summary>
/// <returns> The hash code. </returns>
public override int GetHashCode()
=> HashCode.Combine(_coreTypeMappingInfo, StoreTypeName, IsFixedLength);
}
}
Loading

0 comments on commit 4f93561

Please sign in to comment.