© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•3y ago•
1 reply
Naster

✅ Generic nullable type

I have an interface
ITypelyValue<TValue, TTypelyValue>
ITypelyValue<TValue, TTypelyValue>
that let me create validated value objects.

I need to convert theses types using a ValueConverter for EF Core.

public class TypelyValueConverter<TValue, TTypelyValue> : ValueConverter<TTypelyValue, TValue>
    where TTypelyValue : ITypelyValue<TValue, TTypelyValue>
{
    public TypelyValueConverter()
        : base(
            v => v.Value,
            v => TypelyValue.From<TValue, TTypelyValue>(v))
    {
    }
}
public class TypelyValueConverter<TValue, TTypelyValue> : ValueConverter<TTypelyValue, TValue>
    where TTypelyValue : ITypelyValue<TValue, TTypelyValue>
{
    public TypelyValueConverter()
        : base(
            v => v.Value,
            v => TypelyValue.From<TValue, TTypelyValue>(v))
    {
    }
}

This works fine for value types. e.g.
public struct MyValueObject : ITypelyValue<int, MyValueObject>{...}
public struct MyValueObject : ITypelyValue<int, MyValueObject>{...}
.
With an entity :
public class MyEntity
{
    public MyValueObject Prop1 {get; set;}  //ok
    public MyValueObject? Prop2 {get; set;}  //Error
}
public class MyEntity
{
    public MyValueObject Prop1 {get; set;}  //ok
    public MyValueObject? Prop2 {get; set;}  //Error
}

When I make my value object nullable, the property becomes a reference type and I'm confused of how I can handle saving a null value to the database with the type converter.

Other ref: https://github.com/adampaquette/Typely/blob/main/src/Typely.Core/ITypelyValue.cs
GitHub
Typely/ITypelyValue.cs at main · adampaquette/Typely
Typely lets you create types easily with a fluent API to embrace Domain-driven design and value objects. - Typely/ITypelyValue.cs at main · adampaquette/Typely
Typely/ITypelyValue.cs at main · adampaquette/Typely
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

Using pointers as generic a generic type
C#CC# / help
4y ago
❔ How to make a generic class property nullable?
C#CC# / help
4y ago
Non-Nullable type "required" double up.
C#CC# / help
2y ago
Generic type that extends bool
C#CC# / help
2y ago