EFcoreExtension
All checks were successful
Publish NuGet packages / publish (push) Successful in 1m15s
All checks were successful
Publish NuGet packages / publish (push) Successful in 1m15s
This commit is contained in:
@@ -81,6 +81,24 @@ namespace QWERTYkez.Mensura
|
||||
sb.AppendLine("}");
|
||||
|
||||
spc.AddSource(".ComplexUnits.MensuraBinder.g.cs", SourceText.From(sb.ToString(), Encoding.UTF8));
|
||||
|
||||
|
||||
sb = new StringBuilder();
|
||||
sb.AppendLine("using Microsoft.EntityFrameworkCore;");
|
||||
sb.AppendLine("namespace QWERTYkez.Mensura.Extensions;");
|
||||
sb.AppendLine("public static partial class EFCoreExtension");
|
||||
sb.AppendLine("{");
|
||||
sb.AppendLine(" [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]");
|
||||
sb.AppendLine(" internal static void AddGeneratedComplexConverters(ModelConfigurationBuilder configurationBuilder)");
|
||||
sb.AppendLine(" {");
|
||||
foreach (var structInfo in structs)
|
||||
{
|
||||
sb.AppendLine($" configurationBuilder.Properties<{structInfo.TypeName}>().HaveConversion<MensuraUnitConverter<{structInfo.TypeName}>>();");
|
||||
}
|
||||
sb.AppendLine(" }");
|
||||
sb.AppendLine("}");
|
||||
|
||||
spc.AddSource(".ComplexUnits.EFCoreExtension.g.cs", SourceText.From(sb.ToString(), Encoding.UTF8));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@@ -67,6 +67,24 @@ namespace QWERTYkez.Mensura
|
||||
sb.AppendLine("}");
|
||||
|
||||
spc.AddSource(".Units.MensuraBinder.g.cs", SourceText.From(sb.ToString(), Encoding.UTF8));
|
||||
|
||||
|
||||
sb = new StringBuilder();
|
||||
sb.AppendLine("using Microsoft.EntityFrameworkCore;");
|
||||
sb.AppendLine("namespace QWERTYkez.Mensura.Extensions;");
|
||||
sb.AppendLine("public static partial class EFCoreExtension");
|
||||
sb.AppendLine("{");
|
||||
sb.AppendLine(" [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]");
|
||||
sb.AppendLine(" internal static void AddGeneratedConverters(ModelConfigurationBuilder configurationBuilder)");
|
||||
sb.AppendLine(" {");
|
||||
foreach (var structInfo in structs)
|
||||
{
|
||||
sb.AppendLine($" configurationBuilder.Properties<{structInfo.TypeName}>().HaveConversion<MensuraUnitConverter<{structInfo.TypeName}>>();");
|
||||
}
|
||||
sb.AppendLine(" }");
|
||||
sb.AppendLine("}");
|
||||
|
||||
spc.AddSource(".Units.EFCoreExtension.g.cs", SourceText.From(sb.ToString(), Encoding.UTF8));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@@ -7,16 +7,16 @@
|
||||
private static readonly double scalarDouble = 500.0; // прибавляемое значение в мм
|
||||
|
||||
// Коллекции единиц
|
||||
private static readonly Length[] unitsArray = new[] { Length.Meter, Length._MilliMeter };
|
||||
private static readonly Length?[] nullableUnitsArray = new Length?[] { Length.Meter, null, Length._MilliMeter };
|
||||
private static readonly List<Length> unitsList = new List<Length> { Length.Meter, Length._MilliMeter };
|
||||
private static readonly List<Length?> nullableUnitsList = new List<Length?> { Length.Meter, null, Length._MilliMeter };
|
||||
private static readonly Length[] unitsArray = [Length.Meter, Length._MilliMeter];
|
||||
private static readonly Length?[] nullableUnitsArray = [Length.Meter, null, Length._MilliMeter];
|
||||
private static readonly List<Length> unitsList = [Length.Meter, Length._MilliMeter];
|
||||
private static readonly List<Length?> nullableUnitsList = [Length.Meter, null, Length._MilliMeter];
|
||||
|
||||
// Коллекции double
|
||||
private static readonly double[] doubleArray = new double[] { 200.0, 300.0 };
|
||||
private static readonly double?[] nullableDoubleArray = new double?[] { 200.0, null, 300.0 };
|
||||
private static readonly List<double> doubleList = new List<double> { 200.0, 300.0 };
|
||||
private static readonly List<double?> nullableDoubleList = new List<double?> { 200.0, null, 300.0 };
|
||||
private static readonly double[] doubleArray = [200.0, 300.0];
|
||||
private static readonly double?[] nullableDoubleArray = [200.0, null, 300.0];
|
||||
private static readonly List<double> doubleList = [200.0, 300.0];
|
||||
private static readonly List<double?> nullableDoubleList = [200.0, null, 300.0];
|
||||
|
||||
// ====================== 1. T[] + double ======================
|
||||
[Fact]
|
||||
@@ -303,7 +303,7 @@
|
||||
[Fact]
|
||||
public void Plus_NullArray_ReturnsNull()
|
||||
{
|
||||
Length[] nullArray = null;
|
||||
Length[] nullArray = null!;
|
||||
var result = nullArray.Plus<Length>(5.0);
|
||||
Assert.Null(result);
|
||||
}
|
||||
@@ -311,7 +311,7 @@
|
||||
[Fact]
|
||||
public void Plus_NullList_ReturnsNull()
|
||||
{
|
||||
List<Length> nullList = null;
|
||||
List<Length> nullList = null!;
|
||||
var result = nullList.Plus<Length>(5.0);
|
||||
Assert.Null(result);
|
||||
}
|
||||
|
||||
@@ -20,6 +20,9 @@
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="Microsoft.Data.Sqlite.Core" Version="10.0.9" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="10.0.9" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="10.0.9" />
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="18.6.0" />
|
||||
<PackageReference Include="Newtonsoft.Json" Version="13.0.4" />
|
||||
<PackageReference Include="xunit.runner.visualstudio" Version="3.1.5">
|
||||
@@ -31,8 +34,6 @@
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\QWERTYkez.Mensura\QWERTYkez.Mensura.csproj" />
|
||||
<InternalsVisibleTo Include="..\QWERTYkez.Mensura\QWERTYkez.Mensura.csproj" />
|
||||
|
||||
<ProjectReference Include="..\QWERTYkez.Mensura.Generator\QWERTYkez.Mensura.Generator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
|
||||
</ItemGroup>
|
||||
|
||||
|
||||
98
QWERTYkez.Mensura.Tests/TestEntity.cs
Normal file
98
QWERTYkez.Mensura.Tests/TestEntity.cs
Normal file
@@ -0,0 +1,98 @@
|
||||
using Microsoft.Data.Sqlite;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace QWERTYkez.Mensura.Tests
|
||||
{
|
||||
public class TestEntity1
|
||||
{
|
||||
public int Id { get; set; }
|
||||
public Length Length { get; set; }
|
||||
}
|
||||
|
||||
public class TestEntity2
|
||||
{
|
||||
public int Id { get; set; }
|
||||
public double Length { get; set; }
|
||||
}
|
||||
|
||||
public class SharedDbContext(DbContextOptions<SharedDbContext> options) : DbContext(options)
|
||||
{
|
||||
public DbSet<TestEntity1> Entities1 { get; set; }
|
||||
public DbSet<TestEntity2> Entities2 { get; set; }
|
||||
|
||||
protected override void ConfigureConventions(ModelConfigurationBuilder configurationBuilder)
|
||||
{
|
||||
configurationBuilder.UseMensuraUnits();
|
||||
}
|
||||
}
|
||||
|
||||
public class IdentityConversionTest : IDisposable
|
||||
{
|
||||
private readonly SqliteConnection _connection;
|
||||
|
||||
public IdentityConversionTest()
|
||||
{
|
||||
_connection = new SqliteConnection("Filename=:memory:");
|
||||
_connection.Open();
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
_connection?.Dispose();
|
||||
GC.SuppressFinalize(this);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Length_And_Double_Are_Stored_Identically()
|
||||
{
|
||||
// 1. Сохраняем Length → читаем как double
|
||||
double savedDoubleFromLength;
|
||||
using (var context = new SharedDbContext(new DbContextOptionsBuilder<SharedDbContext>().UseSqlite(_connection).Options))
|
||||
{
|
||||
context.Database.EnsureCreated();
|
||||
var expectedLength = Length.Meter; // 1000 мм
|
||||
context.Entities1.Add(new TestEntity1 { Length = expectedLength });
|
||||
context.SaveChanges();
|
||||
|
||||
using var cmd = context.Database.GetDbConnection().CreateCommand();
|
||||
cmd.CommandText = "SELECT Length FROM Entities1";
|
||||
savedDoubleFromLength = (double)cmd.ExecuteScalar()!;
|
||||
}
|
||||
|
||||
// 2. Сохраняем double → читаем как Length
|
||||
double savedDoubleFromDouble = 1234.5;
|
||||
using (var context = new SharedDbContext(new DbContextOptionsBuilder<SharedDbContext>().UseSqlite(_connection).Options))
|
||||
{
|
||||
context.Database.EnsureCreated();
|
||||
context.Entities2.Add(new TestEntity2 { Length = savedDoubleFromDouble });
|
||||
context.SaveChanges();
|
||||
|
||||
using var cmd = context.Database.GetDbConnection().CreateCommand();
|
||||
cmd.CommandText = "SELECT Length FROM Entities2";
|
||||
savedDoubleFromDouble = (double)cmd.ExecuteScalar()!; // должно быть 1234.5
|
||||
}
|
||||
|
||||
// 3. Проверяем, что сырые значения сохранены корректно
|
||||
Assert.Equal(1000.0, savedDoubleFromLength);
|
||||
Assert.Equal(1234.5, savedDoubleFromDouble);
|
||||
|
||||
// 4. Проверяем обратимость: читаем из Entities1 как TestEntity2
|
||||
using (var context = new SharedDbContext(new DbContextOptionsBuilder<SharedDbContext>().UseSqlite(_connection).Options))
|
||||
{
|
||||
var fromLengthTable = context.Entities2
|
||||
.FromSqlRaw("SELECT Id, Length FROM Entities1")
|
||||
.First();
|
||||
Assert.Equal(1000.0, fromLengthTable.Length);
|
||||
}
|
||||
|
||||
// 5. Проверяем обратимость: читаем из Entities2 как TestEntity1
|
||||
using (var context = new SharedDbContext(new DbContextOptionsBuilder<SharedDbContext>().UseSqlite(_connection).Options))
|
||||
{
|
||||
var fromDoubleTable = context.Entities1
|
||||
.FromSqlRaw("SELECT Id, Length FROM Entities2")
|
||||
.First();
|
||||
Assert.Equal(1234.5, (double)fromDoubleTable.Length);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
20
QWERTYkez.Mensura/Extensions/EFCoreExtension.cs
Normal file
20
QWERTYkez.Mensura/Extensions/EFCoreExtension.cs
Normal file
@@ -0,0 +1,20 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
|
||||
|
||||
namespace QWERTYkez.Mensura.Extensions;
|
||||
|
||||
public static partial class EFCoreExtension
|
||||
{
|
||||
/// <summary>
|
||||
/// Регистрирует ValueConverter для всех типов, реализующих IMensuraUnit.
|
||||
/// </summary>
|
||||
public static void UseMensuraUnits(this ModelConfigurationBuilder configurationBuilder)
|
||||
{
|
||||
AddGeneratedConverters(configurationBuilder);
|
||||
AddGeneratedComplexConverters(configurationBuilder);
|
||||
}
|
||||
|
||||
internal class MensuraUnitConverter<U>()
|
||||
: ValueConverter<U, double>(unit => unit.ToDouble(), value => value.ToUnit<U>())
|
||||
where U : struct, IMensuraUnit, IEquatable<U> { }
|
||||
}
|
||||
@@ -9,7 +9,7 @@ internal class ListMimic<T>
|
||||
}
|
||||
|
||||
[StructLayout(LayoutKind.Explicit, Size = 16)]
|
||||
public struct NullableDoubleMimic
|
||||
internal struct NullableDoubleMimic
|
||||
{
|
||||
[FieldOffset(0)] public bool HasValue;
|
||||
[FieldOffset(8)] public double Value;
|
||||
|
||||
@@ -10,10 +10,18 @@
|
||||
<NoWarn>1701;1702;IDE1006</NoWarn>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\QWERTYkez.Mensura.Generator\QWERTYkez.Mensura.Generator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
|
||||
<InternalsVisibleTo Include="QWERTYkez.Mensura.Tests" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" PrivateAssets="All" />
|
||||
<ProjectReference Include="..\QWERTYkez.Mensura.Generator\QWERTYkez.Mensura.Generator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
|
||||
<InternalsVisibleTo Include="QWERTYkez.Mensura.Tests" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="6.0.36" Condition="'$(TargetFramework)' == 'net6.0'" PrivateAssets="All" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="7.0.20" Condition="'$(TargetFramework)' == 'net7.0'" PrivateAssets="All" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.28" Condition="'$(TargetFramework)' == 'net8.0'" PrivateAssets="All" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="9.0.17" Condition="'$(TargetFramework)' == 'net9.0'" PrivateAssets="All" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="10.0.9" Condition="'$(TargetFramework)' == 'net10.0'" PrivateAssets="All" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
File diff suppressed because it is too large
Load Diff
@@ -1,70 +1,70 @@
|
||||
#if DEBUG
|
||||
namespace QWERTYkez.Mensura.Units;
|
||||
//#if DEBUG
|
||||
//namespace QWERTYkez.Mensura.Units;
|
||||
|
||||
/// <summary>
|
||||
/// Base value is MilliMeters
|
||||
/// </summary>
|
||||
[DebuggerDisplay("mm = {_MilliMeters.ToString(\"0.###\")}, m = {Meters.ToString(\"0.###\")}")]
|
||||
public readonly partial record struct XXXXXXXX
|
||||
{
|
||||
public static XXXXXXXX MilliMeter { get; } = new(1);
|
||||
[NotMapped, JsonIgnore, IgnoreDataMember] public double _MilliMeters { get => _Value; init => _Value = value; }
|
||||
///// <summary>
|
||||
///// Base value is MilliMeters
|
||||
///// </summary>
|
||||
//[DebuggerDisplay("mm = {_MilliMeters.ToString(\"0.###\")}, m = {Meters.ToString(\"0.###\")}")]
|
||||
//public readonly partial record struct XXXXXXXX
|
||||
//{
|
||||
// public static XXXXXXXX MilliMeter { get; } = new(1);
|
||||
// [NotMapped, JsonIgnore, IgnoreDataMember] public double _MilliMeters { get => _Value; init => _Value = value; }
|
||||
|
||||
|
||||
public static XXXXXXXX CentiMeter { get; } = new(XXXXXXXXConv.CentiMeters.To(1));
|
||||
[NotMapped, JsonIgnore, IgnoreDataMember]
|
||||
public double CentiMeters
|
||||
{
|
||||
get => XXXXXXXXConv.CentiMeters.From(_Value);
|
||||
init
|
||||
{
|
||||
_Value = XXXXXXXXConv.CentiMeters.To(value);
|
||||
}
|
||||
}
|
||||
// public static XXXXXXXX CentiMeter { get; } = new(XXXXXXXXConv.CentiMeters.To(1));
|
||||
// [NotMapped, JsonIgnore, IgnoreDataMember]
|
||||
// public double CentiMeters
|
||||
// {
|
||||
// get => XXXXXXXXConv.CentiMeters.From(_Value);
|
||||
// init
|
||||
// {
|
||||
// _Value = XXXXXXXXConv.CentiMeters.To(value);
|
||||
// }
|
||||
// }
|
||||
|
||||
public static XXXXXXXX DeciMeter { get; } = new(XXXXXXXXConv.DeciMeters.To(1));
|
||||
[NotMapped, JsonIgnore, IgnoreDataMember]
|
||||
public double DeciMeters
|
||||
{
|
||||
get => XXXXXXXXConv.DeciMeters.From(_Value);
|
||||
init => _Value = XXXXXXXXConv.DeciMeters.To(value);
|
||||
}
|
||||
// public static XXXXXXXX DeciMeter { get; } = new(XXXXXXXXConv.DeciMeters.To(1));
|
||||
// [NotMapped, JsonIgnore, IgnoreDataMember]
|
||||
// public double DeciMeters
|
||||
// {
|
||||
// get => XXXXXXXXConv.DeciMeters.From(_Value);
|
||||
// init => _Value = XXXXXXXXConv.DeciMeters.To(value);
|
||||
// }
|
||||
|
||||
public static XXXXXXXX Meter { get; } = new(XXXXXXXXConv.Meters.To(1));
|
||||
[NotMapped, JsonIgnore, IgnoreDataMember]
|
||||
public double Meters
|
||||
{
|
||||
get => XXXXXXXXConv.Meters.From(_Value);
|
||||
init => _Value = XXXXXXXXConv.Meters.To(value);
|
||||
}
|
||||
// public static XXXXXXXX Meter { get; } = new(XXXXXXXXConv.Meters.To(1));
|
||||
// [NotMapped, JsonIgnore, IgnoreDataMember]
|
||||
// public double Meters
|
||||
// {
|
||||
// get => XXXXXXXXConv.Meters.From(_Value);
|
||||
// init => _Value = XXXXXXXXConv.Meters.To(value);
|
||||
// }
|
||||
|
||||
public static XXXXXXXX KiloMeter { get; } = new(XXXXXXXXConv.KiloMeters.To(1));
|
||||
[NotMapped, JsonIgnore, IgnoreDataMember]
|
||||
public double KiloMeters
|
||||
{
|
||||
get => XXXXXXXXConv.KiloMeters.From(_Value);
|
||||
init => _Value = XXXXXXXXConv.KiloMeters.To(value);
|
||||
}
|
||||
// public static XXXXXXXX KiloMeter { get; } = new(XXXXXXXXConv.KiloMeters.To(1));
|
||||
// [NotMapped, JsonIgnore, IgnoreDataMember]
|
||||
// public double KiloMeters
|
||||
// {
|
||||
// get => XXXXXXXXConv.KiloMeters.From(_Value);
|
||||
// init => _Value = XXXXXXXXConv.KiloMeters.To(value);
|
||||
// }
|
||||
|
||||
|
||||
public XXXXXXXX AddMilliMeters(double value) => new(_Value + value);
|
||||
public XXXXXXXX AddCentiMeters(double value) => new(_Value + XXXXXXXXConv.CentiMeters.To(value));
|
||||
public XXXXXXXX AddDeciMeters(double value) => new(_Value + XXXXXXXXConv.DeciMeters.To(value));
|
||||
public XXXXXXXX AddMeters(double value) => new(_Value + XXXXXXXXConv.Meters.To(value));
|
||||
public XXXXXXXX AddKiloMeters(double value) => new(_Value + XXXXXXXXConv.KiloMeters.To(value));
|
||||
}
|
||||
// public XXXXXXXX AddMilliMeters(double value) => new(_Value + value);
|
||||
// public XXXXXXXX AddCentiMeters(double value) => new(_Value + XXXXXXXXConv.CentiMeters.To(value));
|
||||
// public XXXXXXXX AddDeciMeters(double value) => new(_Value + XXXXXXXXConv.DeciMeters.To(value));
|
||||
// public XXXXXXXX AddMeters(double value) => new(_Value + XXXXXXXXConv.Meters.To(value));
|
||||
// public XXXXXXXX AddKiloMeters(double value) => new(_Value + XXXXXXXXConv.KiloMeters.To(value));
|
||||
//}
|
||||
|
||||
|
||||
internal readonly struct XXXXXXXXConv
|
||||
{
|
||||
private XXXXXXXXConv(double multiplicator) => this.Multiplicator = multiplicator;
|
||||
public double To(double value) => value * Multiplicator;
|
||||
public double From(double value) => value / Multiplicator;
|
||||
public double Multiplicator { get; init; }
|
||||
public static XXXXXXXXConv MilliMeters { get; } = new(1);
|
||||
public static XXXXXXXXConv CentiMeters { get; } = new(10);
|
||||
public static XXXXXXXXConv DeciMeters { get; } = new(100);
|
||||
public static XXXXXXXXConv Meters { get; } = new(1000);
|
||||
public static XXXXXXXXConv KiloMeters { get; } = new(1000000);
|
||||
}
|
||||
#endif
|
||||
//internal readonly struct XXXXXXXXConv
|
||||
//{
|
||||
// private XXXXXXXXConv(double multiplicator) => this.Multiplicator = multiplicator;
|
||||
// public double To(double value) => value * Multiplicator;
|
||||
// public double From(double value) => value / Multiplicator;
|
||||
// public double Multiplicator { get; init; }
|
||||
// public static XXXXXXXXConv MilliMeters { get; } = new(1);
|
||||
// public static XXXXXXXXConv CentiMeters { get; } = new(10);
|
||||
// public static XXXXXXXXConv DeciMeters { get; } = new(100);
|
||||
// public static XXXXXXXXConv Meters { get; } = new(1000);
|
||||
// public static XXXXXXXXConv KiloMeters { get; } = new(1000000);
|
||||
//}
|
||||
//#endif
|
||||
Reference in New Issue
Block a user