【问题标题】:DocumentDB show all documents of specific entity typeDocumentDB 显示特定实体类型的所有文档
【发布时间】:2018-07-04 10:32:11
【问题描述】:

我有一个通用的 IDocumentDbRepository 存储库,用于为 DocumentDB 提供基本的 CRUD 操作,以及用于与特定实体的附加操作的特定存储库,它继承或实现了此接口和类。

    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 类型的文档

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 类型的文档,但它不起作用,并且列出了具有任何实体类型的所有文档。我做错了什么?

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

【问题讨论】:

    标签: c# asp.net azure asp.net-core azure-cosmosdb


    【解决方案1】:

    好的,有几件事。

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

    使用AsDocumentQuery 并调用ExecuteNextAsync 而query.HasMoreResults。

    其次,当您调用 ToList() 方法时,所有内容都作为 Employee 返回,因为您以这种方式查询它。你基本上是说,“把这个集合中的所有东西都作为员工退还给我,然后再把员工退还给我”。在这个阶段,他们都是员工。

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

    我建议您检查collection sharing 在Cosmonaut 中的工作方式。听起来完全符合您的需要。

    Here 是在不阻塞线程的情况下异步调用 ToList 方法的方法。

    免责声明:我是 Cosmonaut 的创造者。

    【讨论】:

    • 感谢您的回答,将利用您的提示。到那个属性类型。你的意思是我应该在我的Employee 模型和我的EmployeeRepository 中创建类似public bool isEmployee 的属性,我将查询只返回isEmployee==true 的实体?
    • 不完全。如果它是布尔值,那么您仅限于两种不同的类型。只需创建一个具有类似 EntityType 的单个属性的接口,然后让您的 Dtos 实现它。然后您可以通过Where(x=&gt;x.EntityType == nameof(Employee)).ToListAsync() 查询您需要的内容。
    • 我根据您的建议编辑了我的问题并创建了GetItemsAsync() 方法,它应该是怎样的?还创建了现在由我的Employee 模型实现的接口,它现在具有EntityType 属性。我想在我的EmployeeRepository 中仅返回来自GetAllEmployees() 的员工实体,但是尽管您的建议有点丢失,您能否提供一些代码如何在其中执行此操作?抱歉,我是 CosmosDB 的新手。
    • 您必须将where T : class 更改为where T : IYourInterface,然后您应该能够执行类似IDocumentQuery&lt;T&gt; query = client.CreateDocumentQuery&lt;T&gt;(UriFactory.CreateDocumentCollectionUri(DatabaseId, CollectionId)).Where(x=&gt;x.EntityType == nameof(T)).AsDocumentQuery(); 的操作
    • 你的意思是把where T : IYourInterface改成DocumentDBRepository&lt;T&gt;?它将如何提供帮助,这甚至可能吗?我想在我的EmployeeRepository 中重用那个通用的GetItemsAsync() 方法,并根据我已经通过GetItemsAsync() 方法收到的项目在某些方法中进行查询
    猜你喜欢
    • 1970-01-01
    • 2015-04-29
    • 2017-08-09
    • 2020-10-05
    • 1970-01-01
    • 2013-05-01
    • 1970-01-01
    • 2015-10-23
    • 2021-07-26
    相关资源
    最近更新 更多