Skip to content

Commit

Permalink
fix: #625 - marker interfaces for EFCore converters that were not nam…
Browse files Browse the repository at this point in the history
…ed 'EfCoreConverters' caused problems because there were a couple of instances of hardcoded names
  • Loading branch information
SteveDunn committed Jul 20, 2024
1 parent 335abc8 commit 532c649
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 6 deletions.
2 changes: 1 addition & 1 deletion samples/Onion/Domain/Main.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using Vogen;

// We don't need to omit the System.Text.Json converter factory because
// We don't need to emit the System.Text.Json converter factory because
// System.Text.Json, in the Infra project (or anything that references this)
// will have access to the 'fully formed' value objects.
[assembly: VogenDefaults(
Expand Down
11 changes: 8 additions & 3 deletions samples/Onion/Infra/EfCoreScenario.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace UsingTypesGeneratedInTheSameProject;
* a converter generated for it.
*
* Then, we have two ways of registering all of these converters;
* 1. override `ConfigureConventions` and call `RegisterAllInEfCoreConverters`
* 1. override `ConfigureConventions` and call `RegisterAllInVogenEfCoreConverters` (replacing the name after 'RegisterAllIn' with the name of your marker interface - in this example, there's two)
* 2. call `HasVogenConversion` in `OnModelCreating`.
*/

Expand All @@ -25,9 +25,13 @@ namespace UsingTypesGeneratedInTheSameProject;
[EfCoreConverter<Id>]
[EfCoreConverter<Name>]
[EfCoreConverter<Age>]
internal sealed partial class VogenEfCoreConverters1;

// We don't need two marker interfaces; this just demonstrates that you can break them up - see below
// on how they're used (configurationBuilder.RegisterAllInVogenEfCoreConverters2();)
[EfCoreConverter<Department>]
[EfCoreConverter<HireDate>]
internal sealed partial class EfCoreConverters;
internal sealed partial class VogenEfCoreConverters2;

public static class EfCoreScenario
{
Expand Down Expand Up @@ -88,7 +92,8 @@ protected override void ConfigureConventions(ModelConfigurationBuilder configura

// There are two ways of registering these, you can call the generated extension method here,
// or register the converters individually, like below in `OnModelCreating`.
configurationBuilder.RegisterAllInEfCoreConverters();
configurationBuilder.RegisterAllInVogenEfCoreConverters1();
configurationBuilder.RegisterAllInVogenEfCoreConverters2();
}

protected override void OnModelCreating(ModelBuilder builder)
Expand Down
4 changes: 2 additions & 2 deletions src/Vogen/Generators/Conversions/GenerateEfCoreTypes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ public static string GenerateOuterExtensionMethod(EfCoreConverterSpec spec)

string code = _extensionMethodForOuter;

string generatedConverter = $"{spec.SourceType.FullNamespace()}.EfCoreConverters.{spec.VoSymbol.Name}EfCoreValueConverter";
string generatedComparer = $"{spec.SourceType.FullNamespace()}.EfCoreConverters.{spec.VoSymbol.Name}EfCoreValueComparer";
string generatedConverter = $"{spec.SourceType.FullNamespace()}.{spec.SourceType.Name}.{spec.VoSymbol.Name}EfCoreValueConverter";
string generatedComparer = $"{spec.SourceType.FullNamespace()}.{spec.SourceType.Name}.{spec.VoSymbol.Name}EfCoreValueComparer";

code = code.Replace("__CLASS_PREFIX__", spec.VoSymbol.Name);
code = code.Replace("__GENERATED_CONVERTER_NAME__", generatedConverter);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using System.Threading.Tasks;
using Shared;
using Vogen;

namespace SnapshotTests.BugFixes;

// See https://github.com/SteveDunn/Vogen/issues/625
public class Bug625_EFCoreConverters_uses_wrong_marker_interface_name
{
[Fact]
public async Task A_marker_interface_not_named_EfCoreConverters_is_still_referenced_and_that_name_is_used_in_the_generated_code()
{
var source = """
using System;
using Vogen;
[assembly: VogenDefaults(
systemTextJsonConverterFactoryGeneration: SystemTextJsonConverterFactoryGeneration.Omit,
conversions: Conversions.SystemTextJson |
Conversions.TypeConverter |
Conversions.SystemTextJson)]
namespace Foo;
[ValueObject<DateTime>]
public partial struct HireDate;
[ValueObject<string>]
public partial struct Name;
[ValueObject<int>]
public partial struct Age;
[EfCoreConverter<HireDate>]
[EfCoreConverter<Name>]
[EfCoreConverter<Age>]
internal sealed partial class VogenEfCoreConverters;
""";

await new SnapshotRunner<ValueObjectGenerator>()
.WithSource(source)
.IgnoreInitialCompilationErrors()
.RunOn(TargetFramework.Net8_0);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@


0 comments on commit 532c649

Please sign in to comment.