플러터에서 repository 레이어의 데이터 통신 부분을 설계하는 중이다. 나는 retrofit라이브러리를 쓰고 있어서 @RestApi() abstract…
- User
플러터에서 repository 레이어의 데이터 통신 부분을 설계하는 중이다.
나는 retrofit라이브러리를 쓰고 있어서
@RestApi() abstract class NoticeRepository { factory NoticeRepository(Dio dio) = _NoticeRepository;
@GET('/notices') Future<ApiResponse> getNoticeList(); }
기본 호출은 이런식으로 깔끔하게 구현 가능하다. ApiResponse은 서버에서 내려주는 기본 result 양식을 일반화한 클래스이다.
문제는 이 다음이다. 나는 여기서 한번 더 데이터를 가공하여 ApiResult.success, ApiResult.error 형태로 반환하고 dto클래스는 model 클래스로 변환해서 UI 레이어단에서 바로 사용할 수 있도록 하려고 한다.
이것을 수행하는 클래스의 이름은 무엇으로 하면 좋을까? Repository라는 이름이 적절하다면 기존의 retrofit의 이름을 변경해도 좋다. NoticeRepository 가 아니라 NoticeRetrofit 이런식으로 바꾼다던지
Assistant
플러터에서 리포지토리 레이어의 데이터 통신 부분을 설계할 때 명확한 책임 분담과 명명 규칙의 일관성이 중요합니다. 아래와 같은 접근 방식을 추천합니다:
1. 데이터 소스와 리포지토리의 분리
- Retrofit 기반의 데이터 소스 (예:
NoticeRetrofit)는 네트워크 호출을 담당합니다.
- Retrofit 기반의 데이터 소스 (예:
- User
아래는 GPT가 제안한 클래스명인데 지금 상황에서 impl과 repository를 분리하는게 괜찮은 선택인것같은지 의견을 들려줘라.
// Retrofit 직접 통신 @RestApi() abstract class NoticeApi { factory NoticeApi(Dio dio) = _NoticeApi;
@GET('/notices') Future<ApiResponse> getNoticeList(); }
// Repository (비즈니스 가공 담당) abstract class NoticeRepository { Future<ApiResult<List>> getNoticeList(); }
class NoticeRepositoryImpl implements NoticeRepository { final NoticeApi api;
NoticeRepositoryImpl(this.api);
@override Future<ApiResult<List>> getNoticeList() async { try { final response = await api.getNoticeList(); return ApiResult.success(response.data.toModelList()); } catch (e) { return ApiResult.error(e.toString()); } } }
Assistant
네, NoticeApi와 NoticeRepository를 분리하는 것은 매우 적절한 선택입니다. 이 구조는 **단일 책임 원칙(SRP)**과 의존성 분리를 철저히 따르며, 코드의 가독성과 유지보수성을 크게 향상시킵니다. 아래에 구체적인 이유와 장점을 설명드리겠습니다.
🔍 1. 명확한 책임 분담
-
NoticeApi:
Retrofit을 사용한 네트워크 호출을 담당합니다. 단순히 서버에 요청을 보내고 응답을 받는 역할만 수행합니다.
-
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.