Skip to content

Generated code is only supported on .NET 7 and newer #19

Description

@Sewer56

Context

TVO generates dead code that is unfriendly towards runtimes that are older than .NET 7.

Explanation

TVO, by default generates types for the following augments, regardless of whether the features are used or not:

  • DefaultEqualityComparerAugment.g.cs
  • DefaultValueAugment.g.cs
  • EfCoreAugment.g.cs
  • JsonAugment.g.cs
  • IDefaultEqualityComparer.g.cs
  • IDefaultValue.g.cs
  • IValueObject.g.cs

The first 4 can only be used on .NET 5 and above due to ExcludeFromCodeCoverage with Justification.

/// <summary>
/// Augment to enable JSON support by generting a JSON converter for the value object.
/// </summary>
/// <remarks>
/// This only supports <c>System.Text.Json</c> at the moment.
/// </remarks>
[global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage(Justification = "Auto-generated")]
internal sealed class JsonAugment : global::TransparentValueObjects.IAugment { }

The last 3 can only be used on (.NET 6 with LangVersion==preview) or .NET 7+, due to use of static abstract.

/// <summary>
/// Represents the interface for <see cref="global::TransparentValueObjects.DefaultEqualityComparerAugment"/>
/// with members that the consumer has to implement to support the augment.
/// </summary>
internal interface IDefaultEqualityComparer<out TValueObject, TInnerValue>
    where TValueObject : global::TransparentValueObjects.IValueObject<TInnerValue>
    where TInnerValue : notnull
{
    /// <summary>
    /// Gets a equality comparer for the inner value type to be used by default.
    /// </summary>
    public static abstract global::System.Collections.Generic.IEqualityComparer<TInnerValue> InnerValueDefaultEqualityComparer { get; }
}

Expected Outcome

  • Unused code is not generated.

    • This code still ends up in the DLL if not trimmed, increasing size.
  • ExcludeFromCodeCoverage w/ Justification is used on .NET 5+ only. Older runtimes use no justification.

Alternatively source generator warns if TargetFramework is set too low.

Supporting Evidence

When no augments used.

20231205_23h06m37s

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions