Skip to content

Commit 9b1f809

Browse files
committed
Moved Equals() and Compare() helpers from generated into helper class.
1 parent bf96aa6 commit 9b1f809

File tree

6 files changed

+27
-24
lines changed

6 files changed

+27
-24
lines changed

DomainModeling.Generator/IdentityGenerator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -581,7 +581,7 @@ namespace {containingNamespace}
581581
static {idTypeName} IValueWrapper<{idTypeName}, {underlyingTypeFullyQualifiedName}>.Deserialize({underlyingTypeFullyQualifiedName} value)
582582
{{
583583
{(existingComponents.HasFlags(IdTypeComponents.UnsettableValue) ? "// To instead get safe syntax, make the Value property '{ get; private init; }' (or let the source generator implement it)" : "")}
584-
{(existingComponents.HasFlags(IdTypeComponents.UnsettableValue) ? $"return System.Runtime.CompilerServices.Unsafe.As<{underlyingTypeFullyQualifiedName}, {idTypeName}>(ref value);" : "")}
584+
{(existingComponents.HasFlags(IdTypeComponents.UnsettableValue) ? $"return Unsafe.As<{underlyingTypeFullyQualifiedName}, {idTypeName}>(ref value);" : "")}
585585
{(existingComponents.HasFlags(IdTypeComponents.UnsettableValue) ? "//" : "")}return new {idTypeName}() {{ Value = value }};
586586
}}
587587
{(existingComponents.HasFlags(IdTypeComponents.DeserializeFromUnderlying) ? "*/" : "")}

DomainModeling.Generator/TypeSymbolExtensions.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -684,7 +684,7 @@ public static string CreateEqualityExpression(this ITypeSymbol typeSymbol, strin
684684
// DO NOT REORDER
685685

686686
// Not yet source-generated
687-
if (typeSymbol.TypeKind == TypeKind.Error) return $"Equals(this.{memberName}, other.{memberName})";
687+
if (typeSymbol.TypeKind == TypeKind.Error) return $"{ComparisonsNamespace}.InferredTypeDefaultComparer.Equals(this.{memberName}, other.{memberName})";
688688

689689
if (typeSymbol.SpecialType == SpecialType.System_String) return String.Format(stringVariant, memberName);
690690

@@ -733,7 +733,7 @@ public static string CreateComparisonExpression(this ITypeSymbol typeSymbol, str
733733
// DO NOT REORDER
734734

735735
// Not yet source-generated
736-
if (typeSymbol.TypeKind == TypeKind.Error) return $"Compare(this.{memberName}, other.{memberName})";
736+
if (typeSymbol.TypeKind == TypeKind.Error) return $"{ComparisonsNamespace}.InferredTypeDefaultComparer.Compare(this.{memberName}, other.{memberName})";
737737

738738
// Collections have not been implemented, as we do not generate CompareTo() if any data member is not IComparable (as is the case for collections)
739739

DomainModeling.Generator/ValueObjectGenerator.cs

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -322,33 +322,14 @@ public bool Equals({typeName}? other)
322322
}}
323323
{(existingComponents.HasFlags(ValueObjectTypeComponents.EqualsMethod) ? " */" : "")}
324324
325-
/// <summary>
326-
/// Provides type inference when comparing types that are entirely source-generated. The current code's source generator does not know the appropriate namespace, because the type is being generated at the same time, thus necessitating type inference.
327-
/// </summary>
328-
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
329-
private static bool Equals<T>(T left, T right)
330-
{{
331-
return EqualityComparer<T>.Default.Equals(left, right);
332-
}}
333-
334325
{(existingComponents.HasFlags(ValueObjectTypeComponents.CompareToMethod) ? "/*" : "")}
335-
{(isComparable ? "" : "/*")}
336-
// This method is generated only if the ValueObject implements IComparable<T> against its own type and each data member implements IComparable<T> against its own type
326+
{(isComparable ? "" : "/* Generated only if the ValueObject implements IComparable<T> against its own type and each data member implements IComparable<T> against its own type")}
337327
public int CompareTo({typeName}? other)
338328
{{
339329
if (other is null) return +1;
340330
341331
{compareToBodyIfInstanceNonNull}
342332
}}
343-
344-
/// <summary>
345-
/// Provides type inference when comparing types that are entirely source-generated. The current code's source generator does not know the appropriate namespace, because the type is being generated at the same time, thus necessitating type inference.
346-
/// </summary>
347-
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
348-
private static int Compare<T>(T left, T right)
349-
{{
350-
return Comparer<T>.Default.Compare(left, right);
351-
}}
352333
{(isComparable ? "" : "*/")}
353334
{(existingComponents.HasFlags(ValueObjectTypeComponents.CompareToMethod) ? "*/" : "")}
354335

DomainModeling.Generator/WrapperValueObjectGenerator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -495,7 +495,7 @@ namespace {containingNamespace}
495495
496496
{(existingComponents.HasFlags(WrapperValueObjectTypeComponents.DeserializeFromUnderlying) ? "/*" : "")}
497497
{(existingComponents.HasFlags(WrapperValueObjectTypeComponents.UnsettableValue) ? $@"
498-
[System.Runtime.CompilerServices.UnsafeAccessor(System.Runtime.CompilerServices.UnsafeAccessorKind.Field, Name = ""{valueFieldName}"")]
498+
[UnsafeAccessor(UnsafeAccessorKind.Field, Name = ""{valueFieldName}"")]
499499
private static extern ref {underlyingTypeFullyQualifiedName} GetValueFieldReference({typeName} instance);" : "")}
500500
501501
/// <summary>
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using System.Runtime.CompilerServices;
2+
3+
namespace Architect.DomainModeling.Comparisons;
4+
5+
/// <summary>
6+
/// Performs default comparisons with type inference, where the standard syntax does not allow for type inference.
7+
/// </summary>
8+
public static class InferredTypeDefaultComparer
9+
{
10+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
11+
public static bool Equals<T>(T left, T right)
12+
{
13+
return EqualityComparer<T>.Default.Equals(left, right);
14+
}
15+
16+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
17+
public static int Compare<T>(T left, T right)
18+
{
19+
return Comparer<T>.Default.Compare(left, right);
20+
}
21+
}

DomainModeling/DomainModeling.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ Correct string comparisons with EF:
5252

5353
Performance:
5454
- Enhancement: Reduced assembly size by having source-generated WrapperValueObject/Identity types use generic JSON serializers instead of generating their own.
55+
- Enhancement: Reduced assembly size by moving the type-inference Equals() and Compare() helpers on generated ValueObjects into a helper class.
5556
- Enhancement: Improved source generator performance.
5657

5758
Misc improvements:

0 commit comments

Comments
 (0)