组织webapi控制器的最佳方法是什么?

时间:2017-03-02 09:52:48

标签: c# asp.net-web-api routes

我有一些课程,例如CustomerBookMovieGame。 我为每个实体创建了4个控制器来执行CRUD并为每个实体指定逻辑。 现在问题是我想通过customerId检索列表游戏 我应该将方法操作放在CustomerController还是GamesController? 路线将是这样的"/api/customers/123/games" 和书,电影一样。因此,每个控制器中的3种方法是Book,Game,Movie或Customer控制器中的3种方法 那么管理这个的最佳方法是什么?

由于

1 个答案:

答案 0 :(得分:0)

要明白的一点是,web api主要基于REST。休息是基于资源的,所以如果你要返回资源游戏,那么你的GamesController应该处理它。简单

让您的客户控制器返回游戏会很有趣。 默认情况下,您的游戏控制器将具有以下内容:

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

举一个例子:

[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();
        }
    }
}

所以你看,一个Trip可以有多个停靠点,但你的retreive停止方式仍然在停止控制器中,但只是正确映射了路径