0
# Property Management
1
2
Property injection and management system for configuration values and runtime parameters with type-safe access patterns. Enables configuration-driven dependency injection and property-based application setup.
3
4
## Capabilities
5
6
### Property Access in Koin
7
8
Global property management for application-wide configuration values with type-safe retrieval and default value support.
9
10
```kotlin { .api }
11
/**
12
* Get property value with default fallback
13
* @param key - Property key
14
* @param defaultValue - Default value if property not found
15
* @return Property value or default
16
*/
17
fun <T : Any> Koin.getProperty(key: String, defaultValue: T): T
18
19
/**
20
* Get property value or null if not found
21
* @param key - Property key
22
* @return Property value or null
23
*/
24
fun <T : Any> Koin.getProperty(key: String): T?
25
26
/**
27
* Set property value
28
* @param key - Property key
29
* @param value - Property value
30
*/
31
fun Koin.setProperty(key: String, value: Any)
32
33
/**
34
* Delete property
35
* @param key - Property key to delete
36
*/
37
fun Koin.deleteProperty(key: String)
38
```
39
40
**Usage Examples:**
41
42
```kotlin
43
import org.koin.core.context.GlobalContext
44
import org.koin.core.context.startKoin
45
46
// Setup properties at startup
47
startKoin {
48
properties(mapOf(
49
"database.host" to "localhost",
50
"database.port" to 5432,
51
"api.timeout" to 30000,
52
"feature.enabled" to true
53
))
54
modules(appModule)
55
}
56
57
// Access properties in modules
58
val databaseModule = module {
59
single<DatabaseConfig> {
60
val host = getKoin().getProperty<String>("database.host")
61
val port = getKoin().getProperty("database.port", 5432)
62
val timeout = getKoin().getProperty("connection.timeout", 5000)
63
64
DatabaseConfig(host, port, timeout)
65
}
66
}
67
68
// Runtime property management
69
fun updateConfiguration() {
70
val koin = GlobalContext.get()
71
72
// Update property
73
koin.setProperty("api.timeout", 60000)
74
75
// Get updated value
76
val newTimeout = koin.getProperty("api.timeout", 30000)
77
78
// Remove obsolete property
79
koin.deleteProperty("deprecated.setting")
80
}
81
```
82
83
### Property Access in Scope
84
85
Scope-specific property access with inheritance from parent scopes and the global Koin context.
86
87
```kotlin { .api }
88
/**
89
* Get property value with default fallback from scope context
90
* @param key - Property key
91
* @param defaultValue - Default value if property not found
92
* @return Property value or default
93
*/
94
fun <T : Any> Scope.getProperty(key: String, defaultValue: T): T
95
96
/**
97
* Get property value or null from scope context
98
* @param key - Property key
99
* @return Property value or null
100
*/
101
fun <T : Any> Scope.getPropertyOrNull(key: String): T?
102
103
/**
104
* Get property value from scope context (throws if not found)
105
* @param key - Property key
106
* @return Property value
107
* @throws MissingPropertyException if property not found
108
*/
109
fun <T : Any> Scope.getProperty(key: String): T
110
```
111
112
**Usage Examples:**
113
114
```kotlin
115
import org.koin.dsl.module
116
117
// Scope with property access
118
val userModule = module {
119
scope<UserSession> {
120
scoped<UserPreferences> {
121
val theme = getProperty("user.theme", "light")
122
val language = getProperty<String>("user.language")
123
val notifications = getProperty("notifications.enabled", true)
124
125
UserPreferences(theme, language, notifications)
126
}
127
}
128
}
129
130
// Using properties in scoped components
131
class UserService : KoinScopeComponent {
132
override val scope: Scope by lazy { getKoin().createScope<UserService>() }
133
134
fun getUserSettings(): UserSettings {
135
val maxItems = scope.getProperty("display.maxItems", 10)
136
val sortOrder = scope.getPropertyOrNull<String>("display.sortOrder")
137
138
return UserSettings(maxItems, sortOrder)
139
}
140
}
141
```
142
143
### Property Injection Extensions
144
145
Direct property injection into Kotlin properties using delegation for seamless configuration access.
146
147
```kotlin { .api }
148
/**
149
* Inject property value into mutable property using global Koin context
150
*/
151
inline fun <reified T> KMutableProperty0<T>.inject()
152
153
/**
154
* Inject property value into mutable property using specific Koin instance
155
* @param koin - Koin instance to use for property resolution
156
*/
157
inline fun <reified T> KMutableProperty0<T>.inject(koin: Koin)
158
159
/**
160
* Inject property value into mutable property using specific scope
161
* @param scope - Scope instance to use for property resolution
162
*/
163
inline fun <reified T> KMutableProperty0<T>.inject(scope: Scope)
164
```
165
166
**Usage Examples:**
167
168
```kotlin
169
import org.koin.ext.inject
170
import org.koin.core.context.startKoin
171
172
// Setup application with properties
173
startKoin {
174
properties(mapOf(
175
"app.version" to "1.0.0",
176
"app.name" to "MyApplication",
177
"debug.enabled" to false
178
))
179
modules(appModule)
180
}
181
182
// Property injection in classes
183
class ApplicationConfig {
184
var appVersion: String = ""
185
var appName: String = ""
186
var debugEnabled: Boolean = false
187
188
init {
189
::appVersion.inject() // Injects from "appVersion" property
190
::appName.inject() // Injects from "appName" property
191
::debugEnabled.inject() // Injects from "debugEnabled" property
192
}
193
}
194
195
// Property injection with custom Koin instance
196
class ServiceConfig(private val koin: Koin) {
197
var serviceUrl: String = ""
198
var maxRetries: Int = 0
199
200
init {
201
::serviceUrl.inject(koin)
202
::maxRetries.inject(koin)
203
}
204
}
205
206
// Property injection with scope
207
class UserSessionConfig : KoinScopeComponent {
208
override val scope: Scope by lazy { getKoin().createScope<UserSessionConfig>() }
209
210
var sessionTimeout: Long = 0
211
var autoSave: Boolean = false
212
213
init {
214
::sessionTimeout.inject(scope)
215
::autoSave.inject(scope)
216
}
217
}
218
```
219
220
### Property-Driven Definitions
221
222
Use properties to configure dependency definitions dynamically based on runtime configuration.
223
224
**Usage Examples:**
225
226
```kotlin
227
import org.koin.dsl.module
228
229
// Property-driven module configuration
230
val networkModule = module {
231
single<HttpClient> {
232
val baseUrl = getProperty<String>("api.baseUrl")
233
val timeout = getProperty("api.timeout", 30000)
234
val retries = getProperty("api.retries", 3)
235
236
HttpClient {
237
baseUrl(baseUrl)
238
timeout(timeout)
239
retry(retries)
240
}
241
}
242
243
single<CacheConfig> {
244
val cacheSize = getProperty("cache.maxSize", 100)
245
val cacheTtl = getProperty("cache.ttlMinutes", 60)
246
val cacheEnabled = getProperty("cache.enabled", true)
247
248
if (cacheEnabled) {
249
CacheConfig(cacheSize, cacheTtl)
250
} else {
251
CacheConfig.disabled()
252
}
253
}
254
}
255
256
// Environment-specific configuration
257
val environmentModule = module {
258
single<DatabaseConnection> {
259
val environment = getProperty("app.environment", "development")
260
261
when (environment) {
262
"production" -> {
263
val host = getProperty<String>("db.prod.host")
264
val credentials = getProperty<String>("db.prod.credentials")
265
ProductionDatabase(host, credentials)
266
}
267
"staging" -> {
268
val host = getProperty<String>("db.staging.host")
269
StagingDatabase(host)
270
}
271
else -> {
272
LocalDatabase()
273
}
274
}
275
}
276
}
277
278
// Feature flag driven injection
279
val featureModule = module {
280
factory<PaymentProcessor> {
281
val useNewProcessor = getProperty("feature.newPaymentProcessor", false)
282
283
if (useNewProcessor) {
284
NewPaymentProcessor(get())
285
} else {
286
LegacyPaymentProcessor(get())
287
}
288
}
289
}
290
```
291
292
## Types
293
294
### Property-Related Exceptions
295
296
```kotlin { .api }
297
/**
298
* Exception thrown when required property is not found
299
*/
300
class MissingPropertyException(msg: String) : Exception(msg)
301
302
/**
303
* Exception thrown when property file cannot be found
304
*/
305
class NoPropertyFileFoundException(msg: String) : Exception(msg)
306
```