The MVVM Pattern in .NET MAUI

Book description

Gain an in-depth understanding of MVVM and .NET MAUI and learn how to effectively apply the MVVM design pattern with the help of this practical guide

Key Features

  • Get to grips with the principles and benefits of the Model-View-ViewModel design pattern
  • Gain insights into .NET MAUI's MVVM-enabling components and effectively apply them with hands-on examples
  • Learn data binding, navigation, and testable code techniques to create dynamic, accessible, and localized apps
  • Purchase of the print or Kindle book includes a free PDF eBook

Book Description

In today's fast-paced world of modern software development, teams need to be efficient, productive, and capable of rapidly adapting to changes to deliver high-quality products, making it crucial for developers to write maintainable and easy-to-test code.

The MVVM Pattern in .NET MAUI helps you to thoroughly explore the Model-View-View Model (MVVM) design pattern. The chapters show you how this pattern helps in structuring code to embrace the separation of concerns, allowing for loosely coupled user interface and application logic, which ultimately empowers you to write more robust, maintainable, and testable code. The book also highlights .NET MAUI's capabilities and features, and enables you to delve into the essential components within the framework that facilitate the application of the MVVM pattern. With the help of a sample application, this definitive guide takes a hands-on approach to walk you through both the essential and advanced usages of the MVVM pattern to ensure that you successfully apply the practical aspects of the pattern to your .NET MAUI projects.

By the end of this book, you’ll have gained a comprehensive understanding of the MVVM design pattern and its relevance in the context of .NET MAUI, as well as developed the skills needed to successfully apply it in practice.

What you will learn

  • Gain a thorough understanding of the MVVM design pattern
  • Get to grips with the components that enable MVVM in .NET MAUI
  • Apply the MVVM pattern in practice within .NET MAUI
  • Become proficient in data binding in .NET MAUI
  • Discover how to navigate from within a view model
  • Find out how to effectively write testable code and unit tests

Who this book is for

This book is for developers with experience in C# and basic knowledge of .NET MAUI or Xamarin.Forms who are looking to create cross-platform applications with .NET MAUI and leverage the MVVM pattern. Through practical examples and clear explanations, this book helps both newcomers and experienced developers master the application of MVVM principles within .NET MAUI projects.

