-
Notifications
You must be signed in to change notification settings - Fork 16
[김이준] Sprint2 #18
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[김이준] Sprint2 #18
Conversation
- File IO를 통한 데이터 영속화 - 서비스 구현체 분석 - 레포지토리 설계 및 구현
import java.util.UUID; | ||
|
||
public class FileUserRepository implements UserRepository { | ||
private static final String FILE_PATH = "data/users.ser"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
data 디렉토리와 user.ser 파일에 대한 초기화는 필수 입니다.
존재여부 확인하고, 존재하지 않는다면, 새롭게 생성할 수 있도록 구현해야 합니다.
|
||
@Override | ||
public void saveAll(List<User> users) { | ||
try { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
이런식으로 try-catch문을 사용 했다면,
필수적으로 finally문을 선언하고 FileIO와 관련된 리소스를 반환해주셔야 합니다.
fos.close
oos.close; 와 같은 코드의 형태로 말이죠.
그렇지 않다면, 리소스 부족으로 강제로 Applicaiton이 종료 될 수 있습니다.
boolean updated = false; | ||
|
||
for (int i = 0; i < users.size(); i++) { | ||
if (users.get(i).getId().equals(user.getId())) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
users.stream.filter
방식으로 구현했다면, 코드의 가독성을 더 극대화 시킬 수 있습니다.
참고하셔서 다시 한번 고쳐보시기를 권고 드립니다.
FileInputStream fis = new FileInputStream(FILE_PATH); | ||
ObjectInputStream ois = new ObjectInputStream(fis); | ||
messages = (List<Message>) ois.readObject(); | ||
} catch (IOException | ClassNotFoundException e) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
RuntimeException으로 던지기 보다는 예외 처리가 발생한 Exception으로 던져주는 것이 더 많은 정보를 전달 할 수 있습니다.
그래서 아래와 같은 코드의 형태로 오류 처리하는 것을 추천드립니다.
catch (IOException e){
trow new IOException();
} catch (ClassNotFoundException e) {
trow new ClassNotFoundException();
}
} | ||
|
||
@Override | ||
public void updateUser(UUID id, User partialUser) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
이 코드는 아래와 같이 람다를 사용하면 좀 더 깔끔한 코드로 변경될 수 있습니다.
private <T> void applyIfNotNull(T value, Consumer<T> setter) {
if (value != null) setter.accept(value);
}
@Override
public void updateUser(UUID id, User partialUser) {
User user = findVerifiedUser(id);
applyIfNotNull(partialUser.getUserName(), user::setUserName);
applyIfNotNull(partialUser.getEmail(), user::setEmail);
applyIfNotNull(partialUser.getPassword(), user::setPassword);
applyIfNotNull(partialUser.getUserStatus(), user::setUserStatus);
user.updateTimeStamp();
userRepository.save(user);
}
…int2 [김이준] Sprint2
기본 요구사항
기본요구사항
File IO를 통한 데이터 영속화
서비스 구현체 분석
레포지토리 설계 및 구현
심화 요구 사항
관심사 분리를 통한 레이어 간 의존성 주입
변경사항
멘토에게