https://github.com/ealsur/concurrentdocdb
Azure DocumentDB concurrenty-aware extensions
https://github.com/ealsur/concurrentdocdb
azure azure-documentdb concurrency documentdb
Last synced: 7 months ago
JSON representation
Azure DocumentDB concurrenty-aware extensions
- Host: GitHub
- URL: https://github.com/ealsur/concurrentdocdb
- Owner: ealsur
- License: mit
- Created: 2017-03-17T14:42:09.000Z (almost 9 years ago)
- Default Branch: master
- Last Pushed: 2017-03-27T12:39:28.000Z (almost 9 years ago)
- Last Synced: 2025-07-21T12:51:10.833Z (7 months ago)
- Topics: azure, azure-documentdb, concurrency, documentdb
- Language: C#
- Homepage:
- Size: 24.4 KB
- Stars: 5
- Watchers: 3
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Azure DocumentDB concurrency-aware extensions
## Introduction
DocumentDB provides [Optimistic Concurrency Control support](https://docs.microsoft.com/azure/documentdb/documentdb-faq) with the [ETag](https://en.wikipedia.org/wiki/HTTP_ETag) attribute and the [AccessCondition](https://msdn.microsoft.com/library/en-us/Dn799196.aspx) options.
Handling these scenarios is not a cookie-cutter problem, it requires a number of moving parts and try/catch cases to nail it.
Taking a snippet of the [official sample repo](https://github.com/Azure/azure-documentdb-dotnet/blob/master/samples/code-samples/DocumentManagement/Program.cs#L397):
try
{
var ac = new AccessCondition { Condition = readDoc.ETag, Type = AccessConditionType.IfMatch };
readDoc.SetPropertyValue("foo", "the updated value of foo");
updatedDoc = await client.ReplaceDocumentAsync(readDoc, new RequestOptions { AccessCondition = ac });
}
catch (DocumentClientException dce)
{
// now notice the failure when attempting the update
// this is because the ETag on the server no longer matches the ETag of doc (b/c it was changed in step 2)
if (dce.StatusCode == HttpStatusCode.PreconditionFailed)
{
Console.WriteLine("As expected, we have a pre-condition failure exception\n");
}
}
These extensions are an attempt to make it less error-prone and easier to implement.
With the included extensions, the former example changes to:
updateDoc = await client.ReplaceConcurrentDocumentAsync(readDoc).OnConcurrencyException((exception)=>
{
//The original exception is in exception.InnerException
Console.WriteLine($"As expected, we have a pre-condition failure exception: {exception.Message}");
});
The key factors are the [ReplaceConcurrentDocumentAsync](https://github.com/ealsur/concurrentdocdb/blob/master/src/DocumentDb.Concurrency/DocumentClientExtensions.cs) extension and the [OnConcurrencyException](https://github.com/ealsur/concurrentdocdb/blob/master/src/DocumentDb.Concurrency/ConcurrencyExceptionTaskExtension.cs) extension that gives you the opportunity to define a handler that will only execute on a Concurrency error.
These extensions help reduce moving parts and visually aid in understanding the program flow.
## Get it
You can obtain these extensions as a [Nuget Package](https://www.nuget.org/packages/DocumentDB.Concurrency).
Install-Package DocumentDB.Concurrency
Or reference it and use it according to the [License](./LICENSE).
## Issues
Please feel free to [report any issues](https://github.com/ealsur/concurrentdocdb/issues) you might encounter. I am always looking for feedback!
## Supported Frameworks
* .Net 4.5 Full Framework
* [.Net Standard Library](https://docs.microsoft.com/en-us/dotnet/articles/standard/library) 1.6