Skip to content

Pre-defined values support #18

Description

@Aragas

In cases when the ValueObject has functionality similar to an enum, but a VO would fit better.

I suggest a new Attribute marker ValueObjectInstanceAttribute to mark the values as pre-defined and two new Augments - AllValuesAugment and LimitValuesAugment

  • AllValuesAugment exposes an IEnumerable that returns all pre-defined values
  • LimitValuesAugment limits the From function to only handle the values from pre-definded, throwing if a non pre-defined value is provided.
[ValueObject<int>]
public readonly partial struct SampleIntValueObject : IAugmentWith<DefaultValueAugment, AllValuesAugment>
{
    [ValueObjectInstance]
    public static SampleIntValueObject Default { get; } = From(0);
    [ValueObjectInstance]
    public static SampleIntValueObject Instance1 { get; } = From(1);
    [ValueObjectInstance]
    public static SampleIntValueObject Instance2 { get; } = From(2);
    
    public static SampleIntValueObject DefaultValue => Default;

    // Auto Generated
    public static IEnumerable<SampleIntValueObject> All
    {
        get
        {
            yield return Default;
            yield return Instance1;
            yield return Instance2;
        }
    }
}

[ValueObject<int>]
public readonly partial struct SampleIntValueObject : IAugmentWith<LimitValuesAugment>
{
    [ValueObjectInstance]
    public static SampleIntValueObject Default { get; } = From(0);
    [ValueObjectInstance]
    public static SampleIntValueObject Instance1 { get; } = From(1);
    [ValueObjectInstance]
    public static SampleIntValueObject Instance2 { get; } = From(2);

    // Will throw if values other than 0,1,2 will be suplied to From
}

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