百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
C

Cosmonaut

> 数据库
开源

带有 ORM 支持的强大的 Azure CosmosDB .NET SDK

340 stars0 点赞0 次浏览
访问官网GitHub

工具介绍

带有 ORM 支持的强大的 Azure CosmosDB .NET SDK

!Attention! Cosmonaut is now obsolete and in read-only mode. You can still safely use it until Microsoft announces a deprecation of the v2 SDK but I highly recommend you use the official Microsoft SDK.

Cosmonaut

The word was derived from "kosmos" (Ancient Greek: κόσμος) which means world/universe and "nautes" (Ancient Greek: ναῦς) which means sailor/navigator

Cosmonaut is a supercharged SDK with object mapping capabilities that enables .NET developers to work with CosmosDB. It eliminates the need for most of the data-access code that developers usually need to write.

Documentation

Getting started

  • How to easily start using CosmosDB in your C# application in no time with Cosmonaut
  • (Video) Getting started with .NET Core and CosmosDB using Cosmonaut
  • (Video) How to save money in CosmosDB with Cosmonaut's Collection Sharing
  • CosmosDB Fluent Pagination with Cosmonaut
  • Implementing server side CosmosDB pagination in a Blazor Web App (Part 1: Page Number and Page Size)
  • Implementing server side CosmosDB pagination in a Blazor Web App (Part 2: Next/Previous Page)

Samples

  • The samples folder in this project
  • Web app server-side pagination for CosmosDB

Usage

The idea is pretty simple. You can have one CosmosStore per entity (POCO/dtos etc). This entity will be used to create a collection or use part of a one in CosmosDB and it will offer all the data access for this object.

Registering the CosmosStores in ServiceCollection for DI support

csharp
 var cosmosSettings = new CosmosStoreSettings(">", ">", "<>");
                
serviceCollection.AddCosmosStore(cosmosSettings);

//or just by using the Action extension

serviceCollection.AddCosmosStore(">", ">", "<>", settings =>
{
    settings.ConnectionPolicy = connectionPolicy;
    settings.DefaultCollectionThroughput = 5000;
    settings.IndexingPolicy = new IndexingPolicy(new RangeIndex(DataType.Number, -1),
        new RangeIndex(DataType.String, -1));
});

//or just initialise the object

ICosmosStore bookStore = new CosmosStore(cosmosSettings)

To use the AddCosmosStore extension methods you need to install the Cosmonaut.Extensions.Microsoft.DependencyInjection package.

Install-Package Cosmonaut.Extensions.Microsoft.DependencyInjection
or
dotnet add package Cosmonaut.Extensions.Microsoft.DependencyInjection
Retrieving an entity by id (and partition key)
csharp
var user = await cosmosStore.FindAsync("userId");
var user = await cosmosStore.FindAsync("userId", "partitionKey");
var user = await cosmosStore.FindAsync("userId", new RequestOptions());
Querying for entities using LINQ

In order to query for entities all you have to do is call the .Query() method and then use LINQ to create the query you want. It is HIGHLY recommended that you use one of the Async methods to get the results back, such as ToListAsync or FirstOrDefaultAsync , when available.

csharp
var user = await cosmoStore.Query().FirstOrDefaultAsync(x => x.Username == "elfocrash");
var users = await cosmoStore.Query().Where(x => x.HairColor == HairColor.Black).ToListAsync(cancellationToken);
Querying for entities using SQL
csharp
// plain sql query
var user = await cosmoStore.Query("select * from c where c.Firstname = 'Smith'").ToListAsync();
or
var user = await cosmoStore.QueryMultipleAsync("select * from c where c.Firstname = 'Smith'");

// or parameterised sql query
var user = await cosmoStore.QueryMultipleAsync("select * from c where c.Firstname = @name", new { name = "Smith" });

Collection sharing

Cosmonaut is all about making integrating with Cosmos DB easy as well as making things such as cost optimisation part of the library.

That's why Cosmonaut supports transparent collection sharing between different types of entities.

Why would you do that?

Cosmos is charging you based on how many RU/s your individual collection is provisioned at. This means that if you don't need to have one collection per entity because you won't use it that much, even on the minimum 400 RU/s, you will be charged money. That's where the magic of schemaless comes in.

How can you do that?

Well it's actually pretty simple. Just implement the ISharedCosmosEntity interface and decorate your object with the SharedCosmosCollection attribute.

The attribute accepts two properties, SharedCollectionName which is mandatory and EntityName which is optional. The SharedCollectionName property will be used to name the collection that the entity will share with other entities.

The EntityName will be used to make the object identifiable for Cosmosnaut. By default it will pluralize the name of the class, but you can specify it to override this behavior. You can override this by providing your own name by setting the EntityName value at the attribute level.

Once you set this up you can add individual CosmosStores with shared collections.

Collection naming

Your collections will automatically be named based on the plural of the object you are using in the generic type. However you can override that by decorating the class with the CosmosCollection attribute.

Example:

csharp
[CosmosCollection("somename")]

By default you are required to specify your collection name in the attribute level shared entities like this:

