Created
June 6, 2025 08:29
-
-
Save acious/98bc58d92ec4f6b777002c18b228aaee to your computer and use it in GitHub Desktop.
C.C - Data Layer
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // file: data/UserDto.kt | |
| import com.google.gson.annotations.SerializedName | |
| /** | |
| * 데이터 전송 객체 (Data Transfer Object). | |
| * API 응답과 1:1로 매핑되는 모델입니다. 필드명이 다를 수 있습니다. | |
| */ | |
| data class UserDto( | |
| @SerializedName("user_id") val userId: String, | |
| @SerializedName("full_name") val fullName: String, | |
| @SerializedName("contact_email") val email: String?, | |
| @SerializedName("year_of_birth") val birthYear: Int | |
| ) | |
| /** | |
| * Mapper 확장 함수: DTO를 순수한 Domain Entity로 변환합니다. | |
| * 데이터 계층의 책임은 도메인 계층이 이해할 수 있는 형태로 데이터를 가공하여 전달하는 것입니다. | |
| */ | |
| fun UserDto.toDomain(): User { | |
| return User( | |
| id = this.userId, | |
| name = this.fullName, | |
| email = this.email ?: "이메일 정보 없음", // Null 처리 등 데이터 가공 | |
| birthYear = this.birthYear | |
| ) | |
| } | |
| // file: data/UserRepositoryImpl.kt | |
| /** | |
| * Domain 계층의 UserRepository 인터페이스에 대한 실제 구현체. | |
| * 여기서는 Retrofit 같은 API 서비스를 사용한다고 가정합니다. | |
| */ | |
| class UserRepositoryImpl(private val apiService: ApiService) : UserRepository { | |
| override suspend fun getUser(id: String): Result<User> { | |
| return try { | |
| val userDto = apiService.fetchUser(id) | |
| Result.success(userDto.toDomain()) // DTO를 Entity로 변환하여 반환 | |
| } catch (e: Exception) { | |
| Result.failure(e) | |
| } | |
| } | |
| } | |
| // ApiService는 Retrofit 인터페이스라고 가정합니다. | |
| interface ApiService { | |
| @GET("users/{id}") | |
| suspend fun fetchUser(@Path("id") id: String): UserDto | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment