Skip to content
This repository has been archived by the owner on Jul 12, 2020. It is now read-only.

Commit

Permalink
Added wiki pages for CosmosStore and CosmonautClient
Browse files Browse the repository at this point in the history
  • Loading branch information
Elfocrash committed Dec 15, 2018
1 parent 5a3f9d0 commit 7b38c1f
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 4 deletions.
14 changes: 13 additions & 1 deletion docs/CosmonautClient.md
Original file line number Diff line number Diff line change
@@ -1 +1,13 @@
# The CosmonautClient
# The CosmonautClient

### What is it and why do I care?

The CosmonautClient is a wrapper around the DocumentClient that comes from the CosmosDB SDK. It's main purpose is to abstract some of the things you really don't need to know about when it comes to using Cosmos DB. An example would be the UriFactory class.

Normally the DocumentClient call requires you to provide a Uri to resources in order to perform operations. You shouldn't care. All you need to care about is that this call needs a `databaseId` and a `collectionId`. This client wrapper does that.

It also wraps the calls to Cosmos and it profiles them in order to provide performance metrics. This will only happen when you have an active event source. You can learn more about this in the Logging section.

Something worth noting is that the CosmonautClient won't throw an exception for not found documents on methods that the response is `ResourceResponse` but instead it will return null in order to make response handling easier.

Any method that returns a `CosmosResponse` will still obay all the rules described on the "CosmosResponse and response handling" section of the "The CosmosStore" page.
1 change: 0 additions & 1 deletion docs/Response-handling.md

This file was deleted.

77 changes: 76 additions & 1 deletion docs/The-CosmosStore.md
Original file line number Diff line number Diff line change
@@ -1 +1,76 @@
# The CosmosStore
# The CosmosStore

### What is it and why do I care?

The main data context you will be working with while using Cosmonaut is the CosmosStore. The CosmosStore requires you to provide the entity model that it will be working with.

For example if I only wanted to work with the class `Book` my CosmosStore initialisation would look like this:

```c#
ICosmosStore<Book> bookStore = new CosmosStore<Book>(cosmosSettings)
```

> But what is the context of the CosmosStore? What will I get if I query for all the items in a CosmosStore?

The CosmosStore's boundaries can be one of two.

* One entity is stored in it's own collection (ie books)
* One entity is stored in a shared collection that other entities live as well (ie library)

The choice to go with one or the other is completely up to you and it comes down to partitioning strategy, cost and flexibility when it comes to scaleability.

### CosmosStore's lifetime

CosmosStores should be registered as *singletons* in your system. This will achieve optimal performance. If you are using a dependency injection framework make sure they are registered as singletons and if you don't, just make sure you don't dispose them and you keep reusing them.

### CosmosStoreSettings

The CosmosStore can be initialised in multiple ways but the recommended one is by providing the `CosmosStoreSettings` object.

The `CosmosStoreSettings` object can be initialised requires 3 parameters in order to be created. The database name, the Cosmos DB endpoint Uri and the auth key.

```c#
var cosmosSettings = new CosmosStoreSettings("<<databaseName>>", "<<cosmosUri>>", "<<authkey>>");
```

There are other optional settings you can provide such as:

* ConnectionPolicy - The connection policy for this CosmosStore
* ConsistencyLevel - The level of consistency for this CosmosStore
* IndexingPolicy - The indexing policy for this CosmosStore if it's collection in not yet created
* DefaultCollectionThroughput - The default throughput for this CosmosStore if it's collection in not yet created
* JsonSerializerSettings - The object to json serialization settings
* InfiniteRetries - Whether you want infinite retries on throttled requests
* CollectionPrefix - A prefix prepended on the collection name

### CosmosResponse and response handling

By default, Cosmos DB throws exceptions for any bad operation. This includes reading documents that don't exist, pre condition failures or trying to add a document that already exists.

This makes response handing really painful so Cosmonaut changes that.

Instead of throwing an excpetion Cosmonaut wraps the responses into it's own response called `CosmosResponse`.

This object contains the following properties:

* IsSuccess - Indicates whether the operation was successful or not
* CosmosOperationStatus - A Cosmonaut enum which indicates what the status of the response is
* ResourceResponse - The ResourceResponse<Document> that contains things like RU charge, Etag, headers and all the other info that the response would normally have
* Entity - The object you used for this operation
* Exception - The exception that caused this response to fail

It also has an implicit operation which, if present, will return the entity itself.

#### CosmosOperationStatus

The CosmosOperationStatus operation status can have one of 5 values.

* Success - The operation was successful
* RequestRateIsLarge - Your CosmosDB is under heavy load and it can't handle the request
* ResourceNotFound - The item you tried to update/upsert was not found
* PreconditionFailed - The Etag that you provided is different from the document one in the database indicating that it has been changed
* Conflict - You are trying to add an item that already exists

### Notes

The CosmosStore also exposes the underlying CosmonautClient that it's using to perform operations so you can use that for any other operations you want to make against Cosmos DB. You need to know though that the CosmosStore's context is only limited for it's own methods. Once you use the CosmonautClient or the DocumentClient you are outside of the limitations of CosmosStore so be careful.
1 change: 0 additions & 1 deletion docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,5 @@ Welcome to Cosmonaut's documentation!
Document-operations
Collection-sharing
Pagination
Response-handling
Dependency-injection
Logging

0 comments on commit 7b38c1f

Please sign in to comment.