DEV Community

Discussion on: The hidden value of the Value Object

goaty92 profile image

This is great and all but I would suggest that you make the Color class a value type. Specifically I would write it as readonly struct Color : IEquatable<Color>), this has a couple of advantages:

  • It's read-only (immutable) by default
  • Allocation-free when passing between methods (like with public Color Repaint(string hue, bool metallic))
  • Makes the intent more explicit (you want Color to represent some values) if no virtualization is required.
rafalpienkowski profile image
Rafal Pienkowski Author • Edited

Thanks for your comment. You are completely right. My old habits (and laziness) won.
I had problems with storing Value Objects with Entity Framework. I’ve checked that even EF Core 2.0 doesn't support storage of struct objects.
In non-db scenarios read-only struct makes the job right. We need to keep in mind that C# 7.2 is required to use it. I added the struct version to the links too.
Point for you for your mindfulness.