Skip to content

Commit

Permalink
feat: 大量重構 Application 及 Server
Browse files Browse the repository at this point in the history
  • Loading branch information
aa89227 committed Dec 24, 2023
1 parent 5e60e34 commit e844226
Show file tree
Hide file tree
Showing 39 changed files with 325 additions and 280 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace Application.Common;

public interface IPresenter<TResponse>
public interface IPresenter<in TResponse>
{
public Task PresentAsync(TResponse response);
}
6 changes: 3 additions & 3 deletions Application/Common/IRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ internal static class RepositoryExtensions
{
internal static string Save(this IRepository repository, Domain.Monopoly domainMonopoly)
{
Monopoly monopoly = domainMonopoly.ToApplication();
var monopoly = domainMonopoly.ToApplication();
return repository.Save(monopoly);
}
/// <summary>
Expand All @@ -25,14 +25,14 @@ internal static string Save(this IRepository repository, Domain.Monopoly domainM
/// <returns></returns>
private static Monopoly ToApplication(this Domain.Monopoly domainMonopoly)
{
Player[] players = domainMonopoly.Players.Select(player =>
var players = domainMonopoly.Players.Select(player =>
{
var playerChess = player.Chess;

Chess chess = new(playerChess.CurrentBlockId, playerChess.CurrentDirection.ToApplicationDirection());

var landContracts = player.LandContractList.Select(contract =>
new LandContract(contract.Land.Id, contract.InMortgage, contract.Deadline)).ToArray();
new LandContract(contract.Land.Id, contract.InMortgage, contract.Deadline)).ToArray();

return new Player(
player.Id,
Expand Down
10 changes: 4 additions & 6 deletions Application/Common/QueryUsecase.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
namespace Application.Common;
public abstract class QueryUsecase<TRequest, TResponse> where TRequest : Request
public abstract class QueryUsecase<TRequest, TResponse>(IRepository repository)
where TRequest : Request
{
protected IRepository Repository { get; }
public QueryUsecase(IRepository repository)
{
Repository = repository;
}
protected IRepository Repository { get; } = repository;

public abstract Task ExecuteAsync(TRequest request, IPresenter<TResponse> presenter);
}
2 changes: 1 addition & 1 deletion Application/Common/Request.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
namespace Application.Common;

public record Request(string GameId, string PlayerId);
public abstract record Request(string GameId, string PlayerId);
6 changes: 6 additions & 0 deletions Application/Common/Response.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
using Domain.Common;

namespace Application.Common;

public abstract record Response();
public abstract record CommandResponse(IReadOnlyList<DomainEvent> Events) : Response;
14 changes: 4 additions & 10 deletions Application/Common/Usecase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,10 @@

namespace Application.Common;

public abstract class Usecase<TRequest> where TRequest : Request
public abstract class Usecase<TRequest, TResponse>(IRepository repository)
where TRequest : Request where TResponse : Response
{
protected IRepository Repository { get; }
protected IEventBus<DomainEvent> EventBus { get; }
protected IRepository Repository { get; } = repository;

public Usecase(IRepository repository, IEventBus<DomainEvent> eventBus)
{
Repository = repository;
EventBus = eventBus;
}

public abstract Task ExecuteAsync(TRequest request);
public abstract Task ExecuteAsync(TRequest request, IPresenter<TResponse> presenter);
}
6 changes: 3 additions & 3 deletions Application/DependencyInjection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,18 @@ private static IServiceCollection AddUseCases(this IServiceCollection services)
{
var assembly = typeof(DependencyInjection).Assembly;
var types = assembly.GetTypes();
var useCaseType = typeof(Usecase<>);
var useCaseType = typeof(Usecase<,>);
var queryUsecaseType = typeof(QueryUsecase<,>);

foreach (var type in types.Where(t => t.BaseType?.IsGenericType is true))
{
if (type.BaseType?.GetGenericTypeDefinition() == useCaseType)
{
services.AddScoped(type, type);
services.AddTransient(type, type);
}
else if (type.BaseType?.GetGenericTypeDefinition() == queryUsecaseType)
{
services.AddScoped(type, type);
services.AddTransient(type, type);
}
}

Expand Down
14 changes: 6 additions & 8 deletions Application/Usecases/BidUsecase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,12 @@ namespace Application.Usecases;
public record BidRequest(string GameId, string PlayerId, decimal BidPrice)
: Request(GameId, PlayerId);

public class BidUsecase : Usecase<BidRequest>
{
public BidUsecase(IRepository repository, IEventBus<DomainEvent> eventBus)
: base(repository, eventBus)
{
}
public record BidResponse(IReadOnlyList<DomainEvent> Events) : CommandResponse(Events);

public override async Task ExecuteAsync(BidRequest request)
public class BidUsecase(IRepository repository)
: Usecase<BidRequest, BidResponse>(repository)
{
public override async Task ExecuteAsync(BidRequest request, IPresenter<BidResponse> presenter)
{
//查
var game = Repository.FindGameById(request.GameId).ToDomain();
Expand All @@ -25,6 +23,6 @@ public override async Task ExecuteAsync(BidRequest request)
Repository.Save(game);

//推
await EventBus.PublishAsync(game.DomainEvents);
await presenter.PresentAsync(new BidResponse(game.DomainEvents));
}
}
14 changes: 6 additions & 8 deletions Application/Usecases/BuildHouseUsecase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,12 @@ namespace Application.Usecases;
public record BuildHouseRequest(string GameId, string PlayerId)
: Request(GameId, PlayerId);

public class BuildHouseUsecase : Usecase<BuildHouseRequest>
{
public BuildHouseUsecase(IRepository repository, IEventBus<DomainEvent> eventBus)
: base(repository, eventBus)
{
}
public record BuildHouseResponse(IReadOnlyList<DomainEvent> Events) : CommandResponse(Events);

public override async Task ExecuteAsync(BuildHouseRequest request)
public class BuildHouseUsecase(IRepository repository)
: Usecase<BuildHouseRequest, BuildHouseResponse>(repository)
{
public override async Task ExecuteAsync(BuildHouseRequest request, IPresenter<BuildHouseResponse> presenter)
{
//查
var game = Repository.FindGameById(request.GameId).ToDomain();
Expand All @@ -25,6 +23,6 @@ public override async Task ExecuteAsync(BuildHouseRequest request)
Repository.Save(game);

//推
await EventBus.PublishAsync(game.DomainEvents);
await presenter.PresentAsync(new BuildHouseResponse(game.DomainEvents));
}
}
30 changes: 0 additions & 30 deletions Application/Usecases/BuyBlockUsecase.cs

This file was deleted.

14 changes: 6 additions & 8 deletions Application/Usecases/ChooseDirectionUsecase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,12 @@ namespace Application.Usecases;
public record ChooseDirectionRequest(string GameId, string PlayerId, string Direction)
: Request(GameId, PlayerId);

public class ChooseDirectionUsecase : Usecase<ChooseDirectionRequest>
{
public ChooseDirectionUsecase(IRepository repository, IEventBus<DomainEvent> eventBus)
: base(repository, eventBus)
{
}
public record ChooseDirectionResponse(IReadOnlyList<DomainEvent> Events) : CommandResponse(Events);

public override async Task ExecuteAsync(ChooseDirectionRequest request)
public class ChooseDirectionUsecase(IRepository repository)
: Usecase<ChooseDirectionRequest, ChooseDirectionResponse>(repository)
{
public override async Task ExecuteAsync(ChooseDirectionRequest request, IPresenter<ChooseDirectionResponse> presenter)
{
//查
var game = Repository.FindGameById(request.GameId).ToDomain();
Expand All @@ -22,6 +20,6 @@ public override async Task ExecuteAsync(ChooseDirectionRequest request)
//存
Repository.Save(game);
//推
await EventBus.PublishAsync(game.DomainEvents);
await presenter.PresentAsync(new ChooseDirectionResponse(game.DomainEvents));
}
}
23 changes: 8 additions & 15 deletions Application/Usecases/CreateGameUsecase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,12 @@ namespace Application.Usecases;

public record CreateGameRequest(string HostId, string[] PlayerIds) : Request(null!, HostId);

public class CreateGameUsecase : Usecase<CreateGameRequest>
{
public CreateGameUsecase(IRepository repository, IEventBus<DomainEvent> eventBus) : base(repository, eventBus)
{
}
public record CreateGameResponse(string GameId) : Response;

#pragma warning disable CS1998 // Async 方法缺乏 'await' 運算子,將同步執行
public override async Task ExecuteAsync(CreateGameRequest request)
#pragma warning restore CS1998 // Async 方法缺乏 'await' 運算子,將同步執行
{
throw new NotImplementedException();
}

public string Execute(CreateGameRequest request)
public class CreateGameUsecase(IRepository repository)
: Usecase<CreateGameRequest, CreateGameResponse>(repository)
{
public override async Task ExecuteAsync(CreateGameRequest request, IPresenter<CreateGameResponse> presenter)
{
// 查
// 改
Expand All @@ -35,8 +27,9 @@ public string Execute(CreateGameRequest request)
builder.WithMap(new SevenXSevenMap());

// 存
string id = Repository.Save(builder.Build());
var id = Repository.Save(builder.Build());

return id;
// 推
await presenter.PresentAsync(new CreateGameResponse(id));
}
}
14 changes: 6 additions & 8 deletions Application/Usecases/EndAuctionUsecase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,12 @@ namespace Application.Usecases;
public record EndAuctionRequest(string GameId, string PlayerId)
: Request(GameId, PlayerId);

public class EndAuctionUsecase : Usecase<EndAuctionRequest>
{
public EndAuctionUsecase(IRepository repository, IEventBus<DomainEvent> eventBus)
: base(repository, eventBus)
{
}
public record EndAuctionResponse(IReadOnlyList<DomainEvent> Events) : CommandResponse(Events);

public override async Task ExecuteAsync(EndAuctionRequest request)
public class EndAuctionUsecase(IRepository repository)
: Usecase<EndAuctionRequest, EndAuctionResponse>(repository)
{
public override async Task ExecuteAsync(EndAuctionRequest request, IPresenter<EndAuctionResponse> presenter)
{
//查
var game = Repository.FindGameById(request.GameId).ToDomain();
Expand All @@ -25,6 +23,6 @@ public override async Task ExecuteAsync(EndAuctionRequest request)
Repository.Save(game);

//推
await EventBus.PublishAsync(game.DomainEvents);
await presenter.PresentAsync(new EndAuctionResponse(game.DomainEvents));
}
}
14 changes: 6 additions & 8 deletions Application/Usecases/EndRoundUsecase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,12 @@ namespace Application.Usecases;
public record EndRoundRequest(string GameId, string PlayerId)
: Request(GameId, PlayerId);

public class EndRoundUsecase : Usecase<EndRoundRequest>
{
public EndRoundUsecase(IRepository repository, IEventBus<DomainEvent> eventBus)
: base(repository, eventBus)
{
}
public record EndRoundResponse(IReadOnlyList<DomainEvent> Events) : CommandResponse(Events);

public override async Task ExecuteAsync(EndRoundRequest request)
public class EndRoundUsecase(IRepository repository)
: Usecase<EndRoundRequest, EndRoundResponse>(repository)
{
public override async Task ExecuteAsync(EndRoundRequest request, IPresenter<EndRoundResponse> presenter)
{
//查
var game = Repository.FindGameById(request.GameId).ToDomain();
Expand All @@ -25,6 +23,6 @@ public override async Task ExecuteAsync(EndRoundRequest request)
Repository.Save(game);

//推
await EventBus.PublishAsync(game.DomainEvents);
await presenter.PresentAsync(new EndRoundResponse(game.DomainEvents));
}
}
14 changes: 6 additions & 8 deletions Application/Usecases/GameStartUsecase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,12 @@ namespace Application.Usecases;
public record GameStartRequest(string GameId, string PlayerId)
: Request(GameId, PlayerId);

public class GameStartUsecase : Usecase<GameStartRequest>
{
public GameStartUsecase(IRepository repository, IEventBus<DomainEvent> eventBus)
: base(repository, eventBus)
{
}
public record GameStartResponse(IReadOnlyList<DomainEvent> Events) : CommandResponse(Events);

public override async Task ExecuteAsync(GameStartRequest request)
public class GameStartUsecase(IRepository repository)
: Usecase<GameStartRequest, GameStartResponse>(repository)
{
public override async Task ExecuteAsync(GameStartRequest request, IPresenter<GameStartResponse> presenter)
{
//查
var game = Repository.FindGameById(request.GameId).ToDomain();
Expand All @@ -25,6 +23,6 @@ public override async Task ExecuteAsync(GameStartRequest request)
Repository.Save(game);

//推
await EventBus.PublishAsync(game.DomainEvents);
await presenter.PresentAsync(new GameStartResponse(game.DomainEvents));
}
}
14 changes: 6 additions & 8 deletions Application/Usecases/MortgageUsecase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,12 @@ namespace Application.Usecases;
public record MortgageRequest(string GameId, string PlayerId, string BlockId)
: Request(GameId, PlayerId);

public class MortgageUsecase : Usecase<MortgageRequest>
{
public MortgageUsecase(IRepository repository, IEventBus<DomainEvent> eventBus)
: base(repository, eventBus)
{
}
public record MortgageResponse(IReadOnlyList<DomainEvent> Events) : CommandResponse(Events);

public override async Task ExecuteAsync(MortgageRequest request)
public class MortgageUsecase(IRepository repository)
: Usecase<MortgageRequest, MortgageResponse>(repository)
{
public override async Task ExecuteAsync(MortgageRequest request, IPresenter<MortgageResponse> presenter)
{
//查
var game = Repository.FindGameById(request.GameId).ToDomain();
Expand All @@ -25,6 +23,6 @@ public override async Task ExecuteAsync(MortgageRequest request)
Repository.Save(game);

//推
await EventBus.PublishAsync(game.DomainEvents);
await presenter.PresentAsync(new MortgageResponse(game.DomainEvents));
}
}
Loading

0 comments on commit e844226

Please sign in to comment.