From the course: Expert Domain-Driven Design (DDD) Implementation in .NET

Unlock the full course today

Join today to access over 24,900 courses taught by industry experts.

Implementing a value object for a pet's weight

Implementing a value object for a pet's weight - .NET Tutorial

From the course: Expert Domain-Driven Design (DDD) Implementation in .NET

Implementing a value object for a pet's weight

- [Instructor] Up to now, we're using basic data types in the Pet class. So what's the problem with the primitive obsession anti-pattern? Let me show you what it is. Here in the UnitTest1, I'm creating a couple of Pet objects, and for example, I can set the Weight to a negative value such as -20.5, and this will be accepted. This will run without any issues and nothing prevents me from using this kind of value, which makes no sense at all for a pet's weight. This is precisely the problem with primitive data types. They don't know what a weight value actually represents. For this property, I could assign a negative value or even let's do this, decimal.MaxValue, and this is going to work. It will compile and run. But obviously these are totally incoherent values. So what can we do? Well, first of all, I want to create a new value object for Weight. A value object, as its name implies, represents a value. And unlike entities, value objects don't rely on any identity. They only hold a…

Contents