Thanks to visit codestin.com
Credit goes to GitHub.com

Skip to content

FromNullable #875

@MatteoZampariniDev

Description

@MatteoZampariniDev

Describe the feature

I'm wondering if it makes sense to have a From method that accepts nullable values and performs checks like this:

[ValueObject<string>]
public readonly partial struct SampleId
{
    public static SampleId? FromNullable(in string? value)
            => value != null ? SampleId.From(value) : null; // <======= nice to have
}

public class SampleEntity(SampleId id, SampleId? parentEntityId)
{
    public SampleId Id { get; set; } = id;
    public SampleId? ParentEntityId { get; set; } = parentEntityId;
}

public class SampleEntityBuilder
{
    public static SampleEntity Build(string id, string? parentEntityId)
    {
        return new SampleEntity(
             SampleId.From(id),
             SampleId.FromNullable(parentEntityId)); // <======= use case here
    }
}

Am I missing an already existing feature like this?

The use case would be having better in-lined code without the need of explicit checks.

Thanks in advance :)

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions