Skip to content

Commit

Permalink
Merge pull request #16 from Cysharp/add_constructor_for_ef
Browse files Browse the repository at this point in the history
Add parameter less constructor to EntityFrameworkValueConverter
  • Loading branch information
neuecc authored Oct 25, 2022
2 parents fde0328 + 7263807 commit 023ca8c
Show file tree
Hide file tree
Showing 8 changed files with 83 additions and 8 deletions.
11 changes: 9 additions & 2 deletions UnitGenerator.sln
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.30711.63
# Visual Studio Version 17
VisualStudioVersion = 17.3.32929.385
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "UnitGenerator", "src\UnitGenerator\UnitGenerator.csproj", "{208D16B4-5904-4E06-8A41-D06C3FE7293E}"
EndProject
Expand All @@ -17,6 +17,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "UnitGenerator.Tests", "test
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FileGenerate", "sandbox\FileGenerate\FileGenerate.csproj", "{F8353A7A-290E-41D7-A6F8-8D8DBDD44433}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "EntityFrameworkApp", "src\EntityFrameworkApp\EntityFrameworkApp.csproj", "{51AE7857-4223-40FE-AEA9-F0E64C5F8238}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -39,6 +41,10 @@ Global
{F8353A7A-290E-41D7-A6F8-8D8DBDD44433}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F8353A7A-290E-41D7-A6F8-8D8DBDD44433}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F8353A7A-290E-41D7-A6F8-8D8DBDD44433}.Release|Any CPU.Build.0 = Release|Any CPU
{51AE7857-4223-40FE-AEA9-F0E64C5F8238}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{51AE7857-4223-40FE-AEA9-F0E64C5F8238}.Debug|Any CPU.Build.0 = Debug|Any CPU
{51AE7857-4223-40FE-AEA9-F0E64C5F8238}.Release|Any CPU.ActiveCfg = Release|Any CPU
{51AE7857-4223-40FE-AEA9-F0E64C5F8238}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -48,6 +54,7 @@ Global
{8AF2ABCF-38FB-4D64-A051-8735AF83E223} = {34EB4113-923D-4855-979C-A0467461B75C}
{5DA06D43-A023-4464-B856-8BB42E8E4A05} = {187FBF64-D2AA-444D-AFB1-CE999BC6AD34}
{F8353A7A-290E-41D7-A6F8-8D8DBDD44433} = {34EB4113-923D-4855-979C-A0467461B75C}
{51AE7857-4223-40FE-AEA9-F0E64C5F8238} = {34EB4113-923D-4855-979C-A0467461B75C}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {A64DF779-7829-414F-9E6E-3AF349486508}
Expand Down
2 changes: 1 addition & 1 deletion sandbox/FileGenerate/FileGenerate.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<TargetFramework>net6.0</TargetFramework>

<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
<CompilerGeneratedFilesOutputPath>$(ProjectDir)..\Generated</CompilerGeneratedFilesOutputPath>
Expand Down
27 changes: 27 additions & 0 deletions src/EntityFrameworkApp/DbContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using Microsoft.EntityFrameworkCore;
using UnitGenerator;

namespace EntityFrameworkApp;

[UnitOf(typeof(int),
UnitGenerateOptions.ParseMethod |
UnitGenerateOptions.EntityFrameworkValueConverter)]
public readonly partial struct UserId { }

public class User
{
public UserId UserId { get; set; }
}

public class SampleDbContext : DbContext
{
public SampleDbContext() { }
public SampleDbContext(DbContextOptions<SampleDbContext> options) : base(options) { }

public DbSet<User> Users { get; set; } = default!;

protected override void ConfigureConventions(ModelConfigurationBuilder configurationBuilder)
{
configurationBuilder.Properties<UserId>().HaveConversion<UserId.UserIdValueConverter>();
}
}
18 changes: 18 additions & 0 deletions src/EntityFrameworkApp/EntityFrameworkApp.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="6.0.10" />
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="6.0.10" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\UnitGenerator\UnitGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false"/>
</ItemGroup>
</Project>
10 changes: 10 additions & 0 deletions src/EntityFrameworkApp/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using EntityFrameworkApp;
using Microsoft.EntityFrameworkCore;

var options = new DbContextOptionsBuilder<SampleDbContext>()
.UseInMemoryDatabase("SampleDb")
.Options;
var context = new SampleDbContext(options);
var user = context.Users.SingleOrDefault(x => x.UserId == new UserId(1));

Console.WriteLine("Hello, World!");
14 changes: 10 additions & 4 deletions src/UnitGenerator/CodeTemplate.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
// ------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
// Runtime Version: 17.0.0.0
// このコードはツールによって生成されました。
// ランタイム バージョン: 17.0.0.0
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// このファイルへの変更は、正しくない動作の原因になる可能性があり、
// コードが再生成されると失われます。
// </auto-generated>
// ------------------------------------------------------------------------------
namespace UnitGenerator
Expand Down Expand Up @@ -516,6 +516,12 @@ public virtual string TransformText()
this.Write(this.ToStringHelper.ToStringWithCulture(Type));
this.Write(">\r\n {\r\n public ");
this.Write(this.ToStringHelper.ToStringWithCulture(Name));
this.Write("ValueConverter()\r\n : base(\r\n convertToProvi" +
"derExpression: x => x.value,\r\n convertFromProviderExpress" +
"ion: x => new ");
this.Write(this.ToStringHelper.ToStringWithCulture(Name));
this.Write("(x))\r\n {\r\n }\r\n\r\n public ");
this.Write(this.ToStringHelper.ToStringWithCulture(Name));
this.Write(@"ValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null)
: base(
convertToProviderExpression: x => x.value,
Expand Down
7 changes: 7 additions & 0 deletions src/UnitGenerator/CodeTemplate.tt
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,13 @@ namespace <#= Namespace #>
// UnitGenerateOptions.EntityFrameworkValueConverter
public class <#= Name #>ValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter<<#= Name #>, <#= Type #>>
{
public <#= Name #>ValueConverter()
: base(
convertToProviderExpression: x => x.value,
convertFromProviderExpression: x => new <#= Name #>(x))
{
}

public <#= Name #>ValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null)
: base(
convertToProviderExpression: x => x.value,
Expand Down
2 changes: 1 addition & 1 deletion tests/UnitGenerator.Tests/UnitGenerator.Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<TargetFramework>net6.0</TargetFramework>
<LangVersion>latest</LangVersion>
<IsPackable>false</IsPackable>
</PropertyGroup>
Expand Down

0 comments on commit 023ca8c

Please sign in to comment.