Reading Data From CosmosDB in a Xamarin App
Recently James Montemagno and I completed an app that demonstrates several aspects of Azure and Xamarin.Forms development in the form of a contacts application.
And all the code is open sourced!
The App!
The app does several things:
- Displays a list of ... well, contacts ... downloaded from CosmosDB
- Shows the details of those contacts
- Using the geolocation capabilities of your device and the geospatial features of CosmosDB, checks to see if any contact is within 50 km of your current location.
- Displays those contacts that are nearby
- Allows the contacts to login, using Active Directory.
- And once logged in, the contacts can update their location, which invokes an Azure Function - that anonymizes the contact's exact location to be in a city (not the exact coordinates of where they are) - and then saves the location to CosmosDB
For the purposes of demo data of this app - we're using information about my co-workers!
I work as a Cloud Developer Advocate, and my teammates have specialities all over the board ... Linux, IoT, JavaScript, DevOps, Machine Learning ... and on and on and on ... Plus we're spread out across the globe and we're constantly on the go, traveling to different events.
This app then serves two purposes:
- It lets everybody see the bios and specialties of all the CDAs
- It allows everybody (including other CDAs) know who is nearby so we can get together for meetups or conferences or what-have-you!
That's the app ... but what I really want to talk about is how a Xamarin or mobile app can read data out of CosmosDB!
Why CosmosDB?
We chose CosmosDB because it has second-to-none abilities to geo-replicate data across the globe. And since any contacts (not just CDAs) will be spread out, this made total sense.
Also the SQL API portion of CosmosDB (CosmosDB is what they call multi-model, in other words, it can support SQL, Mongo, Graph, etc) supports some sweet geospatial capabilities! It has built-in APIs to determine how close 2 points are to each other, and then some.
So with those 2 things in mind, it made perfect sense to go with CosmosDB!
Reading Data From CosmosDB!
Reading data out of the SQL API portion of CosmosDB in a Xamarin app can be broken down into the following steps.
The Portal
- Create the CosmosDB in the Azure portal (ducks because of stating the obvious 😎)
- Create a database and a collection in which the database will be stored
- Note the URL and an authentication key
The Xamarin App
Note that your core project needs to be either a .NET Standard or a Shared project ... regular PCLs will not work
- Create the
DocumentClient
class - passing in the URL and authentication key from above - Create a DocumentCollectionUri which is a
Uri
object viaUriFactory.CreateDocumentCollectionUri
passing it the database name and collection name you created above - Create a
DocumentQuery
object viaDocumentClient.CreateDocumentQuery<T>
some stuffAsDocumentQuery()
- The some stuff is where things get interesting ... you can use LINQ to pass in Where clauses ... or OrderBy clauses ... or Take ... the world is your oyster!!
- Then with that
DocumentQuery
object - callHasMoreResults
on it... use it as the break condition of awhile
loop - Then within that
while
loop - keep callingExecuteNextAsync<T>
- that's what is going to pull the data from CosmosDB
One thing to note with everything above ... the T
is a model class that will be run through JSON.Net to go from the JSON that the entity is stored as in CosmosDB into a nice strongly typed object in your app.
I'm usually more verbose in my blogs ... so instead of a bunch of writing - I put together a video of implementing a means of reading data from CosmosDB - which you can view here!
That's It!
Reading data out of CosmosDB involves a couple steps to get up and started, but once you're there - the steps are repeatable, and it wouldn't take much work to abstract the reading into it's own service class.
You can read more details on grabbing data from CosmosDB on the documentation site - which is super solid.
You can also view the source code for the contacts app here.
And of course, don't miss the walk through video here!