使用命令式绑定在Cosmos DB中添加文档 [英] Using imperative binding for adding documents in Cosmos DB

查看:83
本文介绍了使用命令式绑定在Cosmos DB中添加文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Azure Functions,并希望使用命令绑定到我的Cosmos数据库存储库.

I'm using Azure Functions and want to use an imperative binding to my Cosmos DB repository.

起初,我在Run-方法中定义了我的输出参数:

At first I had defined my output parameter like this in the Run-method:

[DocumentDB("TablesDB", "minified-urls", ConnectionStringSetting = "CosmosConnectionString", CreateIfNotExists = true)] out MinifiedUrl minifiedUrl,

这很棒.

但是我宁愿使用命令式绑定,所以我可以动态定义绑定中使用的值.

But I'd rather use imperative binding, so I can define the values used in the binding dynamically.

我尝试过的第一件事是这样做,因为您需要设置ConnectionStringSetting.

First thing I tried was this, which doesn't work, because you need to set the ConnectionStringSetting.

var output = await binder.BindAsync<MinifiedUrl>(new DocumentDBAttribute("TablesDB", "minified-urls"));

因此将这种绑定更改为以下内容应该可行,至少我是这样认为的.

So changing this piece of binding to the following should work, at least that's what I thought.

var output = await binder.BindAsync<MinifiedUrl>(new DocumentDBAttribute("TablesDB", "minified-urls")
{
    CreateIfNotExists = true,
    ConnectionStringSetting = "CosmosConnectionString"
});

执行此操作时,活页夹抱怨缺少Id属性,如果填写此Id属性,则还需要设置PartitionKey.

When you do this, the binder is complaining about a missing Id property and if you fill this Id property you also need to set the PartitionKey.

所以最后这就是我的想法.

So in the end this is what I came up with.

var output = await binder.BindAsync<MinifiedUrl>(new DocumentDBAttribute("TablesDB", "minified-urls")
{
    CreateIfNotExists = true,
    ConnectionStringSetting = "CosmosConnectionString",
    Id = Guid.NewGuid().ToString(),
    PartitionKey = DateTime.UtcNow.Year.ToString()
});

因为我想在存储库中创建一个新文档,所以活页夹无法找到某些内容,因此output变量是null.

Because I want to create a new document inside the repository, the binder is not able to find something, therefore the output variable is null.

那么,我想知道如何进行?声明式绑定可以正常工作,但是我不知道如何使该命令式工作生效.

So, what I'd like to know is how to proceed? The declarative binding works properly, but I can't figure out how to get this imperative working.

推荐答案

您的问题来自运行时将命令式绑定解释为输入绑定这一事实.那是因为您要请求自定义类型(MinifiedUrl),所以应该从数据库中加载这是很有意义的.

Your problems come from the fact that runtime interprets your imperative binding as input one. That's because you are requesting the custom type (MinifiedUrl), so it makes sense that this value should be loaded from the database.

要输出绑定,应请求IAsyncCollector类型,然后在其上调用AddAsync:

To make your binding output, you should request IAsyncCollector type, and then call AddAsync on it:

var output = await binder.BindAsync<IAsyncCollector<MinifiedUrl>>(
    new DocumentDBAttribute("TablesDB", "minified-urls")
    { 
        CreateIfNotExists = true,
        ConnectionStringSetting = "CosmosConnectionString"
    });
await output.AddAsync(new MinifiedUrl(...));

这篇关于使用命令式绑定在Cosmos DB中添加文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