Question

Is it possible to have multiple IReturn<> on a request DTO?

For example following route:

[Route("/api/whatever", "GET,POST,PUT,DELETE")]
public class WhateverRequest : IReturn<bool>, IReturn<List<Whatever>>
{
    public string WhateverId { get; set; }
}

Depending on the request method I want to have another IReturn. Post-Put-Delete Request should only return a acknowledge if the request was successful:

IReturn<bool>

but on a GET request I want to have a:

IReturn<List<Whatever>>

It would also be good if there is a way to reflect this in Swagger Api/ Metadata Page. Currently only the first IReturn is shown.

Is this possible or would it be better to create a route for each different IReturn?

Was it helpful?

Solution

You definitely want to be creating different routes to handle the multiple return types. Only one IReturn<T> or IReturnVoid is expected, or consuming clients wouldn't know how to type the returned data correctly.

[Route("/api/whatever", "GET")]
public class ListWhateverRequest : IReturn<List<Whatever>>
{
    public string WhateverId { get; set; }
}

// Action
public List<Whatever> Get(ListWhateverRequest request)
{
    ...
}

[Route("/api/whatever", "POST,PUT,DELETE")]
public class UpdateWhateverRequest : IReturn<bool>
{
    public string WhateverId { get; set; }
}

// Action
public bool Post(UpdateWhateverRequest request)
{
    ...
}

public bool Put(UpdateWhateverRequest request)
{
    ...
}

public bool Delete(UpdateWhateverRequest request)
{
    ...
}

I presume you are returning true from these methods to show they completed successfully. Do the methods ever return false when something goes wrong, or is an exception thrown instead? If you are only throwing exception in the method, and never returning false then instead of returning bool consider using void methods with IReturnVoid. The request is therefore successful if it doesn't throw an exception.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top