CSharpFunctionalExtensions icon indicating copy to clipboard operation
CSharpFunctionalExtensions copied to clipboard

Proposal: Introduce PrimitiveValueObject for single-property ValueObjects

Open MikelThief opened this issue 6 years ago • 11 comments

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.

MikelThief avatar Dec 20 '19 11:12 MikelThief

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?

arthg avatar Dec 20 '19 14:12 arthg

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.

MikelThief avatar Dec 20 '19 14:12 MikelThief

I like the idea too, please go ahead with a PR. Couple notes:

  1. PrimitiveValueObject sounds better than SinglePropertyValueObject (more concise)
  2. PrimitiveValueObject should be abstract with a protected c-tor.

vkhorikov avatar Dec 22 '19 14:12 vkhorikov

Okay, please give me some time to add it - holidays is a busy period :)

MikelThief avatar Dec 25 '19 17:12 MikelThief

@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 avatar Dec 27 '19 10:12 MikelThief

@MikelThief I just pulled the repo and have no problem building the solution.

arthg avatar Dec 27 '19 14:12 arthg

@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 avatar Dec 28 '19 00:12 vkhorikov

@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 :)

MikelThief avatar Dec 28 '19 20:12 MikelThief

Thanks :) I'm out of town atm, will merge it on Monday

vkhorikov avatar Dec 31 '19 11:12 vkhorikov

Merged and renamed it to SimpleValueObject

vkhorikov avatar Jan 05 '20 16:01 vkhorikov

Thanks!

MikelThief avatar Jan 05 '20 18:01 MikelThief