Table of contents

  1. The MVVM Pattern in .NET MAUI
  2. Foreword
  3. Contributors
  4. About the author
  5. About the reviewer
  6. Preface
    1. Who this book is for
    2. What this book covers
    3. To get the most out of this book
    4. Download the example code files
    5. Conventions used
    6. Get in touch
    7. Share Your Thoughts
    8. Download a free PDF copy of this book
  7. Part 1: Key Concepts and Components
  8. Chapter 1: What Is the MVVM Design Pattern?
    1. Technical requirements
    2. Looking at MVVM’s core components
      1. Model
      2. View
      3. ViewModel
      4. Commands
      5. Data binding
    3. Separation of concerns matters
      1. Maintainability
      2. Testability
    4. MVVM in action
      1. The code-behind approach
      2. Using MVVM
    5. Common misconceptions about MVVM
      1. There should be no code in the code-behind
      2. The Model should exclusively be a DTO, a domain entity, or a POCO
      3. The View and the ViewModel should not know each other
      4. MVVM is too complex and used only for large applications
    6. Summary
  9. Chapter 2: What is .NET MAUI?
    1. Technical requirements
    2. .NET MAUI in a nutshell
      1. Reminiscent of Xamarin.Forms
      2. Cross-platform UI and more
    3. How does it work?
      1. Native apps with .NET
      2. .NET MAUI, another abstraction layer
    4. Creating your first .NET MAUI app
      1. Setting up your machine
      2. Hello “Recipes!” app
    5. Summary
    6. Further reading
  10. Chapter 3: Data Binding Building Blocks in .NET MAUI
    1. Technical requirements
    2. Key components for data binding
      1. Elements of data binding
      2. BindableObject
      3. BindableProperty
      4. BindingContext
      5. Data binding in practice
    3. Binding modes and the INotifyPropertyChanged interface
      1. Binding modes in .NET MAUI
      2. INofityPropertyChanged
      3. Different binding modes in action
    4. Handling interactions with the ICommand interface
      1. The ICommand interface
      2. Putting it into action
    5. Summary
    6. Further reading
  11. Chapter 4: Data Binding in .NET MAUI
    1. Technical requirements
    2. ValueConverters and StringFormat
      1. Creating and using ValueConverters
    3. Fallbacks
      1. TargetNullValue
      2. FallbackValue
    4. Element and relative binding
      1. Element binding
      2. Relative binding
    5. Multi-bindings
      1. Multi-binding StringFormat
      2. IMultiValueConverter
    6. Compiled bindings
    7. Summary
    8. Further reading
  12. Chapter 5: Community Toolkits
    1. Technical requirements
    2. The MVVM Toolkit
      1. ObservableObject
      2. RelayCommand
      3. Source Generators
    3. The .NET MAUI Community Toolkit
      1. Converters
      2. Behaviors
    4. Other popular frameworks
    5. Contributing to the community
    6. Summary
    7. Further reading
  13. Chapter 6: Working with Collections
    1. Technical requirements
    2. Using BindableLayout
      1. What to show and how to show it
      2. Dynamically choosing a DataTemplate at runtime
    3. The ICollectionChanged interface
      1. The ICollectionChanged interface and binding modes
      2. Using the ObservableCollection
    4. Working with CollectionView
      1. Displaying grouped data
      2. Selecting items
      3. Incrementally loading data
      4. Other common interactions
    5. Summary
    6. Further reading
  14. Part 2: Building a .NET MAUI App Using MVVM
  15. Chapter 7: Dependency Injection, Services, and Messaging
    1. Technical requirements
    2. Inversion of Control through Dependency Injection
      1. Dependency Injection
    3. Registering, resolving, and injecting services
      1. Registering services
      2. Resolving and injecting services
      3. Applying Dependency Injection
    4. Messaging
      1. WeakReferenceMessenger
    5. Summary
    6. Further reading
  16. Chapter 8: Navigation in MVVM
    1. Technical requirements
    2. About .NET MAUI Shell
      1. Setting up Shell
      2. Routing
      3. Supporting Dependency Injection
    3. Setting up navigation in a .NET MAUI Shell app
      1. Creating an INavigationService interface
      2. Creating and using a NavigationService
      3. Passing parameters
      4. Hooking into navigation
    4. Setting up navigation without .NET MAUI Shell
    5. Passing results back
    6. Summary
    7. Further reading
  17. Chapter 9: Handling User Input and Validation
    1. Technical requirements
    2. Implementing input validation on ViewModels
      1. Using the ObservableValidator
    3. Visualizing validation errors with triggers
    4. Prompts and alerts
    5. Confirming or canceling navigation
    6. Summary
    7. Further reading
  18. Chapter 10: Working with Remote Data
    1. Technical requirements
    2. Revisiting the model architecture
      1. Updates to the codebase
      2. Always return a result
      3. Adding repository interfaces
    3. API communication with Refit
      1. Getting started with Refit
      2. Creating API interfaces
      3. Integrating Refit
    4. API communication from ViewModels
      1. Showing a loading indicator
      2. Handling failures
    5. Summary
    6. Further reading
  19. Part 3: Mastering MVVM Development
  20. Chapter 11: Creating MVVM-Friendly Controls
    1. Technical requirements
    2. Building a custom control with bindable properties
      1. Implementing FavoriteControl
      2. Animating state changes
    3. Enabling interactions on custom controls
      1. User actions and reflecting state
      2. Adding command-based interactions
    4. Working with control templates
      1. Accessing elements from a template
    5. Summary
    6. Further reading
  21. Chapter 12: Localization with MVVM
    1. Technical requirements
    2. Working with cultures, resource files, and localization
      1. Getting the user’s culture
      2. Setting a different culture
      3. Displaying formatted data
      4. What are Resources Files?
      5. Getting localized resources on the screen
    3. Looking at a localization solution for MVVM
      1. The Localization project
      2. The Localization.Maui project
      3. Using the LocalizedResourcesProvider and LocalizationManager
    4. Using a custom Translate markup extension
    5. Fetching localized data from APIs
    6. Summary
    7. Further reading
  22. Chapter 13: Unit Testing
    1. Technical requirements
    2. The importance of unit testing
    3. Setting up a unit test project
      1. Creating a unit test project
      2. Creating unit tests
      3. Looking at data-driven tests
    4. Generating data with Bogus
    5. Mocking dependencies with Moq
      1. Applying mocking in our ViewModel tests
      2. Thin UI, deep tests
    6. Testing MAUI-specific code
    7. Summary
    8. Further reading
  23. Chapter 14: Troubleshooting and Debugging Tips
    1. Technical requirements
    2. Common data binding issues
      1. Checking the Output and XAML Binding Failures window
      2. Debugging with a DoNothingConverter
      3. Troubleshooting collections
      4. The data binding pitfall on Behaviors
    3. Services and Dependency Injection pitfalls
      1. Unable to resolve service for type
      2. No parameterless constructor defined for type
      3. Incorrect service registration
    4. Frequent custom control and converter problems
      1. Troubleshooting bindable properties
      2. Binding to the BindingContext
      3. Value converter issues
    5. Summary
    6. Further reading
  24. Index
    1. Why subscribe?
  25. Other Books You May Enjoy
    1. Packt is searching for authors like you
    2. Share Your Thoughts
    3. Download a free PDF copy of this book

Product information

  • Title: The MVVM Pattern in .NET MAUI
  • Author(s): Pieter Nijs
  • Release date: November 2023
  • Publisher(s): Packt Publishing
  • ISBN: 9781805125006