想象我有两个物体
public class OrderLine : Identifiable<Guid>
{
public Order Order {get;set;}
...
// other properties
}
public class Order : Identifiable<Guid>
{
public Guid CompanyId { get; set;}
...
// other properties
}
我想执行隐式过滤(独立于前端创建的过滤器)。我想通过
CompanyId
过滤 OrderLines,它不是直接在对象上,而是在相关的父对象上。
因此,我正在实现
OnApplyFilter(string existing filter)
,其中我需要 AttrAttribute
来创建表达式等(如 JsonApiDotnetCore 网页上所述)。
public override FilterExpression? OnApplyFilter(FilterExpression? existingFilter)
{
// some initialization and Db fetching
AttrAttribute companyIdAttribute = ResourceType.Attributes
.Single(account => account.Property.Name == "Order.CompanyId");
//This does not work and I am wondering what to do...
// ...
// some logic
var exp = new ComparisonExpression(
ComparisonOperator.Equals,
new ResourceFieldChainExpression(companyIdAttribute),
new LiteralConstantExpression("some company id which signed user has access to"))
// rest of the method checking the filter and returning the appended `existingFilter` by my
// custom filter
}
attr 查找不起作用,因为 CompanyId 上没有 [Attr]。但更重要的是,您正在尝试做的似乎是实施多租户。 https://github.com/json-api-dotnet/JsonApiDotNetCore/blob/master/test/JsonApiDotNetCoreTests/IntegrationTests/MultiTenancy/MultiTenancyTests.cs有一个完整的工作实现。在该示例中,国家/地区代码用于区分网上商店,类似于您使用的 companyID。该示例的构建方式使租户始终分离,这适用于读取和写入端点。它的核心是使用 EF Core 查询过滤器(请参阅 DbContext 类)。