我有一個回傳 void 的函式
public interface IProductService {
void delete(String id);
}
通用方法
public interface IRequestHandler<C , R> {
R handler(C c);
Class<C> commandType();
}
通用介面的實作
@Singleton
public record DeleteProductCommandHandler(IProductService iProductService)
implements IRequestHandler<DeleteProductCommand, Void> {
@Override
public Void handler(DeleteProductCommand deleteProductCommand) {
return iProductService.delete(deleteProductCommand.id);
}
@Override
public Class<DeleteProductCommand> commandType() {
return DeleteProductCommand.class;
}
}
我如何使用 void inIRequestHandler<DeleteProductCommand, Void>以便我可以從中映射 voidiProductService.delete(deleteProductCommand.id);
uj5u.com熱心網友回復:
選項1:
只需回傳null:
@Override
public Void handler(DeleteProductCommand deleteProductCommand) {
iProductService.delete(deleteProductCommand.id);
return null;
}
選項 2:
更新IProductService::delete方法以回傳一些有意義的東西,例如boolean像Collection::remove這樣的值:
public interface IProductService {
boolean delete(String id);
}
@Singleton
public record DeleteProductCommandHandler(IProductService iProductService)
implements IRequestHandler<DeleteProductCommand, Boolean> {
@Override
public Boolean handler(DeleteProductCommand deleteProductCommand) {
return iProductService.delete(deleteProductCommand.id);
}
@Override
public Class<DeleteProductCommand> commandType() {
return DeleteProductCommand.class;
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/401140.html
