DocumentDB显示特定实体类型的所有文档 [英] DocumentDB show all documents of specific entity type

查看:85
本文介绍了DocumentDB显示特定实体类型的所有文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个通用的IDocumentDbRepository存储库,用于提供DocumentDB的基本CRUD操作,以及用于特定实体的其他操作的特定存储库,这些实体继承或实现了此接口和类.

I have a generic IDocumentDbRepository repository for providing basic CRUD operations with DocumentDB and specific repositories for additional operations with specific entities, which inherits or implements this interface and class.

    public interface IDocumentDbRepository<T> where T : class
{
    //IEnumerable<T> GetItems();
    Task<IEnumerable<T>> GetItemsAsync();
    Task<T> GetItemAsync(T id);
    Task<T> AddDocumentAsync(T item);
    Task<T> UpdateDocumentAsync(T id, T item);
    Task DeletedocumentAsync(T id);
}

public class DocumentDbRepository<T> : IDocumentDbRepository<T> where T : class
{
    private readonly string AuthKey;
    private readonly string EndpointUri;
    private readonly string DatabaseId;
    private readonly string CollectionId;
    private static DocumentClient client;


    public DocumentDbRepository(IOptions<DocumentDbSettings> DocumentDbConfig)
    {
        this.DatabaseId = DocumentDbConfig.Value.DatabaseName;
        this.CollectionId = DocumentDbConfig.Value.CollectionName;
        this.AuthKey = DocumentDbConfig.Value.AuthKey;
        this.EndpointUri = DocumentDbConfig.Value.EndpointUri;
        client = new DocumentClient(new Uri(EndpointUri), AuthKey);
    }

public async Task<IEnumerable<T>> GetItemsAsync()
{
     IDocumentQuery<T> query = client.CreateDocumentQuery<T>(
     UriFactory.CreateDocumentCollectionUri(DatabaseId, CollectionId))
     .AsDocumentQuery();

    List<T> results = new List<T>();
    while (query.HasMoreResults)
    {
        results.AddRange(await query.ExecuteNextAsync<T>());
    }
    return results;
}

   // public IEnumerable<T> GetItems()
   // {
   //     var results = client.CreateDocumentQuery<T>//(UriFactory.CreateDocumentCollectionUri(DatabaseId, CollectionId)).ToList();
   //   return results;
   // }
//methods

在我特定的Employee存储库中,我旨在仅返回Employee

In my specific Employee repository I aim to return only documents of type Employee

public interface IEmployeeRepository : IDocumentDbRepository<Employee>
{
    List<Employee> GetAllEmployees();
}

public class EmployeeRepository : DocumentDbRepository<Employee>, IEmployeeRepository
{
    public EmployeeRepository(IOptions<DocumentDbSettings> DocumentDbConfig) : base(DocumentDbConfig)
    {

    }

    public List<Employee> GetAllEmployees()
    {
        return GetItems().Where(x => x.GetType() == typeof(Employee)).ToList(); 
    }
}

我在控制器中调用GetAllEmployees方法以仅在视图中列出类型为Employee的文档,但是它不起作用,并且所有具有任何实体类型的文档都被列出.我在做什么错了?

I call GetAllEmployees method in my controller to only list Documents of Type Employeein my View, but it does not work and all documents with whatever entity type gets listed. What am I doing wrong ?

public IActionResult Index()
{
    return View(_employeeRepository.GetAllEmployees());
}

推荐答案

好,几件事.

首先,您永远不要像这样在CreateDocumentQuery上调用.ToList();.它将通过网络进行多个同步调用,以便返回数据库中的每个文档.这将导致疯狂的RU/s和非常糟糕的性能.

First you should never call .ToList(); on CreateDocumentQuery like that. It will make multiple synchronous calls over the wire in order to return every single document in the database. This will cause insane RU/s and really bad performance.

使用AsDocumentQuery并在query.HasMoreResults时调用ExecuteNextAsync.

第二,当您调用ToList()方法时,所有内容都将作为Employee返回,因为您是通过这种方式查询的.您基本上说过:以雇员的身份将本集合中的所有内容归还给我,然后以雇员的身份归还给我".在这个阶段,他们都是员工.

Second, by the time you call the ToList() method, everything is returned as an Employee because you queried for it that way. You basically said, "return me everything in this collection as an Employee and then from that return me the Employees". At this stage all of them are Employees.

您真正需要的是文档上的某种type属性,您可以在执行ToList调用之前使用该属性进行查询.

What you really need is some sort of type property on your document which you can use to query for before you do the ToList call.

我建议您检查馆藏共享的工作方式https://github.com/Elfocrash/Cosmonaut"rel =" nofollow noreferrer>宇航员.听起来完全像您需要的东西.

I would recommend you check how collection sharing works in Cosmonaut. Sounds exactly like something you need.

此处异步调用ToList方法,而不会阻塞线程.

Here is how you can asynchronously call the ToList method without blocking the thread.

免责声明:我是Cosmonaut的创建者.

这篇关于DocumentDB显示特定实体类型的所有文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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