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 - .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
-
-
-
Creating the initial project for the Management domain model1m 36s
-
Implementing an entity6m 27s
-
(Locked)
Refactoring the logic into an entity base class2m 7s
-
(Locked)
Encapsulating and protecting entity state3m 39s
-
(Locked)
Avoiding the primitive obsession anti-pattern1m 52s
-
(Locked)
Implementing a value object for a pet's weight7m
-
(Locked)
Implementing a second entity for pet breeds4m 6s
-
(Locked)
Invoking a domain service in a value object8m 8s
-
(Locked)
Implementing business rules in the Pet entity7m 43s
-
(Locked)
Implementing implicit operators in value objects2m 9s
-
-
-
-
-
-
-