How to Use Value Objects to Solve Primitive Obsession

preview_player
Показать описание

Value Objects are types defined only by their values. Two value objects are considered equal if their values are identical. Value Objects can be used to solve primitive obsession, which occurs when we use primitive types to represent more complex concepts.

Join my weekly .NET newsletter:

Subscribe for more:

Chapters
0:00 What is Primitive Obsession
2:24 Introducing Value Objects
3:51 Supporting structural equality
6:24 Implementing a Value Object
7:43 How to enforce constraints
10:11 Using our Value Object
12:40 What we get with Value Objects
Рекомендации по теме
Комментарии
Автор

You're very talented and bound to be a great teacher. Loved the video and subbed instantly!

fieryscorpion
Автор

Please make video on aggregates and aggregate root

injypal
Автор

For this purpose, OOP style is over complicated. For example, in F# with structural equality and discriminated out of the box all domain with these value objects and validation would fit into a dozen lines of code. By using bind, no need to throw exception on every data field validation. I'd recommend checking out Scott Wlaschin's "Domain modelling made functional" book and his numerous talks on YouTube, including railway oriented programming

TimurBabyuk
Автор

Wouldn't a record type be a sufficient replacement for a ValueObject? You get type safety, immutability and equality out of the box and way shorter to type.
Am I missing something?

I see that it's already answered way down below

joga_bonito_aro
Автор

Great video on this topic. I agree the complexity is increased and you have to weight up what you gain. Looking forward to the input validation video (going through them all).

What I've done before for validation is a isValid method that lives within the member entity. If not valid it returned null and I checked for that.
After watching your other video I'd use a result instead :)

Thanks as always!

pureevil
Автор

Why not using a record instead of a class to achieve immutability?

brechtlaitem
Автор

What a pleasure to have such a brilliant developer and also teacher for .NET community!

eddypartey
Автор

something something Domain Modeling Made Functional: Tackle Software Complexity with Domain-Driven Design and F#

able
Автор

It would be great to add a link to the video you mentioned during your speech. For example "If you didn't watch my video about Result, ..." and video appears in right top corner.
It will increase the amount of views, I'm sure :)

implicat
Автор

Good thing pointing out the negative aspects in the end. 👍 Everything are tradeoffs and not all patterns fit everyone and sometimes not even most ones.

Benke
Автор

Excellent Milan! Please never stop! Your videos are very good

YuriWithowsky
Автор

Awesome video, l was actually waiting for this notification, what do you think about having validation being the responsibility of something like FluentValidation in a DDD project, on the handlers?

kshooter
Автор

Is anyone using this in production code?

XMaverick
Автор

Great video. You are ver very good at explaining complex subjects in an easy to understand way. I also like how you explain the trade-offs for the different approaches.

EldonElledge
Автор

So much code for one string hmmm don't like it boyy 🤣🤣

darkogele
Автор

Thanks Milan for the video and I really got excited when I saw World Of Warcraft behind you 🤩

TheMostafa
Автор

Just a little comment about the Value Objects and my own experience: Definitively ValueObjects it's the better approach when you developing an application using the DDD patterns, but if you are using Entity Framework you must be careful about how that Value Objects will be translated to a T-SQL or other SQL language (postgres, mysql, etc). By default, Entity Framework don't have a idea how can do that, you can apply some tricks (casting and creating implicit operators on the Value Object) but this is ok in scenarios were you have the full control how create the Linq sentences; if you use a control suite like a DevExpress or Telerik, on backstage the controls don't know how to handle that kind of objects and you can find some error on controls like DataGrid were cannot translate the linq sentences.

batressc
Автор

Awesome but I think FirstName is not a good choice to explain value object with I think Address would be great example.

mahmoudalballah
Автор

Hey Milan, what do you think of ValueOf? and, How would you use this with FluentValidation? meaning, I'd like to have value objects for common properties among my entities and I'd have specific ... ?

ayalamac
Автор

Milan you are a great guy. But let me tell you. Here we are saying that those initial 3 lines of code are not safe (?), not robust (?), not elegant (?) thus I start to write more than 100 LOC quite complex to read and write to justify the greateness of my code. Do you think companies are willing to pay that? How much time do I need to spend? What about an alternative solution using maybe no more than 10 LOC instead? I think software engineering is partially theoretical (good for courses, teaching and video) and partially (the most important part) is to solve real problem in production with still good enough code. Sorry just my 2 cents.

pigrebanto