Cześć, mam problem z rozwiązaniem pewnego dylematu. Ogólnie powiedzmy, że podczas dodania pliku trzeba go wysłać do jakiegoś tam storage np. s3. Czy agregat "Comment" powinien zawierać serwis, który wysyła plik w odpowiednie miejsce, czy jednak jest to po stronie serwisu?
Przykładowy kod:
static class Storage {
public void save(UUID id, InputStream file) {
}
}
static class Comment {
private UUID id;
private String title;
private List<File> files;
//Transient
private final Storage storage;
void addFile(FileRequest file) {
UUID id = UUID.randomUUID();
files.add(new File(id, file.name));
storage.save(id, file.fileContent);
}
}
@AllArgsConstructor(access = AccessLevel.PROTECTED)
static class File {
private UUID id;
private String name;
}
interface CommentRepository {
void save(Comment comment);
Comment findById(UUID id);
}
record FileRequest(
InputStream fileContent,
String name
) {
}
@RequiredArgsConstructor
static class CommentService {
private final CommentRepository commentRepository;
public void addFileToComment(UUID commentId, FileRequest request) {
Comment comment = commentRepository.findById(commentId);
comment.addFile(request);
commentRepository.save(comment);
}
}
vs
static class Storage {
public void save(UUID id, InputStream file) {
}
}
static class Comment {
private UUID id;
private String title;
private List<File> files;
public void addFile(UUID fileId, String name) {
files.add(new File(fileId, name));
}
}
@AllArgsConstructor(access = AccessLevel.PROTECTED)
static class File {
private UUID id;
private String name;
}
interface CommentRepository {
void save(Comment comment);
Comment findById(UUID id);
}
record FileRequest(
InputStream fileContent,
String name
) {
}
@RequiredArgsConstructor
static class CommentService {
private final CommentRepository commentRepository;
private final Storage storage;
public void addFileToComment(UUID commentId, FileRequest request) {
Comment comment = commentRepository.findById(commentId);
UUID fileId = UUID.randomUUID();
storage.save(fileId, request.fileContent);
comment.addFile(fileId, request.name());
commentRepository.save(comment);
}
}
A może powinien on przyjąć tę usługę jako parametr metody?
static class Storage {
public void save(UUID id, InputStream file) {
}
}
static class Comment {
private UUID id;
private String title;
private List<File> files;
void addFile(FileRequest file, Storage storage) {
UUID id = UUID.randomUUID();
files.add(new File(id, file.name));
storage.save(id, file.fileContent);
}
}
@AllArgsConstructor(access = AccessLevel.PROTECTED)
static class File {
private UUID id;
private String name;
}
interface CommentRepository {
void save(Comment comment);
Comment findById(UUID id);
}
record FileRequest(
InputStream fileContent,
String name
) {
}
@RequiredArgsConstructor
static class CommentService {
private final CommentRepository commentRepository;
private final Storage storage;
public void addFileToComment(UUID commentId, FileRequest request) {
Comment comment = commentRepository.findById(commentId);
comment.addFile(request, storage);
commentRepository.save(comment);
}
}