2022.01.17 TIL

김민수·2022년 1월 19일
0

repository pattern

repository 패턴은 비즈니스 로직과 데이터 로직을 분리하며 발생하는 요구사항을 만족하기 위해 만들어 졌다.

  • 비즈니스 로직과 데이터의 분리
  • 일관된 데이터와 로직 제공
  • 중앙 집중 처리

데이터 소스와 비즈니스 로직 사이에서 데이터를 요청하고 결과 값을 알맞은 객체로 변환하여 비즈니스 로직에게 제공한다. 따라서 비즈니스 로직은 repository가 사용하는 데이터 소스가 무엇이든 상관없이 그저 결과만 받아 비즈니스 로직 자체에 집중할 수 있다.

repository가 추상화 되어있어 일관된 인터페이스를 통해 데이터를 요청할 수 있다. 따라서 어떤 환경에서도 같은 결과 반환을 기대할 수 있다.

장점

  • 데이터 로직을 분리할 수 있다.
  • 중앙 집중처리로 일관된 인터페이스를 사용할 수 있다.
  • 단위 테스트가 가능하다.
  • 새로운 데이터 로직 코드 추가가 쉽다.

viewModel, dataBinding, viewBinding gradle setting

// build.gradle(app)
plugins {
  id 'kotlin-kapt'
}

android{
  buildFeatures {
    dataBinding = true
    viewBinding = true
  }
}

dependencies {
  implementation 'androidx.activity:activity-ktx:1.4.0'
}

android testing setting

dependencies{
// Local Unit Tests
    implementation "androidx.test:core:1.2.0"
    testImplementation "junit:junit:4.13"
    testImplementation "org.hamcrest:hamcrest-all:1.3"
    testImplementation "androidx.arch.core:core-testing:2.1.0"
    testImplementation "org.robolectric:robolectric:4.3.1"
    testImplementation "org.jetbrains.kotlinx:kotlinx-coroutines-test:1.5.0"
    testImplementation "com.google.truth:truth:1.1.2"
    testImplementation "org.mockito:mockito-core:2.21.0"

    // Instrumented Unit Tests
    androidTestImplementation "junit:junit:4.13"
    androidTestImplementation "com.linkedin.dexmaker:dexmaker-mockito:2.12.1"
    androidTestImplementation "org.jetbrains.kotlinx:kotlinx-coroutines-test:1.2.1"
    androidTestImplementation "androidx.arch.core:core-testing:2.1.0"
    androidTestImplementation "com.google.truth:truth:1.1.2"
    androidTestImplementation 'androidx.test.ext:junit:1.1.3'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
    androidTestImplementation "org.mockito:mockito-core:2.21.0"
}
profile
도전을 즐기는

0개의 댓글