Connecting to a Database and Implementing CRUD Operations
How many apps on your phone don’t store information in some sort of database? Probably not many. Even simple apps, such as calculators, often save a history of operations.
Back in 2022, when .NET MAUI had its first official release, I built a simple .NET MAUI application. I was surprised to encounter noticeable lags despite the UI being relatively simple. After profiling the app, I discovered that the problem lay in how I was loading data from the database. In this chapter, I will describe how to avoid the issues I encountered and implement best practices for database communication.
In each recipe covered in this chapter, we will build on the project from the previous step. By the end of this chapter, you’ll have implemented a CRUD application that retrieves information from both local and remote storage. You’ll learn how to create separate views to display and edit information, and how to effectively pass data and updates between these views. We’ll also implement the unit of work and repository patterns with caching support, showing you how these patterns make it easy to replace one data retrieval mechanism with another.
In this chapter, we’ll be covering the following topics:
- Connecting to a local SQLite database via Entity Framework Core (EF Core)
- Implementing create and delete operations
- Implementing detail and edit forms
- Implementing the unit of work and repository patterns
- Handling errors while saving data to a database
- Validating editors in the UI before data posting
- Implementing data caching for enhanced performance
- Connecting to a remote web API service