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
}
In cases when the ValueObject has functionality similar to an enum, but a VO would fit better.
I suggest a new Attribute marker
ValueObjectInstanceAttributeto mark the values as pre-defined and two new Augments -AllValuesAugmentandLimitValuesAugmentAllValuesAugmentexposes an IEnumerable that returns all pre-defined valuesLimitValuesAugmentlimits theFromfunction to only handle the values from pre-definded, throwing if a non pre-defined value is provided.