csharp
[SharedCosmosCollection("shared")]
public class Car : ISharedCosmosEntity
{
    public string Id { get; set; }
    public string CosmosEntityName { get; set; }
}

Even though this is convenient I understand that you might need to have a dynamic way of setting this. That's why the CosmosStore class has some extra constructors that allow you to specify the overriddenCollectionName property. This property will override any collection name specified at the attribute level and will use that one instead.

Note: If you have specified a CollectionPrefix at the CosmosStoreSettings level it will still be added. You are only overriding the collection name that the attribute would normally set.

Example

Class implementation:

csharp
[SharedCosmosCollection("shared")]
public class Car : ISharedCosmosEntity
{
    public string Id { get; set; }
    public string CosmosEntityName { get; set; }
}

CosmosStore initialisation:

csharp
var cosmosStore = new CosmosStore(someSettings, "oldcars");

The outcome of this would be a collection named oldcars because the shared collection name is overridden in the constructor. There are also method overloads for the same property at the dependency injection extension level.

Pagination

Cosmonaut supports two types of pagination.

  • Page number + Page size
  • ContinuationToken + Page size

Both of these methods work by adding the .WithPagination() method after you used any of the Query methods.

csharp
var firstPage = await booksStore.Query().WithPagination(1, 10).OrderBy(x=>x.Name).ToListAsync();
var secondPage = await booksStore.Query().WithPagination(2, 10).OrderBy(x => x.Name).ToPagedListAsync();
var thirdPage = await booksStore.Query().WithPagination(secondPage.NextPageToken, 10).OrderBy(x => x.Name).ToPagedListAsync();
var fourthPage = await thirdPage.GetNextPageAsync();
var fifthPage = await booksStore.Query().WithPagination(5, 10).OrderBy(x => x.Name).ToListAsync();

ToListAsync() on a paged query will just return the results. ToPagedListAsync() on the other hand will return a CosmosPagedResults object. This object contains the results but also a boolean indicating whether there are more pages after the one you just got but also the continuation token you need to use to get the next page.

Pagination recommendations

Because page number + page size pagination goes though all the documents until it gets to the requested page, it's potentially slow and expensive. The recommended approach would be to use the page number + page size approach once for the first page and get the results using the .ToPagedListAsync() method. This method will return the next continuation token and it will also tell you if there are more pages for this query. Then use the continuation token alternative of WithPagination to continue from your last query.

Keep in mind that this approach means that you have to keep state on the client for the next query, but that's what you'd do if you where using previous/next buttons anyway.

Adding an entity in the entity store
csharp
var newUser = new User
{
    Name = "Nick"
};
var added = await cosmoStore.AddAsync(newUser);

var multiple = await cosmoStore.AddRangeAsync(manyManyUsers);
Updating entities

When it comes to updating you have two options.

Update...

csharp
await cosmoStore.UpdateAsync(entity);

... and Upsert

csharp
await cosmoStore.UpsertAsync(entity);

The main difference is of course in the functionality. Update will only update if the item you are updating exists in the database with this id. Upsert on the other hand will either add the item if there is no item with this id or update it if an item with this id exists.

Removing entities
csharp
await cosmoStore.RemoveAsync(x => x.Name == "Nick"); // Removes all the entities that match the criteria
await cosmoStore.RemoveAsync(entity);// Removes the specific entity
await cosmoStore.RemoveByIdAsync("<>");// Removes an entity with the specified ID

Response Handling

Cosmonaut follows a different approach when it comes to error handling. The CosmosDB SDK is throwing exceptions for almost every type of error. Cosmonaut follows a different approach.

In Cosmonaut methods that return CosmosResponse or CosmosMultipleResponse won't throw exceptions for the following errors: ResourceNotFound, PreconditionFailed and Conflict. They will instead return a CosmosResponse with the IsSuccess flag to false, the CosmosOperationStatus enum explaining what the error was and the Exception object containing the exceptions that caused the request to fail.

On top of that, any methods that return ResourceResponse in the CosmonautClient will not throw an exception for ResourceNotFound and they will instead return null.

Restrictions

Because of the way the internal id property of Cosmosdb works, there is a mandatory restriction made. You cannot have a property named Id or a property with the attribute [JsonProperty("id")] without it being a string. A cosmos id needs to exist somehow on your entity model. For that reason if it isn't part of your entity you can just extend the CosmosEntity class.

It is HIGHLY RECOMMENDED that you decorate your Id property with the [JsonProperty("id")] attribute to prevent any unexpected behaviour.

CosmonautClient

Cosmonaut has its own version of a DocumentClient called CosmonautClient. The difference is that the CosmonautClient interface is more user friendly and it looks more like something you would use in a real life scenario. It won't throw not found exceptions if an item is not found but it will return null instead. It will also retry automatically when you get 429s (too many req

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

C#azureazure-cosmos-dbazure-cosmosdbcosmonaut

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月18日
分类数据库
定价开源

> 相关工具

P
PostgreSQL
功能强大的开源关系型数据库
R
Redis
内存数据结构存储,常用作缓存与队列
M
MySQL
广泛使用的开源关系型数据库