c# - 访问 JsonApiDotNetCore 中 IResourceService 中的字段过滤器参数
问题描述
我目前正在尝试使用https://github.com/json-api-dotnet/JsonApiDotNetCore在 ASP.NET Core 3.1中设置非实体框架环境以通过 REST/ JSON:API访问数据
所以这是我的示例方法:
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 的情况下做同样的事情,但为了做到这一点,我错过了过滤器信息。
我可以弄清楚,有一个名为ITargetedFields
(https://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;
}
...
}
但属性Attributes
和Relationships
集合的ITargetedFields
长度始终为零。
我在文档或示例中找不到任何内容。
有任何想法吗?
解决方案
我终于找到了一种方法,如何访问查询信息,例如字段,分页,包括在IResourceService<TResource>
:
在方法中注册了几个具有 Scope-Lifetime的IQueryConstraintProvider
服务提供者:JsonApiApplicationBuilder
AddQueryStringLayer()
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);
}
...
}
推荐阅读
- postgresql - Postgres RLS 政策和功能
- android - Android链接助手中的模式匹配
- python - 如何在 Plotly 中将多索引数据框绘制为堆积条形图
- python-3.x - tkinter 样式仅在调用 theme_use 时适用
- python-3.x - 使用正则表达式更改数据框中的名称值格式
- amazon-web-services - aws - 调用 PutMetricData 操作时发生错误 (AccessDenied)
- gdb - 从终端运行 GDB 'commands' 命令?
- reactjs - 使用不在行中的材料表编辑其他字段
- python - Python循环:字符串索引超出范围
- r - 如何使函数跳过向量中的参数