Skip to content

Commit

Permalink
Add parameterless constructors to value converters (#25454)
Browse files Browse the repository at this point in the history
Fixes #25415
  • Loading branch information
ajcvickers authored Aug 9, 2021
1 parent dd21b40 commit eb44e96
Show file tree
Hide file tree
Showing 31 changed files with 293 additions and 36 deletions.
10 changes: 9 additions & 1 deletion src/EFCore/Storage/ValueConversion/BoolToZeroOneConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,22 @@ namespace Microsoft.EntityFrameworkCore.Storage.ValueConversion
/// </summary>
public class BoolToZeroOneConverter<TProvider> : BoolToTwoValuesConverter<TProvider>
{
/// <summary>
/// Creates a new instance of this converter. This converter preserves order.
/// </summary>
public BoolToZeroOneConverter()
: this(null)
{
}

/// <summary>
/// Creates a new instance of this converter. This converter preserves order.
/// </summary>
/// <param name="mappingHints">
/// Hints that can be used by the <see cref="ITypeMappingSource" /> to create data types with appropriate
/// facets for the converted data.
/// </param>
public BoolToZeroOneConverter(ConverterMappingHints? mappingHints = null)
public BoolToZeroOneConverter(ConverterMappingHints? mappingHints)
: base(Zero(), One(), null, mappingHints)
{
}
Expand Down
11 changes: 9 additions & 2 deletions src/EFCore/Storage/ValueConversion/BytesToStringConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,22 @@ namespace Microsoft.EntityFrameworkCore.Storage.ValueConversion
/// </summary>
public class BytesToStringConverter : ValueConverter<byte[]?, string?>
{
/// <summary>
/// Creates a new instance of this converter.
/// </summary>
public BytesToStringConverter()
: this(null)
{
}

/// <summary>
/// Creates a new instance of this converter.
/// </summary>
/// <param name="mappingHints">
/// Hints that can be used by the <see cref="ITypeMappingSource" /> to create data types with appropriate
/// facets for the converted data.
/// </param>
public BytesToStringConverter(
ConverterMappingHints? mappingHints = null)
public BytesToStringConverter(ConverterMappingHints? mappingHints)
: base(
v => v == null ? null : Convert.ToBase64String(v),
v => v == null ? null : Convert.FromBase64String(v),
Expand Down
10 changes: 9 additions & 1 deletion src/EFCore/Storage/ValueConversion/CharToStringConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,22 @@ public class CharToStringConverter : StringCharConverter<char, string>
{
private static readonly ConverterMappingHints _defaultHints = new(size: 1);

/// <summary>
/// Creates a new instance of this converter. This converter preserves order.
/// </summary>
public CharToStringConverter()
: this(null)
{
}

/// <summary>
/// Creates a new instance of this converter. This converter preserves order.
/// </summary>
/// <param name="mappingHints">
/// Hints that can be used by the <see cref="ITypeMappingSource" /> to create data types with appropriate
/// facets for the converted data.
/// </param>
public CharToStringConverter(ConverterMappingHints? mappingHints = null)
public CharToStringConverter(ConverterMappingHints? mappingHints)
: base(
ToString(),
ToChar(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,22 @@ namespace Microsoft.EntityFrameworkCore.Storage.ValueConversion
/// </summary>
public class DateTimeOffsetToBinaryConverter : ValueConverter<DateTimeOffset, long>
{
/// <summary>
/// Creates a new instance of this converter.
/// </summary>
public DateTimeOffsetToBinaryConverter()
: this(null)
{
}

/// <summary>
/// Creates a new instance of this converter.
/// </summary>
/// <param name="mappingHints">
/// Hints that can be used by the <see cref="ITypeMappingSource" /> to create data types with appropriate
/// facets for the converted data.
/// </param>
public DateTimeOffsetToBinaryConverter(ConverterMappingHints? mappingHints = null)
public DateTimeOffsetToBinaryConverter(ConverterMappingHints? mappingHints)
: base(
v => ((v.Ticks / 1000) << 11) | ((long)v.Offset.TotalMinutes & 0x7FF),
v => new DateTimeOffset(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,22 @@ public class DateTimeOffsetToBytesConverter : ValueConverter<DateTimeOffset, byt
private static readonly NumberToBytesConverter<long> _longToBytes = new();
private static readonly NumberToBytesConverter<short> _shortToBytes = new();

/// <summary>
/// Creates a new instance of this converter.
/// </summary>
public DateTimeOffsetToBytesConverter()
: this(null)
{
}

/// <summary>
/// Creates a new instance of this converter.
/// </summary>
/// <param name="mappingHints">
/// Hints that can be used by the <see cref="ITypeMappingSource" /> to create data types with appropriate
/// facets for the converted data.
/// </param>
public DateTimeOffsetToBytesConverter(ConverterMappingHints? mappingHints = null)
public DateTimeOffsetToBytesConverter(ConverterMappingHints? mappingHints)
: base(
v => ToBytes(v),
v => FromBytes(v),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,22 @@ namespace Microsoft.EntityFrameworkCore.Storage.ValueConversion
/// </summary>
public class DateTimeOffsetToStringConverter : StringDateTimeOffsetConverter<DateTimeOffset, string>
{
/// <summary>
/// Creates a new instance of this converter.
/// </summary>
public DateTimeOffsetToStringConverter()
: this(null)
{
}

/// <summary>
/// Creates a new instance of this converter.
/// </summary>
/// <param name="mappingHints">
/// Hints that can be used by the <see cref="ITypeMappingSource" /> to create data types with appropriate
/// facets for the converted data.
/// </param>
public DateTimeOffsetToStringConverter(ConverterMappingHints? mappingHints = null)
public DateTimeOffsetToStringConverter(ConverterMappingHints? mappingHints)
: base(
ToString(),
ToDateTimeOffset(),
Expand Down
10 changes: 9 additions & 1 deletion src/EFCore/Storage/ValueConversion/DateTimeToBinaryConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,22 @@ namespace Microsoft.EntityFrameworkCore.Storage.ValueConversion
/// </summary>
public class DateTimeToBinaryConverter : ValueConverter<DateTime, long>
{
/// <summary>
/// Creates a new instance of this converter.
/// </summary>
public DateTimeToBinaryConverter()
: this(null)
{
}

/// <summary>
/// Creates a new instance of this converter.
/// </summary>
/// <param name="mappingHints">
/// Hints that can be used by the <see cref="ITypeMappingSource" /> to create data types with appropriate
/// facets for the converted data.
/// </param>
public DateTimeToBinaryConverter(ConverterMappingHints? mappingHints = null)
public DateTimeToBinaryConverter(ConverterMappingHints? mappingHints)
: base(
v => v.ToBinary(),
v => DateTime.FromBinary(v),
Expand Down
10 changes: 9 additions & 1 deletion src/EFCore/Storage/ValueConversion/DateTimeToStringConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,22 @@ namespace Microsoft.EntityFrameworkCore.Storage.ValueConversion
/// </summary>
public class DateTimeToStringConverter : StringDateTimeConverter<DateTime, string>
{
/// <summary>
/// Creates a new instance of this converter.
/// </summary>
public DateTimeToStringConverter()
: this(null)
{
}

/// <summary>
/// Creates a new instance of this converter.
/// </summary>
/// <param name="mappingHints">
/// Hints that can be used by the <see cref="ITypeMappingSource" /> to create data types with appropriate
/// facets for the converted data.
/// </param>
public DateTimeToStringConverter(ConverterMappingHints? mappingHints = null)
public DateTimeToStringConverter(ConverterMappingHints? mappingHints)
: base(
ToString(),
ToDateTime(),
Expand Down
10 changes: 9 additions & 1 deletion src/EFCore/Storage/ValueConversion/DateTimeToTicksConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,22 @@ namespace Microsoft.EntityFrameworkCore.Storage.ValueConversion
/// </summary>
public class DateTimeToTicksConverter : ValueConverter<DateTime, long>
{
/// <summary>
/// Creates a new instance of this converter.
/// </summary>
public DateTimeToTicksConverter()
: this(null)
{
}

/// <summary>
/// Creates a new instance of this converter.
/// </summary>
/// <param name="mappingHints">
/// Hints that can be used by the <see cref="ITypeMappingSource" /> to create data types with appropriate
/// facets for the converted data.
/// </param>
public DateTimeToTicksConverter(ConverterMappingHints? mappingHints = null)
public DateTimeToTicksConverter(ConverterMappingHints? mappingHints)
: base(
v => v.Ticks,
v => new DateTime(v),
Expand Down
10 changes: 9 additions & 1 deletion src/EFCore/Storage/ValueConversion/EnumToNumberConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,22 @@ public class EnumToNumberConverter<TEnum, TNumber> : ValueConverter<TEnum, TNumb
: default;
}

/// <summary>
/// Creates a new instance of this converter. This converter preserves order.
/// </summary>
public EnumToNumberConverter()
: this(null)
{
}

/// <summary>
/// Creates a new instance of this converter. This converter preserves order.
/// </summary>
/// <param name="mappingHints">
/// Hints that can be used by the <see cref="ITypeMappingSource" /> to create data types with appropriate
/// facets for the converted data.
/// </param>
public EnumToNumberConverter(ConverterMappingHints? mappingHints = null)
public EnumToNumberConverter(ConverterMappingHints? mappingHints)
: base(
ToNumber(),
ToEnum(),
Expand Down
10 changes: 9 additions & 1 deletion src/EFCore/Storage/ValueConversion/EnumToStringConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,22 @@ namespace Microsoft.EntityFrameworkCore.Storage.ValueConversion
public class EnumToStringConverter<TEnum> : StringEnumConverter<TEnum, string, TEnum>
where TEnum : struct
{
/// <summary>
/// Creates a new instance of this converter. This converter does not preserve order.
/// </summary>
public EnumToStringConverter()
: this(null)
{
}

/// <summary>
/// Creates a new instance of this converter. This converter does not preserve order.
/// </summary>
/// <param name="mappingHints">
/// Hints that can be used by the <see cref="ITypeMappingSource" /> to create data types with appropriate
/// facets for the converted data.
/// </param>
public EnumToStringConverter(ConverterMappingHints? mappingHints = null)
public EnumToStringConverter(ConverterMappingHints? mappingHints)
: base(
ToString(),
ToEnum(),
Expand Down
17 changes: 16 additions & 1 deletion src/EFCore/Storage/ValueConversion/GuidToBytesConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,21 @@ public class GuidToBytesConverter : ValueConverter<Guid, byte[]>
private static readonly ConverterMappingHints _defaultHints
= new(size: 16, valueGeneratorFactory: (p, t) => new SequentialGuidValueGenerator());

/// <summary>
/// <para>
/// Creates a new instance of this converter.
/// </para>
/// <para>
/// This converter does not preserve order because the ordering of bits in
/// the standard binary representation of a GUID does not match the ordering
/// in the standard string representation.
/// </para>
/// </summary>
public GuidToBytesConverter()
: this(null)
{
}

/// <summary>
/// <para>
/// Creates a new instance of this converter.
Expand All @@ -28,7 +43,7 @@ private static readonly ConverterMappingHints _defaultHints
/// Hints that can be used by the <see cref="ITypeMappingSource" /> to create data types with appropriate
/// facets for the converted data.
/// </param>
public GuidToBytesConverter(ConverterMappingHints? mappingHints = null)
public GuidToBytesConverter(ConverterMappingHints? mappingHints)
: base(
v => v.ToByteArray(),
v => new Guid(v),
Expand Down
10 changes: 9 additions & 1 deletion src/EFCore/Storage/ValueConversion/GuidToStringConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,22 @@ namespace Microsoft.EntityFrameworkCore.Storage.ValueConversion
/// </summary>
public class GuidToStringConverter : StringGuidConverter<Guid, string>
{
/// <summary>
/// Creates a new instance of this converter.
/// </summary>
public GuidToStringConverter()
: this(null)
{
}

/// <summary>
/// Creates a new instance of this converter.
/// </summary>
/// <param name="mappingHints">
/// Hints that can be used by the <see cref="ITypeMappingSource" /> to create data types with appropriate
/// facets for the converted data.
/// </param>
public GuidToStringConverter(ConverterMappingHints? mappingHints = null)
public GuidToStringConverter(ConverterMappingHints? mappingHints)
: base(
ToString(),
ToGuid(),
Expand Down
10 changes: 9 additions & 1 deletion src/EFCore/Storage/ValueConversion/IPAddressToBytesConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,22 @@ public class IPAddressToBytesConverter : ValueConverter<IPAddress?, byte[]?>
{
private static readonly ConverterMappingHints _defaultHints = new(size: 16);

/// <summary>
/// Creates a new instance of this converter.
/// </summary>
public IPAddressToBytesConverter()
: this(null)
{
}

/// <summary>
/// Creates a new instance of this converter.
/// </summary>
/// <param name="mappingHints">
/// Hints that can be used by the <see cref="ITypeMappingSource" /> to create data types with appropriate
/// facets for the converted data.
/// </param>
public IPAddressToBytesConverter(ConverterMappingHints? mappingHints = null)
public IPAddressToBytesConverter(ConverterMappingHints? mappingHints)
: base(
v => v == null ? default : v.GetAddressBytes(),
v => v == null ? default : new IPAddress(v),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,22 @@ public class IPAddressToStringConverter : ValueConverter<IPAddress?, string?>
// IPv4-mapped IPv6 addresses can go up to 45 bytes, e.g. 0000:0000:0000:0000:0000:ffff:192.168.1.1
private static readonly ConverterMappingHints _defaultHints = new(size: 45);

/// <summary>
/// Creates a new instance of this converter.
/// </summary>
public IPAddressToStringConverter()
: this(null)
{
}

/// <summary>
/// Creates a new instance of this converter.
/// </summary>
/// <param name="mappingHints">
/// Hints that can be used by the <see cref="ITypeMappingSource" /> to create data types with appropriate
/// facets for the converted data.
/// </param>
public IPAddressToStringConverter(ConverterMappingHints? mappingHints = null)
public IPAddressToStringConverter(ConverterMappingHints? mappingHints)
: base(
ToString(),
ToIPAddress(),
Expand Down
18 changes: 17 additions & 1 deletion src/EFCore/Storage/ValueConversion/NumberToBytesConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,22 @@ public class NumberToBytesConverter<TNumber> : ValueConverter<TNumber, byte[]>
// ReSharper disable once StaticMemberInGenericType
private static readonly ConverterMappingHints _defaultHints = new(size: GetByteCount());

/// <summary>
/// <para>
/// Creates a new instance of this converter.
/// </para>
/// <para>
/// This converter supports <see cref="double" />, <see cref="float" />, <see cref="decimal" />,
/// <see cref="int" />, <see cref="long" />, <see cref="short" />, <see cref="byte" />,
/// <see cref="uint" />, <see cref="ulong" />, <see cref="ushort" />, <see cref="sbyte" />,
/// and <see cref="char" />.
/// </para>
/// </summary>
public NumberToBytesConverter()
: this(null)
{
}

/// <summary>
/// <para>
/// Creates a new instance of this converter.
Expand All @@ -30,7 +46,7 @@ public class NumberToBytesConverter<TNumber> : ValueConverter<TNumber, byte[]>
/// Hints that can be used by the <see cref="ITypeMappingSource" /> to create data types with appropriate
/// facets for the converted data.
/// </param>
public NumberToBytesConverter(ConverterMappingHints? mappingHints = null)
public NumberToBytesConverter(ConverterMappingHints? mappingHints)
: base(ToBytes(), ToNumber(), _defaultHints.With(mappingHints))
{
}
Expand Down
Loading

0 comments on commit eb44e96

Please sign in to comment.