-
Notifications
You must be signed in to change notification settings - Fork 25
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
[둘리] 2, 3단계 자동 DI 미션 제출합니다 #28
Changes from 1 commit
168589f
87d3c2a
657518e
1bdceaf
5e94a5e
7ab7e4e
04ac319
c75db5c
f483bbb
d2744a7
c6393e6
8b64b40
ffa5c7b
2254b53
8d57300
e2b9366
62465b1
559b93f
f6c41a8
c0ee682
38725ee
55308d0
a80f744
15b4fe6
37d31ed
7f5c22d
368e9c9
83f960e
6f0dcf2
d66108d
5abddb2
b9ebfdf
0b4f3a1
2498638
9eecad2
8a46ecd
8fca93c
a636e8c
7184501
7443924
353d48b
bbb76a5
a89cc44
acaa5ef
e6ee91f
1f27660
d53b175
885ab4d
fa81116
77bc755
ad360bb
55bc13c
cc6cc2e
6c59beb
a7baa40
47600c6
a91d38b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,9 @@ | ||
package woowacourse.shopping.data | ||
|
||
import io.hyemdooly.di.annotation.InMemory | ||
import woowacourse.shopping.model.CartProduct | ||
import woowacourse.shopping.model.Product | ||
|
||
// TODO: Step2 - CartProductDao를 참조하도록 변경 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 필요없는 주석이 InMemory 에도 따라왔네요! |
||
@InMemory | ||
class InMemoryCartRepository : CartRepository { | ||
private val products = mutableListOf<CartProduct>() | ||
override suspend fun addCartProduct(product: Product) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,19 @@ | ||
package io.hyemdooly.di | ||
|
||
import kotlin.reflect.KClass | ||
|
||
data class InstanceInfo(val clazz: KClass<*>, val annotations: List<Annotation>) | ||
import kotlin.reflect.full.isSubclassOf | ||
|
||
object Container { | ||
private val instances = mutableMapOf<InstanceInfo, Any>() | ||
fun addInstance(type: KClass<*>, instance: Any) { | ||
val key = InstanceInfo(type, instance::class.annotations) | ||
instances[key] = instance | ||
private val instances = mutableMapOf<KClass<*>, Any>() | ||
|
||
fun addInstance(instance: Any) { | ||
println(instance::class) | ||
instances[instance::class] = instance | ||
} | ||
|
||
fun getInstance(type: KClass<*>, annotations: List<Annotation> = emptyList()): Any? { | ||
val key = InstanceInfo(type, annotations) | ||
fun getInstance(type: KClass<*>): Any? { | ||
if (instances[type] != null) instances[type] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. instances 의 get 을 해주는 것 말고는 하는게 없는데 의도하신게 있나요? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. return이 빠졌네요^^...;; |
||
val key = instances.keys.firstOrNull { it.isSubclassOf(type) } | ||
return instances[key] | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,11 @@ | ||
package io.hyemdooly.di | ||
|
||
import io.hyemdooly.di.annotation.Inject | ||
import io.hyemdooly.di.annotation.Qualifier | ||
import kotlin.reflect.KClass | ||
import kotlin.reflect.KFunction | ||
import kotlin.reflect.full.declaredMemberProperties | ||
import kotlin.reflect.full.findAnnotation | ||
import kotlin.reflect.full.hasAnnotation | ||
import kotlin.reflect.full.primaryConstructor | ||
import kotlin.reflect.jvm.isAccessible | ||
|
@@ -30,7 +32,13 @@ object Injector { | |
private fun <T> getParamInstances(constructor: KFunction<T>): List<Any> { | ||
val paramInstances = constructor.parameters.map { param -> | ||
val type = param.type.jvmErasure | ||
Container.getInstance(type, param.annotations) ?: inject(type) | ||
|
||
val annotation = param.findAnnotation<Qualifier>() | ||
if (annotation != null) { | ||
Container.getInstance(annotation.clazz) ?: inject(annotation.clazz) | ||
} else { | ||
Container.getInstance(type) ?: inject(type) | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 같은 함수에 대한 분기를 Container 외부에서 하고있네요. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Annotation을 뜯어 타입을 확인하는 것을 Container 내부에서 하게 하려고 했으나, |
||
} | ||
return paramInstances | ||
} | ||
|
This file was deleted.
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
package io.hyemdooly.di.annotation | ||
|
||
@Target(AnnotationTarget.ANNOTATION_CLASS) | ||
annotation class Qualifier | ||
import kotlin.reflect.KClass | ||
|
||
annotation class Qualifier(val clazz: KClass<*>) |
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. object 주입이나 인자가 여러개인 경우 등 다른 경우의 수를 추가해서 테스트 해봤는데 잘 작동하는 것 확인했습니다! |
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.
필요없는 주석은 제거해주세요!