Item types
Pick the right base class — Item, EtagItem, TimeStampedItem, TimeToLiveItem, or FullItem.
The library provides a small family of base classes that opt your model into specific Cosmos DB features. Inherit from the one that has just enough behavior for your scenario.
Hierarchy at a glance
| Base class | Use when you want… |
|---|---|
Item | The simplest CRUD model. Adds Id, Type, and partition-key handling. |
EtagItem | Item + an Etag for optimistic concurrency control. Abstract. |
TimeStampedItem | Item + CreatedTimeUtc, LastUpdatedTimeUtc, and LastUpdatedTimeRaw. |
TimeToLiveItem | Item + a TimeToLive property that overrides the container default TTL. |
FullItem | Everything in EtagItem, TimeStampedItem, and TimeToLiveItem. |
The corresponding interfaces (IItem, IItemWithEtag, IItemWithTimeStamps, IItemWithTimeToLive) are exposed if you need to compose your own base.
The simplest item
using Microsoft.Azure.CosmosRepository;
public class Product : Item{ public string Name { get; set; } = null!;
public string CategoryId { get; set; } = null!;
public double Price { get; set; }
public StockInformation Stock { get; set; } = null!;
protected override string GetPartitionKeyValue() => CategoryId;}Item already gives you:
Id(default:Guid.NewGuid().ToString()).Type(the .NET class name — used as a discriminator inside shared containers).GetPartitionKeyValue()— defaults toId. Override when partitioning by another field.
When to upgrade
- Need optimistic concurrency? →
EtagItemorIItemWithEtag. - Need created / updated timestamps? →
TimeStampedItem. - Need item-level TTL overrides? →
TimeToLiveItem. - Want all three? →
FullItem.