JSON API 多对多与超媒体库

JSON API Many-Many with Hypermedia Library

我正在尝试为 discosWeb API 创建一个 SDK。

从表面上看,这似乎遵循了 JSON API 标准。例如,这里有一个 space 携带的对象的数据示例:

{
    "data": [
        {
            "type": "object",
            "attributes": {
                "shape": "Cyl",
                "height": 28.0,
                "xSectAvg": 59.8316320876176,
                "xSectMax": 72.9933461154505,
                "name": "Sputnik (8K71PS) Blok-A",
                "cosparId": "1957-001A",
                "depth": 28.0,
                "satno": 1,
                "xSectMin": 5.30929158456675,
                "objectClass": "Rocket Body",
                "length": 2.6,
                "vimpelId": null,
                "mass": 3964.32
            },
            "relationships": {
                "states": {
                    "links": {
                        "self": "/api/objects/1/relationships/states",
                        "related": "/api/objects/1/states"
                    }
                },
                "destinationOrbits": {
                    "links": {
                        "self": "/api/objects/1/relationships/destination-orbits",
                        "related": "/api/objects/1/destination-orbits"
                    }
                },
                "launch": {
                    "links": {
                        "self": "/api/objects/1/relationships/launch",
                        "related": "/api/objects/1/launch"
                    }
                },
                "initialOrbits": {
                    "links": {
                        "self": "/api/objects/1/relationships/initial-orbits",
                        "related": "/api/objects/1/initial-orbits"
                    }
                },
                "operators": {
                    "links": {
                        "self": "/api/objects/1/relationships/operators",
                        "related": "/api/objects/1/operators"
                    }
                },
                "reentry": {
                    "links": {
                        "self": "/api/objects/1/relationships/reentry",
                        "related": "/api/objects/1/reentry"
                    }
                }
            },
            "id": "1",
            "links": {
                "self": "/api/objects/1"
            }
        }
    ],
    "links": {
        "self": "/api/objects?page%5Bsize%5D=1",
        "first": "/api/objects?page%5Bsize%5D=1&page%5Bnumber%5D=1",
        "last": "/api/objects?page%5Bsize%5D=1&page%5Bnumber%5D=61384",
        "next": "/api/objects?page%5Bsize%5D=1&page%5Bnumber%5D=2",
        "prev": null
    },
    "meta": {
        "pagination": {
            "totalPages": 61384,
            "currentPage": 1,
            "pageSize": 1
        }
    }
}

反序列化这些数据显然不像简单的 REST API 那样简单,它刚刚返回了包含我们需要的所有数据的漂亮 POCO。所以,我正试图找到一个可以为我完成这部分繁重工作的库。

我一直在看这个list of implementations and I'm trying to give this Hypermedia library

不幸的是,文档有点稀疏...但是随着快速入门,我将这个class(和其他一些)作为我的对象之一。

public record DiscosObject : DiscosModelBase
{
    public int Id { get; init; }
    public string? CosparId { get; init; }
    public string? VimpelId { get; init; }
    public int? SatNo { get; init; }
    public string? Shape { get; init; }
    public float? Mass { get; init; }
    public float? Length { get; init; }
    public float? Height { get; init; }
    public float? Depth { get; init; }
    public double CrossSectionMaximum { get; init; }
    public double CrossSectionMinimum { get; init; }
    public double CrossSectionAverage { get; init; }
    public ObjectClass? ObjectClass { get; init; }
    public Reentry Reentry { get; init; }
    public IReadOnlyCollection<Country> States { get; init; }
    public IReadOnlyCollection<OrbitDetails> DestinationOrbits { get; init; }
    public IReadOnlyCollection<OrbitDetails> InitialOrbits { get; init; }
    public IReadOnlyCollection<Organisation> Operators { get; init; }
}

所以我正在尝试为它创建我的 Builder()

        builder.With<DiscosObject>("object")
               .Id(nameof(DiscosObject.Id))
               .HasMany<Country>("states")
                .Template("/api/objects/{id}/states", nameof(DiscosObject.Id), res =>res.Id)
               .HasMany<OrbitDetails>("initial-orbits"))
                .Template("/api/objects/{id}/relationships/destination-orbits", nameof(DiscosObject.Id), res =>res.Id)
               .HasMany<OrbitDetails>("destination-orbits"))
               //etc...

但是这里的关系建模让我很困惑。我们有 With()BelongsTo()HasMany()。但是,当我们需要 With() 以及如何在这里建立多对多关系模型时,对我来说并不明显。

例如,国际空间站有俄罗斯、美国、欧盟等多个国家,每个国家都有很多物体,但没有 BelongsToMany() 选项。我不确定这是框架的限制还是我理解的限制。

有没有人对这个库有任何经验并且知道我哪里出错了或者知道更简单的方法来做到这一点?

With用于定义对象,而HasManyBelongsTo用于定义对象之间的关系。

Many-to-Many 关系实际上只是存在于两个对象上的 HasMany