-
Notifications
You must be signed in to change notification settings - Fork 4
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
How to support creating links for sub-collections in the PagedListRepresentation sample? #42
Comments
This is pretty fiddly. I think it's going to be tricky to come up with a universal solution for this which remains inline with the intention that Hallo requires no modification to controllers or models, and which doesn't have an over-complicated setup. Two very similar options are below. The first requires public class CarListRepresentation : PagedListRepresentation<Car>
{
public CarListRepresentation(CarRepresentation carRepresentation, IActionContextAccessor actionContextAccessor)
: base($"/races/{actionContextAccessor.ActionContext.RouteData.Values["raceId"]}/cars", carRepresentation)
{ }
} The second option is basically the same but makes use of public class CarListRepresentation : PagedListRepresentation<Car>
{
public CarListRepresentation(CarRepresentation carRepresentation, IHttpContextAccessor httpContextAccessor)
: base($"/races/{httpContextAccessor.HttpContext!.GetRouteValue("raceId")}/cars", carRepresentation)
{ }
} Both options require intimate knowledge of the route values from the endpoint which could potentially be a fragile dependency - someone might do a seemingly harmless rename on the route value which would quietly break this. It's also pretty ugly looking but it could be tidied up with a developers own abstraction if needs be. This also increases the risk of weird lifestyle issues if using the ASP.NET Core dependency injection mechanism and a developer registers Lastly these options really limit the portability of representations. If we wanted to reuse this same representation for a sub-collection under another resource then we couldn't use the same I will keep experimenting with options for this though I think some trade offs will have to be accepted in this scenario. |
Example resolving
public class CarListRepresentation : PagedListRepresentation<Car>
{
private readonly IHttpContextAccessor _httpContextAccessor;
public CarListRepresentation(CarRepresentation carRepresentation, IHttpContextAccessor httpContextAccessor)
: base(carRepresentation)
{
_httpContextAccessor = httpContextAccessor;
}
protected override string GetBaseUrl() =>
$"/races/{_httpContextAccessor.HttpContext!.GetRouteValue("raceId")}/cars";
} |
That or just use the full url https://docs.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.http.extensions.urihelper.getencodedurl |
Actually you won't want that as you'll want relative paths but you could just use the request path? |
Yeah you could. I think there are lots of options for this and I'm not sure that the library can cover all scenarios for everyone. What might help is a better sample which shows a more complex scenario which can at least provide a starting point for people to adapt. |
Yarp!
…On Thu, 25 Feb 2021 at 19:00, Jason Mitchell ***@***.***> wrote:
Yeah you could. I think there are lots of options for this and I'm not
sure that the library can cover all scenarios for everyone. What might help
is a better sample which shows a more complex scenario which can at least
provide a starting point for people to adapt.
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<#42 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAAZVJS6PDXRC5BHNNEYYCTTA2M5LANCNFSM4YCZTJXA>
.
|
The
Hallo.Sample
project includes an examplePagedListRepresentation<T>
which accepts thebaseUrl
for the collection. Below is the snippet of how this is constructed.hallo/Hallo.Sample/Models/Hypermedia/PersonRepresentation.cs
Lines 24 to 28 in db5d14e
This works fine for constant base URLs like in this example but is more difficult when the base URL is dynamic. For example, if the URL to the collection was
/race/123/cars
(where123
is the resource id) how do we support getting the race id into the base URL?The text was updated successfully, but these errors were encountered: