Use Null Object Pattern in Your Rich Domain Model

แชร์
ฝัง
  • เผยแพร่เมื่อ 17 ต.ค. 2024
  • The source code from this demo is available on Patreon: / use-null-object-83554737
    Ever wondered about the impact of the Null Object design pattern on domain modeling? This video offers a deep dive into this fascinating topic, demonstrating how identifying objects that behave like 'zero' can create a more intuitive and friendly model, even in a complex business domain.
    We bring this concept to life through a real-world example, demonstrating the significant influence of 'null objects' in a deep domain model when it comes to implementing essential features. Watch this video to experience this simplifying effect firsthand!
    And not just that, we also reveal situations where you might want to avoid using the Null Object pattern by adding a touch of complexity to your classes, but gaining more from their simple and intuitive public interface. Discover why maintaining a simple public interface could be advantageous over polymorphic Null Object implementation.
    By the end of this journey, you'll be equipped with the know-how to apply these concepts creatively in your domain modeling, ultimately leading to more structured code.
    Thank you so much for watching! Please like, comment & share this video as it helps me a ton!! Don't forget to subscribe to my channel for more amazing videos and make sure to hit the bell icon to never miss any updates.🔥❤️
    ✅🔔 Become a patron ► / zoranhorvat
    ✅🔔 Subscribe ► / @zoran-horvat
    ⭐ Learn more from video courses:
    Beginning Object-oriented Programming with C# ► codinghelmet.c...
    ⭐ Collections and Generics in C# ► codinghelmet.c...
    ⭐ Making Your C# Code More Object-oriented ► codinghelmet.c...
    ▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬
    ⭐ CONNECT WITH ME 📱👨
    🌐Become a patron ► / zoranhorvat
    🌐Buy me a Coffee ► ko-fi.com/zora...
    🗳 Pluralsight Courses ► codinghelmet.c...
    📸 Udemy Courses ► codinghelmet.c...
    📸 Join me on Twitter ► / zoranh75
    🌐 Read my Articles ► codinghelmet.c...
    📸 Join me on LinkedIn ► / zoran-horvat
    ▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬
    👨 About Me 👨
    Hi, I’m Zoran, I have more than 20 years of experience as a software developer, architect, team lead, and more. I have been programming in C# since its inception in the early 2000s. Since 2017 I have started publishing professional video courses at Pluralsight and Udemy and by this point, there are over 100 hours of the highest-quality videos you can watch on those platforms. On my TH-cam channel, you can find shorter video forms focused on clarifying practical issues in coding, design, and architecture of .NET applications.❤️
    ▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬
    ⚡️RIGHT NOTICE:
    The Copyright Laws of the United States recognize a “fair use” of copyrighted content. Section 107 of the U.S. Copyright Act states: “Notwithstanding the provisions of sections 106 and 106A, the fair use of a copyrighted work, including such use by reproduction in copies or phono records or by any other means specified by that section, for purposes such as criticism, comment, news reporting, teaching (including multiple copies for classroom use), scholarship, or research, is not an infringement of copyright." This video and our youtube channel, in general, may contain certain copyrighted works that were not specifically authorised to be used by the copyright holder(s), but which we believe in good faith are protected by federal law and the Fair use doctrine for one or more of the reasons noted above.
    ⭐For copyright or any inquiries, please contact us at zh@codinghelmet.com
    #dotnet #csharp #designpatterns

ความคิดเห็น • 30

  • @codingbloke
    @codingbloke ปีที่แล้ว +13

    Excellent presentation. Well balanced. None of the "Your doing X wrong!!" hysterics here.

    • @zoran-horvat
      @zoran-horvat  ปีที่แล้ว +14

      The greatest secret of software design is that every decision is a tradeof.

  • @tarsala1995
    @tarsala1995 ปีที่แล้ว +10

    I rarely watch videos twice. This one opened my eyes. I think your videos are reminding us how we should look at the code.

    • @zoran-horvat
      @zoran-horvat  ปีที่แล้ว +2

      Thank you for your comment. I am glad to hear the video was helpful.

  • @Bankoru
    @Bankoru ปีที่แล้ว +6

    Essentially creating an algebra by adding an Identity element under the Add binary operator.

  • @cnaegle
    @cnaegle ปีที่แล้ว +1

    Great presentation from one of my favorite instructors! Making my development team watch it.

  • @driden1987
    @driden1987 ปีที่แล้ว

    Haven’t programmed in C# for a couple of years now, still loving the examples. Good job !

  • @thygrrr
    @thygrrr 4 หลายเดือนก่อน +1

    "There are obviously two kinds of money - Zero and Non-Zero"
    Me: "Wait... You guys have non-zero money?"

  • @robertmrobo8954
    @robertmrobo8954 ปีที่แล้ว +5

    Thank you for not using the default 'Order, product and Order Items' examples. 😊

  • @majixx
    @majixx ปีที่แล้ว +2

    Thank you again Zoran. Brilliant video!

  • @t304pk
    @t304pk ปีที่แล้ว +4

    Thank you for the impressive video, as always.

  • @5cover
    @5cover ปีที่แล้ว +4

    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.

  • @tplummer217
    @tplummer217 ปีที่แล้ว +1

    Another excellent video sir!

  • @7th_CAV_Trooper
    @7th_CAV_Trooper 7 หลายเดือนก่อน +1

    omg, a list of my favorite books.

  • @Skaral2
    @Skaral2 2 หลายเดือนก่อน

    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.

    • @zoran-horvat
      @zoran-horvat  2 หลายเดือนก่อน +1

      @@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.

  • @malzsmith
    @malzsmith ปีที่แล้ว +1

    I'm loving your videos! Do you have plans to make content about async programming and synchronization?

    • @zoran-horvat
      @zoran-horvat  ปีที่แล้ว +1

      I have one video in the queue that talks about asynchronous programming, which I might record in the near future.

    • @malzsmith
      @malzsmith ปีที่แล้ว +1

      @@zoran-horvat glad to hear it, thank you!

  • @10199able
    @10199able ปีที่แล้ว +8

    "poor man's polymorphism"

    • @zoran-horvat
      @zoran-horvat  ปีที่แล้ว +2

      Oh yes, you can find it everywhere.

    • @josebarria3233
      @josebarria3233 ปีที่แล้ว

      That makes you wonder which one is rich man polymorphism

    • @zoran-horvat
      @zoran-horvat  ปีที่แล้ว +5

      @@josebarria3233 Just polymorphism.

  • @thygrrr
    @thygrrr 4 หลายเดือนก่อน +1

    8:30 "Money has just become abstract"
    ... not just...

  • @m4inline
    @m4inline ปีที่แล้ว +3

    Ming the Merciless retired as a coder?

  • @PedroPabloCalvoMorcillo
    @PedroPabloCalvoMorcillo ปีที่แล้ว +1

    Great ideas and insights. Not a big fan of throwing exceptions in this case, thought.

    • @zoran-horvat
      @zoran-horvat  ปีที่แล้ว +4

      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.

  • @elraito
    @elraito หลายเดือนก่อน +1

    "...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.