Skip to content
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

[부나] 1단계 자동 DI 미션 제출합니다 #2

Merged
merged 14 commits into from
Sep 4, 2023

Conversation

tmdgh1592
Copy link
Member

@tmdgh1592 tmdgh1592 commented Sep 2, 2023

안녕하세요 반달!

지연/리플렉션을 통해 구현하였습니다.
코드를 보다가 의구심이 드는 부분은 과감하게 리뷰해주세요!

테스트 코드는 정상적으로 통과하며, 요구사항 체크리스트는 아래와 같이 완료하였습니다.

감사합니다.

+) 따로 Dao를 사용하지 않았기 때문에 Repository 단위 테스트는 진행하지 않았습니다.
이후에 Room을 사용하면 테스트 코드를 추가로 작성하겠습니다 : )

0.5단계
기능 요구 사항
생성자 주입 - 수동
다음 문제점을 해결한다.

  • 테스트하기 어렵다.
  • Repository 객체를 교체하기 위해 또다른 객체를 만들어 바꿔줘야 한다. 즉, ViewModel에 직접적인 변경사항이 발생한다.

1단계
기능 요구 사항
생성자 주입 - 자동
다음 문제점을 해결한다.

  • ViewModel에서 참조하는 Repository가 정상적으로 주입되지 않는다.
  • Repository를 참조하는 다른 객체가 생기면 주입 코드를 매번 만들어줘야 한다.
  • ViewModel에 수동으로 주입되고 있는 의존성들을 자동으로 주입되도록 바꿔본다.
  • 특정 ViewModel에서만이 아닌, 범용적으로 활용될 수 있는 자동 주입 로직을 작성한다. (MainViewModel, CartViewModel 모두 하나의 로직만 참조한다)
  • 100개의 ViewModel이 생긴다고 가정했을 때, 자동 주입 로직 100개가 생기는 것이 아니다. 하나의 자동 주입 로직을 재사용할 수 있어야 한다.
  • 장바구니에 접근할 때마다 매번 CartRepository 인스턴스를 새로 만들고 있다.
  • 여러 번 인스턴스화할 필요 없는 객체는 최초 한 번만 인스턴스화한다. (이 단계에서는 너무 깊게 생각하지 말고 싱글 오브젝트로 구현해도 된다.)

선택 요구 사항

  • TDD로 DI 구현
  • Robolectric으로 기능 테스트
  • ViewModel 테스트
  • 모든 도메인 로직, Repository 단위 테스트

프로그래밍 요구 사항

  • 사전에 주어진 테스트 코드가 모두 성공해야 한다.
  • Annotation은 이 단계에서 활용하지 않는다.

@tmdgh1592 tmdgh1592 requested a review from no1msh September 2, 2023 05:32
Copy link

@no1msh no1msh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

안녕하세요 부나!

DSL로 깔끔하게 구현한 자동 DI가 인상적이었습니다!

변경 요청보단 궁금한 점 위주로 리뷰하였습니다.

확인 부탁드리겠습니다.!!

Comment on lines +38 to +46
class ClassInjectorDsl {
inline fun <reified T : Any> inject(instance: T) {
ClassInjector.inject(instance)
}
}

fun modules(block: ClassInjectorDsl.() -> Unit) {
ClassInjectorDsl().apply(block)
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

DI 라이브러리를 구현하는 입장에서 DSL로 구현하신점 멋지네요.
DSL로 구현하시면서 체감되는 장점은 어떤 것들이 있었나요?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

DSL의 장점은 아무래도 조금 더 코틀린스럽게 코드를 작성할 수 있는 것이 가장 큰 장점이라고 생각합니다.

이번 미션은 마치 라이브러리를 만드는 느낌이 강했기 때문에,
본인을 포함하여, 코드를 사용하는 개발자 입장에서 생각해봤을 때
보다 깔끔하고 편리하게 코드를 사용할 수 있을 것이라고 생각합니다.

Comment on lines 10 to 12
@MainThread
inline fun <reified VM : ViewModel> ComponentActivity.viewModel(): Lazy<VM> {
return ViewModelLazy(
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@MainThread 어노테이션 사용 이유가 궁금합니다!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

기존 코드에서는 내부적으로 MainThread에서 동작하는 함수가 일부 있어서 추가해두었습니다.
지금은 필요 없으니 제거해도 괜찮아 보이네요! 👍🏻

Comment on lines 9 to 11
class CartViewModel constructor(
private val cartRepository: CartRepository,
) : ViewModel() {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

constructor를 명시해준 이유가 궁금합니다!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

원래는 constructor와 관련된 애노테이션을 사용할 것을 가정하고 constructor를 명시해주었습니다.
하지만 미션 요구사항과 같이 애노테이션을 사용하지 않는 것을 원칙으로 하고 있기 때문에 명시해줄 필요가 없어보이네요!
제거하도록 하겠습니다 : )

Comment on lines +21 to +22
@OptIn(ExperimentalStdlibApi::class)
fun getParameterValues(parameters: List<KParameter>): MutableList<Any> {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@OptIn 어노테이션을 사용하신 이유가 궁금합니다.

Copy link
Member Author

@tmdgh1592 tmdgh1592 Sep 4, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

스크린샷 2023-09-04 오후 7 49 00

위 사진에서 보이는 문구처럼, KType 객체에서 javaType을 얻어오는 확장 프로퍼티는 아직 실험적 단계이더군요!
따라서 @OptIn(ExperimentalStdlibApi::class)을 명시적으로 지정해주지 않으면 컴파일 에러가 발생하여 추가하였습니다 : )

Copy link

@no1msh no1msh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

아 request changes를 안해서 리뷰반영 요청이 안들어가네요 한번 더 작성하겠습니다. 죄송합니다!

Copy link

@no1msh no1msh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1단계 미션 고생 많으셨습니다!
역쉬 고수부나군요.
어노테이션 사용시 어떻게 바뀔지 기대가 됩니다!

@no1msh no1msh merged commit e4c9c8d into woowacourse:tmdgh1592 Sep 4, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants