Kill Anemic Domain Models with Encapsulation: The Hard Part

Поделиться
HTML-код
  • Опубликовано: 15 сен 2024
  • Become a patron and gain access to source code and exclusive live streams: / taming-anemic-82548284
    Ready to level up your OOP and OOD skills? In this video, we dive into the world of encapsulation, a crucial concept in object-oriented programming and object-oriented design. Encapsulation helps us tackle issues that arise in anemic domain models, making our software more robust and efficient. Through a hands-on demo, we reveal how encapsulation can turn an anemic domain model into a rich, well-structured one.
    The key takeaway from this video is the vital role encapsulation plays in preserving class invariants and ensuring the smooth functioning of your application.
    The video walks you through a step-by-step demonstration of transforming an anemic domain model into a powerful rich domain model using encapsulation techniques. This demo is the perfect example of how encapsulation can solve the issues that cause bugs, making your system more consistent and in line with object-oriented design principles.
    Throughout the video, we touch upon the concept of design by contract and its role in developing maintainable software systems. We highlight the connection between encapsulation and the broader principles of object-oriented design, focusing on the importance of a solid class design.
    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...
    ▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬
    ⚡️ Have a look at our other Videos :
    👉 Using GitHub Copilot to Write Complex Code | Step-by-step Tutorial ► • Using GitHub Copilot t...
    👉 Coding with GitHub Copilot - Beginner to Master | VS Code Demo ► • A Comprehensive Guide ...
    👉 What is Covariance and Contravariance in C# ► • What is Covariance and...
    How to Initialize a Clean ASP.NET Core Project with Entity Framework Core and Identity ► • How to Initialize a Cl...
    👉 The Null Conundrum: A Guide to Optional Objects in C# ► • How to Avoid Null Refe...
    ▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬
    ⭐ 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 RUclips 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
    #oop #csharp #aspnetcore

Комментарии • 35

  • @MahmoudIsmail-rw7hg
    @MahmoudIsmail-rw7hg Год назад +2

    thanks Zoran, I like your explanation.

  • @milanmladenovic
    @milanmladenovic Год назад +3

    Hvala Zorane

  • @WolfieVenturi
    @WolfieVenturi Год назад +1

    Awesome

  • @samosimoncic5020
    @samosimoncic5020 Год назад +2

    amazing video. I do not understand how you tell the ef that AuthorsCollection has to be loaded in Book. If AuthorsCollection is internal (in domain project) and the repository is placed in an infrastructure project, it is impossible that Book DbSet reaches AuthorsCollection and includes them when GetBooks method from repository is called.

    • @zoran-horvat
      @zoran-horvat  Год назад +3

      Entity Framework is using reflection. It can even populate private members if needed, though you must specify non-public members with a string, rather than with a lambda.

    • @nickbarton3191
      @nickbarton3191 5 месяцев назад

      ​​@@zoran-horvatIt's been years since I've written a dB application and haven't used EF. So it uses reflection, that's awesome. I've watch many of your videos wondering how the data is materialised, now I know.
      But I dislike Blazer, once again MS have gone for interleaving HTML and code, so messy. This is no better than classic VB Web pages in this respect, written plenty of those. I much prefer markup like AngularJS. Maybe HTMX has the solution to this old problem.

    • @zoran-horvat
      @zoran-horvat  5 месяцев назад +1

      @@nickbarton3191 Blazor is just one of the implementations of WebAssembly, which is a separate standard defining a virtual machine and the corresponding byte code. It has little to do with Microsoft. Wasm can execute on any system, such as a browser or an embedded system, though it seems like only the browsers have accepted it so far.

    • @nickbarton3191
      @nickbarton3191 5 месяцев назад +1

      @@zoran-horvat Ah OK, I'm out of touch with web programming.

  • @VoroninPavel
    @VoroninPavel 10 месяцев назад +1

    Once again. I hate EF Core for poor DDD support. All these code tricks with Lazy and alike are needed because navigation properties cannot be set through constructor. And there's no way to force EF to call some private method after entity is materialized. EF team does an excellent job and delivers many features, but in most cases those features are adding convenience for anemic models. ='(

    • @7th_CAV_Trooper
      @7th_CAV_Trooper 4 месяца назад

      I agree. It's time for MSFT to rewrite EF with modern design principles in mind.

  • @superpcstation
    @superpcstation Год назад +2

    Hi Zoran, doesn't the bool TryMoveAuthorUp(int index) functions breaks the command/query separation principle? Or is it something you don't worry about?

    • @zoran-horvat
      @zoran-horvat  Год назад +13

      Command/query separation principle has a corollary: it is common to return the command execution status in situations when querying for the same information after the command would look awkward, inefficient, needlessly complex or be outright impossible.
      A common case is when a command creates a child object and adds it to private structures that it also returns the newly created object, or at least a reference to it, like an ID, for the caller's bookkeeping (e.g. to add it to a DbContext, so that the child object will be persisted).
      However, it would be a violation of the principle if the command would return a result that would otherwise qualify as a query. The danger comes from some caller using such command in order to fetch its result, not knowing, or tolerating, the side effects caused by its action, which might not be appropriate in that situation.

  • @alexeycherepanov9150
    @alexeycherepanov9150 4 месяца назад

    10:20 - why did you not sort list in AuthorsCollection's setter?

    • @zoran-horvat
      @zoran-horvat  4 месяца назад +1

      Because it is populated by EF.

  • @software_developer5
    @software_developer5 Год назад

    I am working on a project, where we are using anemic design pattern throughout the project. We have anemic domain classes which are used everywhere and we have services to perform business logic, we are also using CQRS which then call these services so buniess logic divided between services and CQRS. Can you make a video or suggest how to move from anemic design model to rich domain in such cases

    • @zoran-horvat
      @zoran-horvat  Год назад +4

      I have plans for a series of videos dedicated to the gradual process of improving some typical deficiencies in the design, though I still don't have a timeline when I will start recording them.
      I also have a workshop ready, which I am holding in companies, titled Gradually Improving Software Design. If your managers are interested in organizing a training for your team, they can contact me directly for details.

  • @alexeycherepanov9150
    @alexeycherepanov9150 4 месяца назад

    10:20 there is easy way to do same - you can sort authors in setter of AuthorCollection property.

    • @zoran-horvat
      @zoran-horvat  4 месяца назад

      That property is managed by EF.

    • @alexeycherepanov9150
      @alexeycherepanov9150 4 месяца назад

      ​@@zoran-horvat and that is problem with it?? ef still need to call setter...

    • @zoran-horvat
      @zoran-horvat  4 месяца назад

      @@alexeycherepanov9150 It is not the problem with that property. It is its purpose.
      How would you sort it anyway? It is an ICollection.

    • @alexeycherepanov9150
      @alexeycherepanov9150 4 месяца назад

      @@zoran-horvat something like this:
      private ICollection _authorsCollection;
      internal ICollection AuthorsCollection
      {
      get => _authorsCollection;
      set => _authorsCollection = value.OrderBy(x=>x.Ordinal).ToArray();
      }

    • @alexeycherepanov9150
      @alexeycherepanov9150 4 месяца назад

      @@zoran-horvat something like this:
      private ICollection _authorsCollection;
      internal ICollection AuthorsCollection
      {
      get => _authorsCollection;
      set => _authorsCollection = value.OrderBy(x=>x.Ordinal).ToArray();
      }

  • @TreeLuvBurdpu
    @TreeLuvBurdpu Год назад

    They could still sort by last name.

    • @zoran-horvat
      @zoran-horvat  Год назад +2

      Yes, but that wouldn't make it the list of authors of a book.