[英]What is the best way to organize webapi controller?
I have some classes such as Customer
, Book
, Movie
, Game
. 我有一些类,例如Customer
, Book
, Movie
, Game
。 I created 4 Controllers per each entity to do CRUD and specify logic per each. 我为每个实体创建了4个控制器来执行CRUD并为每个实体指定逻辑。 Now the question is I would like to retrieve a list games by customerId
Should I put the method action in CustomerController
or GamesController
? 现在的问题是,我想按customerId
检索列表游戏。应该将方法action放在CustomerController
或GamesController
吗? The route will be like this "/api/customers/123/games"
And the same with Book, Movie. 路线将类似于"/api/customers/123/games"
并且与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? 因此,每个控制器中的3种方法Book,Game,Movie或Customer控制器中的3种方法那么,什么是最好的管理方法?
Thanks 谢谢
See the point to understand is that web api is largely REST based. 看到的要点是,Web API主要基于REST。 And rest is Resource based, so if you are returning the resource Games, then your GamesController should handle it. 其余部分基于资源,因此,如果您要返回资源游戏,则GamesController应该处理它。 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 因此,您可以看到,一次旅行可以有多个停靠点,但是您回退停靠点的方式仍在停靠点控制器中,只是路线已正确映射
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.