Skip to content

Support Maybe patterns for navigations #22504

Open

Description

@ajcvickers

Activity

  1. added this to the Backlog milestone on Sep 14, 2020
  2. alexmurari commented on Dec 19, 2020

    @alexmurari
    Contributor

    @ajcvickers

    Just to give this issue a little bit of context:

    The request here is to make navigation properties more flexible:

    public class Student
    {
        public Course FavoriteCourse { get; set; } // This could be null.
    }

    In functional programming, we are taught to be more explicit on whether or not a reference type object is null, so we wrap them in a Maybe<T> monad, that works more-or-less like Nullable<T>.

    public class Student
    {
        private Course _favoriteCourse; // Backing field remains of the same type.
        public Maybe<Course> FavoriteCourse => _favoriteCourse; // Maybe<T> provides implicit conversion.
    }

    Use it like this:

    Maybe<Course> favCourseOrNothing = student.FavoriteCourse;
    
    if (favCourseOrNothing.HasNoValue)
        return Error("Bad student doesn't have a favorite course");
    
    Course favCourse = favCourseOrNothing.Value; // Throws exception if null, preventing us from walking around with null objects.

    EF Core doesn't know how to convert Maybe<T> to T, so it gives an exception when building the model.
    But, since EF Core works primarily on top of backing fields (and we actually map to the backing field), something like that is totally possible.

    • References

    https://github.com/vkhorikov/CSharpFunctionalExtensions#make-nulls-explicit-with-the-maybe-type
    https://enterprisecraftsmanship.com/posts/functional-c-non-nullable-reference-types/

  3. lucasteles commented on Jul 27, 2023

    @lucasteles

    I think that having a way to define a NullValueConverter for navigations would be good enough

  4. added theissue type on Jun 15, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions