首页 > 解决方案 > 访问 JsonApiDotNetCore 中 IResourceService 中的字段过滤器参数

问题描述

我目前正在尝试使用https://github.com/json-api-dotnet/JsonApiDotNetCore在 ASP.NET Core 3.1中设置非实体框架环境以通过 REST/ JSON:API访问数据

我按照示例如下所示:https ://github.com/json-api-dotnet/JsonApiDotNetCore/blob/master/src/Examples/NoEntityFrameworkExample/Services/WorkItemService.cs

所以这是我的示例方法:

public class DepartmentResourceService : IResourceService<Department>
{

    public Task<IReadOnlyCollection<Department>> GetAsync(CancellationToken cancellationToken)
    {
        IReadOnlyCollection<Department> departments = new List<Department>{
            new Department{ Id = 1, Name = "SE", Contact = "se@someaddress.at" },
            new Department{ Id = 2, Name = "SD", Contact = "sd@someaddress.at" }
        }.AsReadOnly();
        return Task.FromResult(departments);
    }

...

}

我的示例运行良好,但我还没有弄清楚如何访问给定的 JSON:API fields-filter。但是:过滤器确实以某种方式自动应用,并且仅发送查询字符串中定义的给定字段,但在生成对象应用过滤器。使用 EF 时,我可以看到 sql-queries 已经限制在定义的 JSON:API fields-filter列表中,因此该对象仅填充了请求的信息,没有其他内容。

我想在没有 EF 的情况下做同样的事情,但为了做到这一点,我错过了过滤器信息。

我可以弄清楚,有一个名为ITargetedFieldshttps://github.com/json-api-dotnet/JsonApiDotNetCore/blob/master/src/JsonApiDotNetCore/Resources/TargetedFields.cs/)的接口,我想也许这可能是像这样注入构造函数:

public class DepartmentResourceService : IResourceService<Department>
{
    private readonly ITargetedFields targetedFields;

    public DepartmentResourceService(ITargetedFields targetedFields)
    {
        this.targetedFields = targetedFields;
    }
...
}

但属性AttributesRelationships集合的ITargetedFields长度始终为零。

我在文档或示例中找不到任何内容。

有任何想法吗?

标签: c#jsonrest.net-corejson-api

解决方案


我终于找到了一种方法,如何访问查询信息,例如字段,分页,包括在IResourceService<TResource>

在方法中注册了几个具有 Scope-Lifetime的IQueryConstraintProvider服务提供者:JsonApiApplicationBuilderAddQueryStringLayer()

  • IIncludeQueryStringParameterReader
  • IFilterQueryStringParameterReader
  • ISortQueryStringParameterReader
  • ISparseFieldSetQueryStringParameterReader
  • IPaginationQueryStringParameterReader
  • IResourceDefinitionQueryableParameterReader

它们由 DI 在您的IResourceService<TResource>实现的构造函数中注入,这是一个示例:

public class DepartmentResourceService : IResourceService<Department>
{
    private readonly ISparseFieldSetQueryStringParameterReader _sparseFieldSetQueryStringParameterReader;
    private readonly IIncludeQueryStringParameterReader _includeQueryStringParameterReader;
    private readonly ISortQueryStringParameterReader _sortQueryStringParameterReader;
    private readonly IPaginationQueryStringParameterReader _paginationQueryStringParameterReader;

    public DepartmentResourceService(
        ISparseFieldSetQueryStringParameterReader sparseFieldSetQueryStringParameterReader,
        IIncludeQueryStringParameterReader includeQueryStringParameterReader,
        ISortQueryStringParameterReader sortQueryStringParameterReader,
        IPaginationQueryStringParameterReader paginationQueryStringParameterReader
        )
    {
        _sparseFieldSetQueryStringParameterReader = sparseFieldSetQueryStringParameterReader;
        _includeQueryStringParameterReader = includeQueryStringParameterReader;
        _sortQueryStringParameterReader = sortQueryStringParameterReader;
        _paginationQueryStringParameterReader = paginationQueryStringParameterReader;
    }
    public Task<IReadOnlyCollection<Department>> GetAsync(CancellationToken cancellationToken)
    {

        // Accessing all provided information:
        IReadOnlyCollection<ExpressionInScope> constraints = _sparseFieldSetQueryStringParameterReader.GetConstraints();
        IReadOnlyCollection<ExpressionInScope> includes = _includeQueryStringParameterReader.GetConstraints();
        IReadOnlyCollection<ExpressionInScope> sortQuery = _sortQueryStringParameterReader.GetConstraints();
        IReadOnlyCollection<ExpressionInScope> pagination = _paginationQueryStringParameterReader.GetConstraints();

        // ***************************************************************
        // Do what ever you need to do, with the information provided here
        // ***************************************************************

        // Return something
        IReadOnlyCollection<Department> departments = new List<Department>{
            new Department{ Id = 1, Name = "SE", Contact = "se@someaddress.at" },
            new Department{ Id = 2, Name = "SD", Contact = "sd@someaddress.at" }
        }.AsReadOnly();
        return Task.FromResult(departments);
    }
...
}

推荐阅读