Getting Started with Microsoft Cosmos DB Using C#

Adding some data

Your browser needs to be JavaScript capable to view this video

Try reloading this page, or reviewing your browser settings

This segment demonstrates how to add data into a Cosmos DB database using the CreateItems and AddItems methods.

Keywords

  • Visual Studio
  • Interfaces
  • Add Items Methods
  • Container
  • C#

About this video

Author(s)
Kevin McDonnell
First online
29 April 2020
DOI
https://doi.org/10.1007/978-1-4842-6020-3_6
Online ISBN
978-1-4842-6020-3
Publisher
Apress
Copyright information
© Kevin McDonnell 2020

Video Transcript

Adding some data– in the next section, we’re going to walk through some of the code that you can use to add some data using C#.

So back in Visual Studio again, and we’re going to take a look at some of the services. We’ll kick off looking at the interface. And just a reminder of the different methods we have to be set up, so you’ve got the Get, the Add, the Delete, the Get Items, and the Update.

For this section, we’re going to be focusing a lot more on the Add items. Let’s go into the actual– into the service itself. We’ll flick straight into the Add Item here.

You can see the basis in the formats are there. It’s asynchronous, as with all things in Cosmos DB. We’re passing a generic type into there. You can see the T on there. So we can pass any object and store that into Cosmos. It doesn’t care from that side. It’s only what we have that we pass in the code. It doesn’t worry about the details there.

We’ve got a container that’s been referenced above. You can see we’ve passed in this part of the creation of the container in the constructor. Up there, we’re passing in that database of clients, that client that we created in the start-up as well as the name of the container.

And that just retrieves the instance of that container. And then we called the Create Item Async, passing it back generic type again. We’re passing in the actual items of the object itself.

And a partition key– you can see that we’re using, in this instance, the Item ID as a partition key. But you can create the right thing for your partition key that you think is appropriate.

And then once we have that, you can return the response resource. If there’s any issues, you can see there’s a specific Cosmos exception. We can capture and handle that, not handled particularly well in that case, in the generic one. But you can build your own locking service in there.

Now we’re going to go into the base data service. And this is where we’re going to start to generate some data. See, I’ve just pasted in a whole load of items there. And now, traditional red line under everything, we’ve got there. We don’t need to pass in the container details. We can put those into the setup above itself. Just clean up some of the items there. With Visual Studio, see, if we have those red lines, we can look at the fixes. And more often than not, it’s all about using statements. So we’re just adding the correct using statements along the top. We could copy and paste those. I find it easiest to use the quick fix. And you can see the majority of those now set up and working nicely.

So all these are just plain old C# objects. And the one to notice mostly is the journey. Because while the others are primarily strings, this one has things like the start point that has the location, in this case a Starbucks Coffee object. And the end point is the cafe.

You can see we’re passing the name of it, just using Starbucks as Zephyr. Passing a date, just pick up the date and taking a little bit of random time away from that, a few hours. And you can see the values. We’re getting into that, again, the ones we showed from the object earlier, and just creating that’s a new instance of that object.

Now, what’s interesting with this as we get to the bottom, we’re creating the IDs. And we’re creating with different details there. But we aren’t actually saving any of these objects into Cosmos itself. All we’re doing is creating up these different objects as they can be used, putting the properties into there so that it can be used in that main journey on down the bottom. So we’ve got the vehicle, the nice Ford Focus being driven there, and passing all those properties into that journey object itself.

Now, we have the object. We can start to look at the service. And you can see the Cosmos service, just calling Add Item and passing that journey into there. Because it’s async. And this one’s not async. We’ve just got to get a waiter and get results there so it runs synchronously and saves that detail.

So, lots and lots of code when we’re only adding one document into the actual service itself. So let’s do a couple of tidy up of those items there. And let’s returned back to Start Up. Oh, no. Hang on. Sorry. We’re just going to make that asynchronous as well. And return to Start Up.

And we’re actually going to create that sample data at the end of the configure services. So we’ve got the base service. Because it’s static, we don’t need to set up any constructor, and just passing in those details straight into the method there. So this will ensure that we create that data every time this runs, make sure we have the relevant base data that’s needed.

So as we kick that off, we’ll see it break again, I think, on the Create Database. And of course, because we already have the database– well, it will stop there. You will actually carry on and say, yep, it’s already there. All good, all happy. You should see it pause any second now onto the creation. And if it doesn’t exist, it will create it. If not, we can just carry on.

So we can step over. And you can see it’s not failing, it’s not complaining that to say that that database already exists, I can’t again. Because that Useful method, it’s all there and done for you. And the same with container as well. So nothing additional needs to go through.

Setting up all the services there, setting up the controllers– and here you can see we’re getting into the actual generate base data. I’ll step over a few of those, creating up the objects nicely. And we’ll really get down to the service, that’s the key part we want to do.

So yeah, the service, passing again that client and database name into the service itself. When we run this standard, it’s with dependency injection. But here, we need to pass it in manually. And that’s run.

So let’s go and have a look at the data explorer. And refresh that. And with any luck, we should see– there we go, the item on top.

Now, what’s interesting when you see this, is you can see all those start point, all those child entities at the top. And down the bottom here, the ID and name. But all those child entities are there with the full child JSON. So you see things like the address line from that start point. You can see exactly where you’re Starbucks is.

For the data that we’ve saved, there are few nulls because we haven’t put what we’ve said there in the model. We have not actually populated those other details there. In fact, you can see two levels there. The vehicle driver also has home location.

There are some items down the bottom, such as the attachments and others that are added by default by Cosmos. At this stage, not anything you need to worry about. It’s not a thing you need to configure at all or make use of. So you can ignore those. Don’t need to be held in the data model– they don’t need to be held in the C# model at all.

So let’s start having a think about how we’re going to work a little bit more with the queries.