Preparing your classes for the CosmosStore¶
There are some things you need to get familiar with when it comes to using Cosmonaut. You see, Cosmonaut it doing a lot of things for you behind the scenes but it’s performing even better if you do just a little bit of configuration.
Key attributes¶
[CosmosCollection]¶
The CosmosCollection
attribute is an optional attribute that you can decorate your entity’s class with. It has two purposes.
First it allows you to override the default Cosmonaut collection naming bahaviour, which is to name your collections as a lowercase plularised version of the class name.
Second it allows you to map your class to a pre existing collection with a different name.
You can of course do a further override of theCosmosStore
target name at theCosmosStore
constructor level by providing theoverriddenCollectionName
parameter.
[CosmosPartitionKey]¶
This is a parameterless attribute. It is used to decorate the property that represents the partition key definition of your collection.
public class Book
{
[CosmosPartitionKey]
public string Name { get; set; }
[JsonProperty("id")]
public string Id { get; set; }
}
In this case I have a partition key definition in my collection named /Name
.
By decorating the Name
property with the CosmosPartitionKey
attribute I enable Cosmonaut to do a lot of behind the scenes operation optimisation where it will set the partition key value for you if present, speeding up the operation and making it more cost efficient.
[JsonProperty(“id”)]¶
This is not a Cosmonaut attribute and it is coming from JSON.NET which the underlying Cosmos DB SDK is using.
Even though not required I strongly recommend that you decorate your property which represents the id of the entity with the [JsonProperty("id")]
attribute. This will prevent any unwanted behaviour with LINQ to SQL conversions and the id property not being mapped propertly.