Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Program.cs #2

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 4 additions & 30 deletions CosmosGettingStartedDotnetCoreTutorial/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -118,21 +118,8 @@ private async Task AddItemsToContainerAsync()
IsRegistered = false
};

// Read the item to see if it exists. Note ReadItemAsync will not throw an exception if an item does not exist. Instead, we check the StatusCode property off the response object.
ItemResponse<Family> andersenFamilyResponse = await this.container.ReadItemAsync<Family>(andersenFamily.Id, new PartitionKey(andersenFamily.LastName));

if (andersenFamilyResponse.StatusCode == HttpStatusCode.NotFound)
{
// Create an item in the container representing the Andersen family. Note we provide the value of the partition key for this item, which is "Andersen"
andersenFamilyResponse = await this.container.CreateItemAsync<Family>(andersenFamily, new PartitionKey(andersenFamily.LastName));

// Note that after creating the item, we can access the body of the item with the Resource property off the ItemResponse. We can also access the RequestCharge property to see the amount of RUs consumed on this request.
Console.WriteLine("Created item in database with id: {0} Operation consumed {1} RUs.\n", andersenFamilyResponse.Resource.Id, andersenFamilyResponse.RequestCharge);
}
else
{
Console.WriteLine("Item in database with id: {0} already exists\n", andersenFamilyResponse.Resource.Id);
}
// Upsert the item, i.e. if it exists it gets updated, otherwise it gets created
ItemResponse<Family> andersenFamilyResponse = await this.container.UpsertItemAsync<Family>(andersenFamily, new PartitionKey(andersenFamily.LastName));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe this beats the purpose of the original code, while I agree that Upsert solves the exception, the code was showing how to handle a scenario where an item might Not exist.

Possible try/catch CosmosException when StatusCode == NotFound

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ealsur Thanks for the feedback. I added try catch semantics to the code. Keep in mind that the method Container.ReadItemAsync returns an exception when the code runs. Therefore, since the Cosmos Database and its container are empty the catch block will always execute and the items will get created in each catch clause.


// Create a family object for the Wakefield family
Family wakefieldFamily = new Family
Expand Down Expand Up @@ -170,21 +157,8 @@ private async Task AddItemsToContainerAsync()
IsRegistered = true
};

// Read the item to see if it exists
ItemResponse<Family> wakefieldFamilyResponse = await this.container.ReadItemAsync<Family>(wakefieldFamily.Id, new PartitionKey(wakefieldFamily.LastName));

if (wakefieldFamilyResponse.StatusCode == HttpStatusCode.NotFound)
{
// Create an item in the container representing the Wakefield family. Note we provide the value of the partition key for this item, which is "Wakefield"
wakefieldFamilyResponse = await this.container.CreateItemAsync<Family>(wakefieldFamily, new PartitionKey(wakefieldFamily.LastName));

// Note that after creating the item, we can access the body of the item with the Resource property off the ItemResponse. We can also access the RequestCharge property to see the amount of RUs consumed on this request.
Console.WriteLine("Created item in database with id: {0} Operation consumed {1} RUs.\n", wakefieldFamilyResponse.Resource.Id, wakefieldFamilyResponse.RequestCharge);
}
else
{
Console.WriteLine("Item in database with id: {0} already exists\n", wakefieldFamilyResponse.Resource.Id);
}
// Upsert the item, i.e. if it exists it gets updated, otherwise it gets created
ItemResponse<Family> wakefieldFamilyResponse = await this.container.UpsertItemAsync<Family>(wakefieldFamily, new PartitionKey(wakefieldFamily.LastName));
}

/*
Expand Down