question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Proposal: Introduce PrimitiveValueObject for single-property ValueObjects

See original GitHub issue

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.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:11 (9 by maintainers)

github_iconTop GitHub Comments

1reaction
vkhorikovcommented, Dec 22, 2019

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.
0reactions
MikelThiefcommented, Jan 5, 2020

Thanks!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Value Objects to the rescue!. Get rid of your primitive ...
Value objects are one of the building blocks introduced in the book ... Primitive Obsession is a code smell that is easy to...
Read more >
Value Objects: when to create one?
I would say that if the domain concept needs more than one primitive, then definitely introduce a separate Value Object for it.
Read more >
Domain Primitives: what they are and how you can use them ...
A value object precise enough in its definition that it, by its mere existence, manifests its validity is called a domain primitive.
Read more >
java - Primitive vs Class to represent simple domain object?
If you can live with simple values because it's not really critical to functionality, use a primitive. Share.
Read more >
Using Value Objects to Give Meaning - Blog - Martin Bean
Value objects for single properties. In my application, I've been unearthing values that can be elevated from simple, primitive values (like ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found