IMO there's a flaw in your current implementation, as it is possible create a Money object with Currency.Empty and a non-zero amount. So it is possible to handle money without a currency. A possible solution is to throw an exception in the constructor if currency equals Currency.Empty. Also I would rename Currency.Empty to Currency.None, as there's no such thing as an "empty" currency.
How do you make the null object pattern work with Entity Framework? I had an entity with a navigation property to another entity ( Product 1:1 ProductPicture in separate tables), but it was possible for it to be NULL. I would like to replace the NULL with the null object, but I can't save it into the database since that'd create a duplicate.
@@Skaral2 EF Core does not support null objects out of the box. Custom mappings do not work because EF Care does not invoke it when the database field is null. Therefore, you cannot map a database null into a non-mull object in the application. There are at least two workarounds. One is to persist a private nullable field. The public getter would coalesce null into a null object. That is an easy solution, which avoids the complexities associated with persisting a hierarchy. The private field could be of the concrete type that acts as the non-mull case. A more engaged option is to have a null object persisted with a well-known ID value which is then also used in the application's null object instance. In that case, you can read and write objects as usual. There are a few details to work out in either case, but both options generally work well in practice.
There is a more complex design where Money separates "strict" addition from that with mixed currencies. However, subtraction can never be with mixed currencies, except for subtracting a zero... For a 100% safe and sound solution, we would need to return an Either monad from all strict methods. But with no syntactic support for errors in C#, the implementation and its use would become significantly more complex and less readable.
"...lets add a touch of human. That is when we have no money" Look. It is illegal to make both excellent jokes and excellent video at the same time. Stop it.
I rarely watch videos twice. This one opened my eyes. I think your videos are reminding us how we should look at the code.
Thank you for your comment. I am glad to hear the video was helpful.
Excellent presentation. Well balanced. None of the "Your doing X wrong!!" hysterics here.
The greatest secret of software design is that every decision is a tradeof.
Essentially creating an algebra by adding an Identity element under the Add binary operator.
Great presentation from one of my favorite instructors! Making my development team watch it.
Haven’t programmed in C# for a couple of years now, still loving the examples. Good job !
Thank you for not using the default 'Order, product and Order Items' examples. 😊
Of course :)
Thank you again Zoran. Brilliant video!
omg, a list of my favorite books.
IMO there's a flaw in your current implementation, as it is possible create a Money object with Currency.Empty and a non-zero amount.
So it is possible to handle money without a currency.
A possible solution is to throw an exception in the constructor if currency equals Currency.Empty.
Also I would rename Currency.Empty to Currency.None, as there's no such thing as an "empty" currency.
Good observations.
Thank you for the impressive video, as always.
Another excellent video sir!
"There are obviously two kinds of money - Zero and Non-Zero"
Me: "Wait... You guys have non-zero money?"
"poor man's polymorphism"
Oh yes, you can find it everywhere.
That makes you wonder which one is rich man polymorphism
@@josebarria3233 Just polymorphism.
8:30 "Money has just become abstract"
... not just...
Ming the Merciless retired as a coder?
How do you make the null object pattern work with Entity Framework? I had an entity with a navigation property to another entity ( Product 1:1 ProductPicture in separate tables), but it was possible for it to be NULL. I would like to replace the NULL with the null object, but I can't save it into the database since that'd create a duplicate.
@@Skaral2 EF Core does not support null objects out of the box. Custom mappings do not work because EF Care does not invoke it when the database field is null. Therefore, you cannot map a database null into a non-mull object in the application.
There are at least two workarounds. One is to persist a private nullable field. The public getter would coalesce null into a null object. That is an easy solution, which avoids the complexities associated with persisting a hierarchy. The private field could be of the concrete type that acts as the non-mull case.
A more engaged option is to have a null object persisted with a well-known ID value which is then also used in the application's null object instance. In that case, you can read and write objects as usual.
There are a few details to work out in either case, but both options generally work well in practice.
Great ideas and insights. Not a big fan of throwing exceptions in this case, thought.
There is a more complex design where Money separates "strict" addition from that with mixed currencies. However, subtraction can never be with mixed currencies, except for subtracting a zero...
For a 100% safe and sound solution, we would need to return an Either monad from all strict methods. But with no syntactic support for errors in C#, the implementation and its use would become significantly more complex and less readable.
I'm loving your videos! Do you have plans to make content about async programming and synchronization?
I have one video in the queue that talks about asynchronous programming, which I might record in the near future.
@@zoran-horvat glad to hear it, thank you!
"...lets add a touch of human. That is when we have no money" Look. It is illegal to make both excellent jokes and excellent video at the same time. Stop it.
Lmao you invented a monoid
@@ŊŊŊ-d1q You missed the point.