简体   繁体   中英

What is the best way to organize webapi controller?

I have some classes such as Customer , Book , Movie , Game . I created 4 Controllers per each entity to do CRUD and specify logic per each. Now the question is I would like to retrieve a list games by customerId Should I put the method action in CustomerController or GamesController ? The route will be like this "/api/customers/123/games" And the same with Book, Movie. So 3 methods in each controller Book, Game, Movie or 3 methods in Customer controller So what is the best way to manage this?

Thanks

See the point to understand is that web api is largely REST based. And rest is Resource based, so if you are returning the resource Games, then your GamesController should handle it. Simple

It would be funny to have your Customer controller return games. By default your Games controller will have something like:

[HttpGet]
[Route("api/games/{customerId}"]
public List<Games> Get(int customerId)
{
    // get games from repository
    return games;
}

take an example:

[RoutePrefix("api")]
public class TripsController: ApiController
{
    #region Fields

    private readonly IQueryDispatcher _queryDispatcher;

    #endregion

    #region Constructors

    /// <summary>
    /// Initializes a new instance of the <see cref="TripsController"/> class
    /// </summary>
    /// <param name="queryDispatcher">Query Dispatcher</param>
    public TripsController(IQueryDispatcher queryDispatcher)
    {
        if (queryDispatcher == null)
            throw new ArgumentNullException(nameof(queryDispatcher));
        _queryDispatcher = queryDispatcher;
    }

    #endregion

    #region Actions

    [HttpGet]
    [Route("trips", Name = "TripList")]
    public IHttpActionResult Get([FromUri]TripsQuery query)
    {
        try
        {
            if (query == null)
                return BadRequest();
            var result = _queryDispatcher.Dispatch<TripsQuery, TripsQueryResult>(query);
            HttpContext.Current.Response.Headers.AddPaginationHeader(query, result, new UrlHelper(Request), "TripList");

            return Ok(result);
        }
        catch (Exception)
        {
            return InternalServerError();
        }
    }

    [HttpGet]
    [Route("trips/{tripId}")]
    public IHttpActionResult Get([FromUri]TripDetailsQuery query)
    {
        try
        {
            var result = _queryDispatcher.Dispatch<TripDetailsQuery, TripDetailsQueryResult>(query);
            return Ok(result);
        }
        catch (Exception)
        {
            return InternalServerError();
        }
    }

    #endregion
}

[RoutePrefix("api")]
public class StopsController: ApiController
{
    #region Fields

    private readonly IQueryDispatcher _queryDispatcher;

    #endregion

    #region Constructors

    /// <summary>
    /// Initializes a new instance of the <see cref="StopsController"/> class
    /// </summary>
    /// <param name="queryDispatcher">Query Dispatcher</param>
    public StopsController(IQueryDispatcher queryDispatcher)
    {
        if (queryDispatcher == null)
            throw new ArgumentNullException(nameof(queryDispatcher));
        _queryDispatcher = queryDispatcher;
    }

    #endregion

    [Route("trips/{tripId}/stops", Name = "StopList")]
    [HttpGet]
    public IHttpActionResult Get([FromUri]StopsQuery query)
    {
        try
        {
            if (query == null)
                return BadRequest();
            var result = _queryDispatcher.Dispatch<StopsQuery, StopsQueryResult>(query);
            HttpContext.Current.Response.Headers.AddPaginationHeader(query, result, new UrlHelper(Request), "StopList");
            return Ok(result);
        }
        catch (Exception)
        {
            return InternalServerError();
        }
    }

    [Route("trips/{tripId}/stops/{stopId}")]
    [HttpGet]
    public IHttpActionResult Get([FromUri]StopDetailsQuery query)
    {
        try
        {
            if (query == null)
                return BadRequest();
            var result = _queryDispatcher.Dispatch<StopDetailsQuery, StopDetailsQueryResult>(query);
            return Ok(result);
        }
        catch (Exception)
        {
            return InternalServerError();
        }
    }
}

So you see, a Trip can have more than one stops, but the way you retreive stops is still in the stops controller but just have the routes mapped properly

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM