Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature] Consider implement IEnumerable<T> to allow directly iteration #8

Open
NotAsea opened this issue Jul 17, 2024 · 1 comment

Comments

@NotAsea
Copy link

NotAsea commented Jul 17, 2024

I instinctively thought Pagination as IEnumerable and try to foreach it only to realise it just a class holding actual Results in it, so why dont we directly implement IEnumerable for it some thing like

public class Pagination<TSource> : IEnumerable<TSource>
{
    // other Properties
    public IEnumerator<TSource> GetEnumerator() => Results.GetEnumerator();
    IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
}
@SitholeWB
Copy link
Owner

SitholeWB commented Jul 21, 2024

@NotAsea I never thought of this, and I just tried to implement it.
This is a nice recommendation, and I like it, but this approach has a problem when it comes to JSON<=>Object Serialization. The fields (TotalItems, TotalPages, NextPage, etc.) are lost.
I know that you can set global configuration on the Startup file but I believe that it is much better to keep it this way, the dot results (.Results) is easy to understand.

The API just returns the following:

[
    {
        "Id": "722cde66-a1cc-4af8-84ef-6fe461bfda29",
        "Firstname": "Bob",
        "Lastname": "Smith"
    },
    {
        "Id": "fa787de9-da32-44f6-a338-6cf3c9d04cc7",
        "Firstname": "Alice",
        "Lastname": "Cool"
    }
]

The correct API response should be:

{
    "TotalItems": 3,
    "CurrentPage": 1,
    "NextPage": 2,
    "PreviousPage": null,
    "TotalPages": 2,
    "Results": [
        {
            "Id": "d541a35f-e13a-4206-9e59-5aa7b854dcdc",
            "Firstname": "Bob",
            "Lastname": "Smith"
        },
        {
            "Id": "b753eb02-ac33-4a4c-a8b4-5f68f2863985",
            "Firstname": "Alice",
            "Lastname": "Cool"
        }
    ]
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants