Skip to content

Commit

Permalink
refactor: 把推播改回 eventbus, 並將大部分使用 presenter 的地方刪除
Browse files Browse the repository at this point in the history
1. 由於不希望在presenter這種展示資料的地方偷偷進行推播,因此改回用eventbus推播的方式
2. 由於多數usecase不需要presenter回傳資訊,因此將usecase的參數增加一種無需回傳的形式
  • Loading branch information
aa89227 committed Jul 14, 2024
1 parent d1ed6e1 commit 508c8d8
Show file tree
Hide file tree
Showing 26 changed files with 115 additions and 219 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,9 @@ public abstract class Usecase<TRequest, TResponse> where TRequest : BaseRequest
{
public abstract Task ExecuteAsync(TRequest request, IPresenter<TResponse> presenter,
CancellationToken cancellationToken = default);
}

public abstract class Usecase<TRequest> where TRequest : BaseRequest
{
public abstract Task ExecuteAsync(TRequest request, CancellationToken cancellationToken = default);
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@ private static IServiceCollection AddUseCases(this IServiceCollection services)
{
var assembly = typeof(DependencyInjection).Assembly;
var usecaseType = typeof(Usecase<,>);
var usecaseType2 = typeof(Usecase<>);

var types = assembly.GetTypes()
.Where(t => t is { IsAbstract: false, BaseType.IsGenericType: true})
.Where(t => t.BaseType?.GetGenericTypeDefinition() == usecaseType);
.Where(t => t.BaseType?.GetGenericTypeDefinition() == usecaseType || t.BaseType?.GetGenericTypeDefinition() == usecaseType2);
foreach (var type in types)
{
services.AddTransient(type);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,10 @@ namespace Monopoly.ApplicationLayer.Application.MonopolyUsecases.Commands;
public record BidRequest(string GameId, string PlayerId, decimal BidPrice)
: GameRequest(GameId, PlayerId);

public record BidResponse(IReadOnlyList<DomainEvent> Events) : CommandResponse(Events);

public class BidUsecase(IRepository<MonopolyAggregate> repository, IEventBus<DomainEvent> eventBus)
: Usecase<BidRequest, BidResponse>
: Usecase<BidRequest>
{
public override async Task ExecuteAsync(BidRequest request, IPresenter<BidResponse> presenter,
public override async Task ExecuteAsync(BidRequest request,
CancellationToken cancellationToken = default)
{
//查
Expand All @@ -25,6 +23,6 @@ public override async Task ExecuteAsync(BidRequest request, IPresenter<BidRespon
await repository.SaveAsync(game);

//推
await presenter.PresentAsync(new BidResponse(game.DomainEvents), cancellationToken);
await eventBus.PublishAsync(game.DomainEvents, cancellationToken);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,10 @@ namespace Monopoly.ApplicationLayer.Application.MonopolyUsecases.Commands;
public record BuildHouseRequest(string GameId, string PlayerId)
: GameRequest(GameId, PlayerId);

public record BuildHouseResponse(IReadOnlyList<DomainEvent> Events) : CommandResponse(Events);

public class BuildHouseUsecase(IRepository<MonopolyAggregate> repository, IEventBus<DomainEvent> eventBus)
: Usecase<BuildHouseRequest, BuildHouseResponse>
: Usecase<BuildHouseRequest>
{
public override async Task ExecuteAsync(BuildHouseRequest request, IPresenter<BuildHouseResponse> presenter,
CancellationToken cancellationToken = default)
public override async Task ExecuteAsync(BuildHouseRequest request, CancellationToken cancellationToken = default)
{
//查
var game = await repository.FindByIdAsync(request.GameId);
Expand All @@ -25,6 +22,6 @@ public override async Task ExecuteAsync(BuildHouseRequest request, IPresenter<Bu
await repository.SaveAsync(game);

//推
await presenter.PresentAsync(new BuildHouseResponse(game.DomainEvents), cancellationToken);
await eventBus.PublishAsync(game.DomainEvents, cancellationToken);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,11 @@ namespace Monopoly.ApplicationLayer.Application.MonopolyUsecases.Commands;
public record ChooseDirectionRequest(string GameId, string PlayerId, string Direction)
: GameRequest(GameId, PlayerId);

public record ChooseDirectionResponse(IReadOnlyList<DomainEvent> Events) : CommandResponse(Events);

public class ChooseDirectionUsecase(IRepository<MonopolyAggregate> repository, IEventBus<DomainEvent> eventBus)
: Usecase<ChooseDirectionRequest, ChooseDirectionResponse>
: Usecase<ChooseDirectionRequest>
{
public override async Task ExecuteAsync(ChooseDirectionRequest request,
IPresenter<ChooseDirectionResponse> presenter, CancellationToken cancellationToken = default)
public override async Task ExecuteAsync(ChooseDirectionRequest request
, CancellationToken cancellationToken = default)
{
//查
var game = await repository.FindByIdAsync(request.GameId);
Expand All @@ -25,6 +23,6 @@ public override async Task ExecuteAsync(ChooseDirectionRequest request,
await repository.SaveAsync(game);

//推
await presenter.PresentAsync(new ChooseDirectionResponse(game.DomainEvents), cancellationToken);
await eventBus.PublishAsync(game.DomainEvents, cancellationToken);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,10 @@ namespace Monopoly.ApplicationLayer.Application.MonopolyUsecases.Commands;
public record EndAuctionRequest(string GameId, string PlayerId)
: GameRequest(GameId, PlayerId);

public record EndAuctionResponse(IReadOnlyList<DomainEvent> Events) : CommandResponse(Events);

public class EndAuctionUsecase(IRepository<MonopolyAggregate> repository, IEventBus<DomainEvent> eventBus)
: Usecase<EndAuctionRequest, EndAuctionResponse>
: Usecase<EndAuctionRequest>
{
public override async Task ExecuteAsync(EndAuctionRequest request, IPresenter<EndAuctionResponse> presenter,
public override async Task ExecuteAsync(EndAuctionRequest request,
CancellationToken cancellationToken = default)
{
//查
Expand All @@ -25,6 +23,6 @@ public override async Task ExecuteAsync(EndAuctionRequest request, IPresenter<En
await repository.SaveAsync(game);

//推
await presenter.PresentAsync(new EndAuctionResponse(game.DomainEvents), cancellationToken);
await eventBus.PublishAsync(game.DomainEvents, cancellationToken);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,11 @@ namespace Monopoly.ApplicationLayer.Application.MonopolyUsecases.Commands;
public record EndRoundRequest(string GameId, string PlayerId)
: GameRequest(GameId, PlayerId);

public record EndRoundResponse(IReadOnlyList<DomainEvent> Events) : CommandResponse(Events);

public class EndRoundUsecase(IRepository<MonopolyAggregate> repository, IEventBus<DomainEvent> eventBus)
: Usecase<EndRoundRequest, EndRoundResponse>
: Usecase<EndRoundRequest>
{
public override async Task ExecuteAsync(EndRoundRequest request, IPresenter<EndRoundResponse> presenter,
public override async Task ExecuteAsync(EndRoundRequest request,
CancellationToken cancellationToken = default)
{
//查
Expand All @@ -25,6 +24,6 @@ public override async Task ExecuteAsync(EndRoundRequest request, IPresenter<EndR
await repository.SaveAsync(game);

//推
await presenter.PresentAsync(new EndRoundResponse(game.DomainEvents), cancellationToken);
await eventBus.PublishAsync(game.DomainEvents, cancellationToken);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,10 @@ namespace Monopoly.ApplicationLayer.Application.MonopolyUsecases.Commands;
public record MortgageRequest(string GameId, string PlayerId, string BlockId)
: GameRequest(GameId, PlayerId);

public record MortgageResponse(IReadOnlyList<DomainEvent> Events) : CommandResponse(Events);

public class MortgageUsecase(IRepository<MonopolyAggregate> repository, IEventBus<DomainEvent> eventBus)
: Usecase<MortgageRequest, MortgageResponse>
: Usecase<MortgageRequest>
{
public override async Task ExecuteAsync(MortgageRequest request, IPresenter<MortgageResponse> presenter,
public override async Task ExecuteAsync(MortgageRequest request,
CancellationToken cancellationToken = default)
{
//查
Expand All @@ -25,6 +23,6 @@ public override async Task ExecuteAsync(MortgageRequest request, IPresenter<Mort
await repository.SaveAsync(game);

//推
await presenter.PresentAsync(new MortgageResponse(game.DomainEvents), cancellationToken);
await eventBus.PublishAsync(game.DomainEvents, cancellationToken);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,11 @@ namespace Monopoly.ApplicationLayer.Application.MonopolyUsecases.Commands;
public record PayTollRequest(string GameId, string PlayerId)
: GameRequest(GameId, PlayerId);

public record PayTollResponse(IReadOnlyList<DomainEvent> Events) : CommandResponse(Events);

public class PayTollUsecase(IRepository<MonopolyAggregate> repository, IEventBus<DomainEvent> eventBus)
: Usecase<PayTollRequest, PayTollResponse>
: Usecase<PayTollRequest>
{
public override async Task ExecuteAsync(PayTollRequest request, IPresenter<PayTollResponse> presenter,
public override async Task ExecuteAsync(PayTollRequest request,
CancellationToken cancellationToken = default)
{
//查
Expand All @@ -25,6 +24,6 @@ public override async Task ExecuteAsync(PayTollRequest request, IPresenter<PayTo
await repository.SaveAsync(game);

//推
await presenter.PresentAsync(new PayTollResponse(game.DomainEvents), cancellationToken);
await eventBus.PublishAsync(game.DomainEvents, cancellationToken);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,10 @@ namespace Monopoly.ApplicationLayer.Application.MonopolyUsecases.Commands;
public record PlayerBuyLandRequest(string GameId, string PlayerId, string LandID)
: GameRequest(GameId, PlayerId);

public record PlayerBuyLandResponse(IReadOnlyList<DomainEvent> Events) : CommandResponse(Events);

public class PlayerBuyLandUsecase(IRepository<MonopolyAggregate> repository, IEventBus<DomainEvent> eventBus)
: Usecase<PlayerBuyLandRequest, PlayerBuyLandResponse>
: Usecase<PlayerBuyLandRequest>
{
public override async Task ExecuteAsync(PlayerBuyLandRequest request, IPresenter<PlayerBuyLandResponse> presenter,
public override async Task ExecuteAsync(PlayerBuyLandRequest request,
CancellationToken cancellationToken = default)
{
//查
Expand All @@ -25,6 +23,6 @@ public override async Task ExecuteAsync(PlayerBuyLandRequest request, IPresenter
await repository.SaveAsync(game);

//推
await presenter.PresentAsync(new PlayerBuyLandResponse(game.DomainEvents), cancellationToken);
await eventBus.PublishAsync(game.DomainEvents, cancellationToken);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,11 @@ namespace Monopoly.ApplicationLayer.Application.MonopolyUsecases.Commands;
public record PlayerRollDiceRequest(string GameId, string PlayerId)
: GameRequest(GameId, PlayerId);

public record PlayerRollDiceResponse(IReadOnlyList<DomainEvent> Events) : CommandResponse(Events);

public class PlayerRollDiceUsecase(IRepository<MonopolyAggregate> repository, IEventBus<DomainEvent> eventBus)
: Usecase<PlayerRollDiceRequest, PlayerRollDiceResponse>
: Usecase<PlayerRollDiceRequest>
{
public override async Task ExecuteAsync(PlayerRollDiceRequest request,
IPresenter<PlayerRollDiceResponse> presenter, CancellationToken cancellationToken = default)
CancellationToken cancellationToken = default)
{
//查
var game = await repository.FindByIdAsync(request.GameId);
Expand All @@ -25,6 +23,6 @@ public override async Task ExecuteAsync(PlayerRollDiceRequest request,
await repository.SaveAsync(game);

//推
await presenter.PresentAsync(new PlayerRollDiceResponse(game.DomainEvents), cancellationToken);
await eventBus.PublishAsync(game.DomainEvents, cancellationToken);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,10 @@ namespace Monopoly.ApplicationLayer.Application.MonopolyUsecases.Commands;
public record RedeemRequest(string GameId, string PlayerId, string BlockId)
: GameRequest(GameId, PlayerId);

public record RedeemResponse(IReadOnlyList<DomainEvent> Events) : CommandResponse(Events);

public class RedeemUsecase(IRepository<MonopolyAggregate> repository, IEventBus<DomainEvent> eventBus)
: Usecase<RedeemRequest, RedeemResponse>
: Usecase<RedeemRequest>
{
public override async Task ExecuteAsync(RedeemRequest request, IPresenter<RedeemResponse> presenter,
public override async Task ExecuteAsync(RedeemRequest request,
CancellationToken cancellationToken = default)
{
//查
Expand All @@ -25,6 +23,6 @@ public override async Task ExecuteAsync(RedeemRequest request, IPresenter<Redeem
await repository.SaveAsync(game);

//推
await presenter.PresentAsync(new RedeemResponse(game.DomainEvents), cancellationToken);
await eventBus.PublishAsync(game.DomainEvents, cancellationToken);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,11 @@ namespace Monopoly.ApplicationLayer.Application.MonopolyUsecases.Commands;
public record SettlementRequest(string GameId, string PlayerId)
: GameRequest(GameId, PlayerId);

public record SettlementResponse(IReadOnlyList<DomainEvent> Events) : CommandResponse(Events);

public class SettlementUsecase(IRepository<MonopolyAggregate> repository, IEventBus<DomainEvent> eventBus)
: Usecase<SettlementRequest, SettlementResponse>
: Usecase<SettlementRequest>
{
public override async Task ExecuteAsync(SettlementRequest request, IPresenter<SettlementResponse> presenter,
public override async Task ExecuteAsync(SettlementRequest request,
CancellationToken cancellationToken = default)
{
//查
Expand All @@ -25,6 +24,6 @@ public override async Task ExecuteAsync(SettlementRequest request, IPresenter<Se
await repository.SaveAsync(game);

//推
await presenter.PresentAsync(new SettlementResponse(game.DomainEvents), cancellationToken);
await eventBus.PublishAsync(game.DomainEvents, cancellationToken);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,21 @@ namespace Monopoly.ApplicationLayer.Application.ReadyRoomUsecases.Commands;

public record PlayerJoinReadyRoomRequest(string RoomId, string PlayerId) : BaseRequest;

public record PlayerJoinReadyRoomResponse : Response;

public class PlayerJoinReadyRoomUsecase(IRepository<ReadyRoomAggregate> repository, IEventBus<DomainEvent> eventBus)
: Usecase<PlayerJoinReadyRoomRequest, PlayerJoinReadyRoomResponse>
: Usecase<PlayerJoinReadyRoomRequest>
{
public override async Task ExecuteAsync(PlayerJoinReadyRoomRequest request,
IPresenter<PlayerJoinReadyRoomResponse> presenter,
CancellationToken cancellationToken = default)
{
// 查
var readyRoom = await repository.FindByIdAsync(request.RoomId);

// 改
readyRoom.PlayerJoin(request.PlayerId);

// 存
await repository.SaveAsync(readyRoom);

// 推
await eventBus.PublishAsync(readyRoom.DomainEvents, cancellationToken);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,10 @@ namespace Monopoly.ApplicationLayer.Application.ReadyRoomUsecases.Commands;
public record PlayerReadyRequest(string GameId, string PlayerId)
: GameRequest(GameId, PlayerId);

public record PlayerReadyResponse(IReadOnlyList<DomainEvent> Events) : CommandResponse(Events);

public class PlayerReadyUsecase(IRepository<ReadyRoomAggregate> repository, IEventBus<DomainEvent> eventBus)
: Usecase<PlayerReadyRequest, PlayerReadyResponse>
: Usecase<PlayerReadyRequest>
{
public override async Task ExecuteAsync(PlayerReadyRequest request, IPresenter<PlayerReadyResponse> presenter,
CancellationToken cancellationToken = default)
public override async Task ExecuteAsync(PlayerReadyRequest request, CancellationToken cancellationToken = default)
{
//查
var readyRoom = await repository.FindByIdAsync(request.GameId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,10 @@ namespace Monopoly.ApplicationLayer.Application.ReadyRoomUsecases.Commands;
public record SelectRoleRequest(string GameId, string PlayerId, string Role)
: GameRequest(GameId, PlayerId);

public record SelectRoleResponse(IReadOnlyList<DomainEvent> Events) : CommandResponse(Events);

public class SelectRoleUsecase(IRepository<ReadyRoomAggregate> repository, IEventBus<DomainEvent> eventBus)
: Usecase<SelectRoleRequest, SelectRoleResponse>
: Usecase<SelectRoleRequest>
{
public override async Task ExecuteAsync(SelectRoleRequest request, IPresenter<SelectRoleResponse> presenter,
CancellationToken cancellationToken = default)
public override async Task ExecuteAsync(SelectRoleRequest request, CancellationToken cancellationToken = default)
{
//查
var readyRoom = await repository.FindByIdAsync(request.GameId);
Expand Down
Loading

0 comments on commit 508c8d8

Please sign in to comment.