Proposal: Introduce PrimitiveValueObject for single-property ValueObjects
Hello,
recently i switched from using ValueOf to this library's ValueObject class. I have a lot of single-property VOs. ValueOf made it easy for me to create them as everything i had to do was to write a single line like:
public class EmailAddress : ValueOf<string, EmailAddress> { }
With ValueObject I have to manually write something like
public class EmailAddress : ValueObject
{
public string Value { get; }
private EmailAddress(string value)
{
Value = value;
}
protected override IEnumerable<object> GetEqualityComponents()
{
yield return Value;
}
}
Every time i create single-property VO i have to write several lines of boilerplate code.
I would like to make it simpler by introducing PrimitiveValueObject or SinglePropertyValueObject:
public class PrimitiveValueObject<T> : ValueObject
{
public T Value { get; }
public PrimitiveValueObject(T value) { Value = value; }
protected override IEnumerable<object> GetEqualityComponents() { yield return Value; }
}
With this class i can create EmailAddress like:
public class EmailAddress : PrimitiveValueObject<string>
{
public EmailAddress (string value) : base(value)
{
}
}
Do you think that it would be beneficial to be included in the library? I can create a PR if you like this.
I tend to like the idea, I noticed the boilerplate as well.
It will be interesting to hear feedback about this suggestion.
- Where would validations happen? In the derived class constructor?
- Should the PrimitiveValueObject be an abstract class?
Where would validations happen? In the derived class constructor?
I didn't put any validation as original class didn't have anything related to that either (or the intent was not clear enough to me. I don't think that ctors should perform validation). I vote for static factory method. ValueOf is unfortunately a bit clunky in getting around with validation. See this tiny project code for details. You will see that due to inheritance something is not right and had to be workarounded.
I like the idea too, please go ahead with a PR. Couple notes:
PrimitiveValueObjectsounds better thanSinglePropertyValueObject(more concise)PrimitiveValueObjectshould beabstractwith aprotectedc-tor.
Okay, please give me some time to add it - holidays is a busy period :)
@vkhorikov I have problems building the solution locally - VS indicated that ValueObject is declared twice (and both declaration places indicate the same location CSharpFunctionalExtensions.ValueObject). Am I missing something?
@MikelThief I just pulled the repo and have no problem building the solution.
@MikelThief could you post a screen shot of the error? There are 2 implementations of ValueObject, but they shouldn't conflict with each other -- one is generic and the other is not.
@vkhorikov @arthg It appeared that when I initially created a new file with PrimitiveValueObject inheriting ValueObject it was by default placed under CSharpFunctionalExtensions.ValueObject namespace. This had to confuse the compiler by suggesting that there is ambiguity in what ValueObject really is. I failed to see that yesterday late at night.
PR has been created :)
Thanks :) I'm out of town atm, will merge it on Monday
Merged and renamed it to SimpleValueObject
Thanks!