Vogen icon indicating copy to clipboard operation
Vogen copied to clipboard

ValueObject of double? produces CS8607 warning in the generated file.

Open r14958 opened this issue 6 months ago • 2 comments

Describe the bug

When I try to create a nullable int value object, such as

[ValueObject<double?>] public readonly partial record struct Test;

The compiler reports a warning CS8607: A possible null value may not be used for a type marked with [NotNull] or [DisallowNull] and points to the return line of code in the generated code file:

public readonly override global::System.Int32 GetHashCode() { return global::System.Collections.Generic.EqualityComparer<System.Nullable<System.Double>>.Default.GetHashCode(Value); }

I prefer to TreatWarningsAsErrors=True, so this results in a compilation error. Is this behavior expected? Are nullable value types not supported? Is there a workaround or way to suppress the warning/error in the generated file? I am using VS Studio 2022 17.4.4 and net9.0. Thank you for your help; I really like this library!

Steps to reproduce

https://github.com/r14958/TestVogen

Expected behaviour

No compiler warning is generated.

r14958 avatar Jun 12 '25 03:06 r14958

Hi, thanks for the feedback. Nullable underlying primitives aren't supported. They're not explicitly unsupported, but rather by side-effect of the purpose of value objects. In an effort to make myself clearer, what I'm trying to say is that value objects have value and Vogen is an elaborately necessary abstraction in C# to avoid nulls.

I might be able to offer some advice of an alternative way to use Vogen: what is the use case for your nullable primitive?

SteveDunn avatar Jun 19 '25 17:06 SteveDunn

Hi Steve, thanks for the response. I have a non-typical use case where I need my value objects to still be created and persisted in an "error state", which includes holding null values. I realize this is counter to typical DDD and "always valid" coding practices. I really like your library and thought I could use its "optional" validation to suite my needs, but I ran across this warning. Plus, I was confused by two things:

  1. I could not find any warning in the generated code itself.
  2. I found a statement in a similar (closed) issue: https://github.com/SteveDunn/Vogen/issues/776, where the submitter stated "It has no problems with other nullable types like decimal? or int?," which was contrary to my experience.

So I thought I may be doing something wrong. I would be happy to hear any workarounds, but I also understand perfectly that the Vogen library expects non-null values, so please feel free to dismiss this issue.

r14958 avatar Jun 19 '25 19:06 r14958

Hi again, pruning some issues. The only workaround I can suggest is to represent an error state as a predefined value, something like:

[ValueObject<double?]
public readonly partial record struct Test
{
    public static readonly Test Undefined = new(-1);
}

Let me know if this issue deserves some reconsideration. Thanks again!

SteveDunn avatar Nov 16 '25 18:11 SteveDunn