【发布时间】:2019-01-29 16:44:56
【问题描述】:
我正在尝试了解 .net 核心中的洋葱架构模式。鉴于下面的 Github 示例项目,您将如何包含相关实体?例如,在 GamesController 中,您还如何返回相关的平台实体?由于每个服务都绑定到一个实体存储库
https://github.com/CubicleJockey/OnionPattern
/// <summary>
/// Get a list of all games.
/// </summary>
/// <returns></returns>
[HttpGet]
[Route("all")]
public IActionResult Get()
{
return ExecuteAndHandleRequest(() => GameRequestAggregate.GetAllGamesRequest.Execute());
}
public class GameRequestAggregate : BaseRequestAggregate<Domain.Game.Entities.Game>, IGameRequestAggregate
{
public GameRequestAggregate(IRepository<Domain.Game.Entities.Game> repository, IRepositoryAggregate repositoryAggregate)
: base(repository, repositoryAggregate) {}
#region Implementation of IGameRequestAggregate
private ICreateGameRequest createGameRequest;
public ICreateGameRequest CreateGameRequest => createGameRequest ?? (createGameRequest = new CreateGameRequest(Repository, RepositoryAggregate));
private IDeleteGameByIdRequest deleteGameByIdRequest;
public IDeleteGameByIdRequest DeleteGameByIdRequest =>deleteGameByIdRequest ?? (deleteGameByIdRequest = new DeleteGameByIdRequest(Repository, RepositoryAggregate));
private IGetAllGamesRequest getAllGamesRequest;
public IGetAllGamesRequest GetAllGamesRequest => getAllGamesRequest ?? (getAllGamesRequest = new GetAllGamesRequest(Repository, RepositoryAggregate));
#endregion
}
public class GetAllGamesRequest : BaseServiceRequest<Domain.Game.Entities.Game>, IGetAllGamesRequest
{
public GetAllGamesRequest(IRepository<Domain.Game.Entities.Game> repository, IRepositoryAggregate repositoryAggregate)
: base(repository, repositoryAggregate) { }
#region Implementation of IGetAllGamesRequest
public GameListResponse Execute()
{
Log.Information("Retrieving Games List...");
var gameListResponse = new GameListResponse();
try
{
var games = Repository.GetAll()?.ToArray();
if (games == null || !games.Any())
{
var exception = new Exception("No Games Returned.");
Log.Error(EXCEPTION_MESSAGE_TEMPLATE, exception.Message);
HandleErrors(gameListResponse, exception, 404);
}
else
{
gameListResponse = new GameListResponse
{
Games = games,
StatusCode = 200
};
var count = games.Length;
Log.Information("Retrieved [{Count}] Games.", count);
}
【问题讨论】:
标签: asp.net-core onion-architecture