Online/Offline Data-Capable Cross-Platform Apps with MongoDB Atlas, Atlas Device SDKs and .NET MAUI
Rate this tutorial
In a world of always-on, always-connected devices, it is more important than ever that apps function in a way that gives a user a good experience. But as well as the users, developers matter too. We want to be able to feel productive and focus on delivery and innovation, not solving common problems.
In this article, we will look at how you can mix .NET MAUI with MongoDB’s Atlas App Services, including the Atlas Device SDKs mobile database, for online/offline-capable apps without the pain of coding for network handling and errors.
Atlas Device SDKs, formerly is an that takes advantage of MongoDB’s document data model. It is a mobile-first database that has been designed for modern data-driven applications. Although the focus of this article is the mobile side of Atlas Device SDK, it actually also supports the building of web, desktop, and IoT apps.
Atlas Device SDKs have some great features that save a lot of time as a developer. It uses an object-oriented data model so you can work directly with the native objects without needing any Object Relational Mappers (ORMs) or Data Access Objects (DAO). This also means it is simple to start working with and scales well.
Plus, Atlas Device SDKs are part of the suite of products that you get access to via the SDK. This means that Realm also has automatic access to a built-in, device-to-cloud sync feature. It uses a local database stored on the device to allow for always-on functionality. MongoDB also has Atlas, a document database as a service in the cloud, offering many benefits such as resilience, security, and scaling. The great thing about device sync with App Services is it links to a cloud-hosted , automatically taking care of syncing between them, including in the event of changes in network connectivity. By taking advantage of Atlas, you can share data between multiple devices, users, and the back ends using the same database cluster.
It takes advantage of all the great features of Atlas Device SDKs and App Services, including device sync, data partitioning based on the logged-in user, and authentication to handle the logging in and out.
Note that despite the product update name, the Realm name is still used in the library name and code for now so you will see references to Realm throughout the next sections.
This code creates your Realm Sync App and lives inside of App.Xaml.cs.
The code above is part of an initialization method and uses the RealmApp from earlier to create the connection to your app inside of App Services. This gives you access to features such as authentication (and more), as well as your Atlas data.
Working with authentication is equally as simple. Creating an account is as easy as picking an authentication type and passing the required credentials.
The most simple way is email and password auth using details entered in a form in your mobile app.
Logging in, too, is one call.
As mentioned earlier in the article, Atlas Device SDKs can work with simple C# objects with properties, and use those as fields in your document, handling mapping between object and document.
One example of this is the MovingTask object, which represents a moving task. Below is a snippet of part of the MovingTask.cs model object.
It uses standard properties, with some additional attributes from the , which mark fields as required and also say what fields they map to in the document. This is great for handling different upper and lower case naming conventions, differing data types, or even if you wanted to use a totally different name in your documents versus your code, for any reason.
You will notice that the last property uses the DateTimeOffset data type, which is part of C#. This isn’t available as a data type in a MongoDB document, but the driver is able to handle converting this to and from a supported type without requiring any manual code, which is super powerful.
Absolutely. It fully supports INotifyPropertyChanged events, meaning you don’t have to worry about whether the data is up to date. You can trust that it is. This support for events means that you don’t need to have an extra layer between your viewmodel and your database if you don’t want to.
As of Realm 10.18.0 (as it was known at the time), there is even support for Source Generators, making it even easier to work with Atlas Device SDKs and MVVM applications.
HouseMovingAssistant fully takes advantage of Source Generators. In fact, the MovingTask model that we saw earlier implements IRealmObject, which is what brings in source generation to your models.
The list of moving tasks visible on the page uses a standard IEnumerable type, fully supported by CollectionView in MAUI.
Populating that list of tasks is then easy thanks to LINQ support.
There are a couple of extra things to know about working with Atlas Device SDKs from your .NET MAUI applications.
Although as discussed above, you can easily and safely talk directly to the database (via the SDK) from your viewmodel, it is good practice to have an additional service class. This could be in a different/shared project that is used by other applications that want to talk to Atlas, or within your application for an added abstraction.
In HouseMovingAssistant, there is a RealmDatabaseService.cs class which provides a method for fetching the Realm instance. This is because you only want one instance of your Realm at a time, so it is better to have this as a public method in the service.
Because of the way Atlas Device SDKs work under the hood, any kind of operation to it — be it read, create, update, or delete — is done inside what is called a write transaction. The use of transactions means that actions are grouped together as one and if one of those fails, the whole thing fails.
Carrying out a transaction inside the Realm .NET SDK is super easy. We use it in HouseMovingAssistant for many features, including creating a new task, updating an existing task, or deleting one.
The code above creates a task using the model we saw earlier and then inside a write transaction, adds that object to the Realm database, which will in turn update the Atlas cluster it is connected to. This is a great example of how you don’t need an ORM, as we create an object from our model class and can directly add it, without needing to do anything extra.
In this article, we have gone on a whistle stop tour of .NET MAUI with Atlas Device SDKs (formerly Realm), and how you can quickly get up and running with a data capable application, with online/offline support and no need for an ORM.