Value objects and How to Appreciate Them

Value objects and How to Appreciate Them

In this article, I will dive into why I find value objects useful and discuss various trade-offs in designing value objects. I will be doing examples in C#, but the examples should apply to many of the other major languages out there.

A value object is a design pattern in which an object is made to represent something simple, like currencies or dates. A value object should be equal to another value object if both objects have the same value despite being two different objects. In this article, I will dive into why I find value objects useful and discuss various trade-offs in designing value objects. I will be doing examples in C#, but the examples should apply to many of the other major languages out there.

Shopping for value — a shopping cart example:

Imagine that you’re in charge of developing or maintaining the code base of a simple webshop. Every item in the store has a unique ID, name, and price associated with it. In this case, an item may look like this in code:

public class Item {
  public Guid Id {get; set; }
  public string Name { get; set; }
  public decimal Price { get; set; }
}

Suddenly a new requirement emerges: shop items may have their prices quoted in multiple currencies.

Considering the time to market, and the fact that you can feel your boss breathing down your neck, you may be tempted to introduce a dictionary. This way you can look up the price for each currency. Without much hesitation and under pressure to deliver, you opt to go for a simple dictionary from string to decimal:

public class Item {
  public Guid Id {get; set; }
  public string Name { get; set; }
  public IDictionary<string, decimal> Prices {get; set; }
}

programming c-sharp-programming dotnet software-development coding c++

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Offshore Software Development - Best Practices

To make the most out of the benefits of offshore software development, you should understand the crucial factors that affect offshore development.

5 Best C# (C-Sharp) Programming Courses for Beginners in 2020

In this article, I am going to share some of the best courses to learn C# or C-Sharp in 2020. These courses are suitable for both beginners and experienced programmers who want to fill gaps in their C# knowledge.

5 Core Criteria for Selecting Software Development Company - TopDevelopers.co

Check out these five criteria for the selection of your software vendor, and you will never regret having the wrong quality product made for you.

Top C# Development Companies & Developers Reviews

Get in touch with the most sought after C# development service providers in the web and application development industry through TopDevelopers. Among the huge number of C# developers, we have sorted the best C# service providers by their satisfied...

Software Developer vs Software Engineer — Differences: Bogus or Real?

In this article, see if there are any differences between software developers and software engineers. What you’re about to read mostly revolves around my personal thoughts, deductions, and offbeat imagination. If you have different sentiments, add them in the comment section, and let’s dispute! So, today’s topic…