or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

Files

docs

application-setup.mdcomponent-integration.mdconfiguration-logging.mdconstructor-reference.mdindex.mdjava-interop.mdmodule-definition.mdproperty-management.mdqualifiers-parameters.mdscope-management.md

constructor-reference.mddocs/

0

# Constructor Reference DSL

1

2

Reflection-free dependency injection using constructor references. Provides type-safe constructor resolution without runtime reflection overhead, enabling better performance and compile-time safety.

3

4

## Capabilities

5

6

### SingleOf Constructor References

7

8

Define singleton instances using constructor references with full type safety and dependency resolution.

9

10

```kotlin { .api }

11

/**

12

* Create singleton instance using constructor reference (0 parameters)

13

* @param constructor - Constructor reference function

14

* @param options - Optional definition configuration

15

* @return KoinDefinition for the singleton

16

*/

17

inline fun <reified R> Module.singleOf(

18

crossinline constructor: () -> R,

19

noinline options: DefinitionOptions<R>? = null

20

): KoinDefinition<R>

21

22

/**

23

* Create singleton instance using constructor reference (1 parameter)

24

* @param constructor - Constructor reference function with 1 dependency

25

* @param options - Optional definition configuration

26

* @return KoinDefinition for the singleton

27

*/

28

inline fun <reified R, reified T1> Module.singleOf(

29

crossinline constructor: (T1) -> R,

30

noinline options: DefinitionOptions<R>? = null

31

): KoinDefinition<R>

32

33

/**

34

* Create singleton instance using constructor reference (2 parameters)

35

* @param constructor - Constructor reference function with 2 dependencies

36

* @param options - Optional definition configuration

37

* @return KoinDefinition for the singleton

38

*/

39

inline fun <reified R, reified T1, reified T2> Module.singleOf(

40

crossinline constructor: (T1, T2) -> R,

41

noinline options: DefinitionOptions<R>? = null

42

): KoinDefinition<R>

43

```

44

45

**Usage Examples:**

46

47

```kotlin

48

import org.koin.dsl.module

49

import org.koin.core.module.dsl.singleOf

50

51

// Zero parameter constructor

52

class Logger

53

val appModule = module {

54

singleOf(::Logger)

55

}

56

57

// Single parameter constructor

58

class UserService(private val repository: UserRepository)

59

val serviceModule = module {

60

singleOf(::UserRepository)

61

singleOf(::UserService)

62

}

63

64

// Multiple parameter constructor

65

class NotificationService(

66

private val userService: UserService,

67

private val emailService: EmailService

68

)

69

val notificationModule = module {

70

singleOf(::UserService)

71

singleOf(::EmailService)

72

singleOf(::NotificationService)

73

}

74

75

// With options

76

class DatabaseConfig

77

val configModule = module {

78

singleOf(::DatabaseConfig) {

79

createdAtStart()

80

bind<ConfigurationInterface>()

81

}

82

}

83

```

84

85

### FactoryOf Constructor References

86

87

Define factory instances using constructor references for new instances on each injection.

88

89

```kotlin { .api }

90

/**

91

* Create factory instance using constructor reference (0 parameters)

92

* @param constructor - Constructor reference function

93

* @param options - Optional definition configuration

94

* @return KoinDefinition for the factory

95

*/

96

inline fun <reified R> Module.factoryOf(

97

crossinline constructor: () -> R,

98

noinline options: DefinitionOptions<R>? = null

99

): KoinDefinition<R>

100

101

/**

102

* Create factory instance using constructor reference (1 parameter)

103

* @param constructor - Constructor reference function with 1 dependency

104

* @param options - Optional definition configuration

105

* @return KoinDefinition for the factory

106

*/

107

inline fun <reified R, reified T1> Module.factoryOf(

108

crossinline constructor: (T1) -> R,

109

noinline options: DefinitionOptions<R>? = null

110

): KoinDefinition<R>

111

112

/**

113

* Create factory instance using constructor reference (2 parameters)

114

* @param constructor - Constructor reference function with 2 dependencies

115

* @param options - Optional definition configuration

116

* @return KoinDefinition for the factory

117

*/

118

inline fun <reified R, reified T1, reified T2> Module.factoryOf(

119

crossinline constructor: (T1, T2) -> R,

120

noinline options: DefinitionOptions<R>? = null

121

): KoinDefinition<R>

122

```

123

124

**Usage Examples:**

125

126

```kotlin

127

import org.koin.dsl.module

128

import org.koin.core.module.dsl.factoryOf

129

130

// Request-scoped objects

131

class RequestContext

132

class RequestHandler(private val context: RequestContext)

133

134

val webModule = module {

135

factoryOf(::RequestContext)

136

factoryOf(::RequestHandler)

137

}

138

139

// Processing objects with dependencies

140

class DataProcessor(

141

private val validator: DataValidator,

142

private val transformer: DataTransformer

143

)

144

val processingModule = module {

145

singleOf(::DataValidator)

146

singleOf(::DataTransformer)

147

factoryOf(::DataProcessor)

148

}

149

```

150

151

### New Constructor Helper

152

153

Direct constructor resolution for immediate instance creation within definitions.

154

155

```kotlin { .api }

156

/**

157

* Create new instance using constructor reference within a definition

158

* @param constructor - Constructor reference function

159

* @return New instance of type R

160

*/

161

inline fun <reified R> Scope.new(constructor: () -> R): R

162

163

/**

164

* Create new instance using constructor reference with 1 dependency

165

* @param constructor - Constructor reference function with 1 dependency

166

* @return New instance of type R

167

*/

168

inline fun <reified R, reified T1> Scope.new(constructor: (T1) -> R): R

169

170

/**

171

* Create new instance using constructor reference with 2 dependencies

172

* @param constructor - Constructor reference function with 2 dependencies

173

* @return New instance of type R

174

*/

175

inline fun <reified R, reified T1, reified T2> Scope.new(constructor: (T1, T2) -> R): R

176

```

177

178

**Usage Examples:**

179

180

```kotlin

181

import org.koin.dsl.module

182

183

class DatabaseConnection(private val config: DatabaseConfig)

184

class ConnectionPool(connections: List<DatabaseConnection>)

185

186

val databaseModule = module {

187

single<DatabaseConfig> { DatabaseConfig() }

188

189

single<ConnectionPool> {

190

// Create multiple connections using new()

191

val connections = (1..5).map {

192

new(::DatabaseConnection)

193

}

194

ConnectionPool(connections)

195

}

196

}

197

198

// Complex object creation

199

class ComplexService(

200

private val dependency1: ServiceA,

201

private val dependency2: ServiceB,

202

private val helper: HelperClass

203

)

204

class HelperClass(private val config: Config)

205

206

val complexModule = module {

207

singleOf(::ServiceA)

208

singleOf(::ServiceB)

209

singleOf(::Config)

210

211

single<ComplexService> {

212

ComplexService(

213

get<ServiceA>(),

214

get<ServiceB>(),

215

new(::HelperClass) // Direct creation of helper

216

)

217

}

218

}

219

```

220

221

## Types

222

223

### Definition Options

224

225

```kotlin { .api }

226

typealias DefinitionOptions<T> = BeanDefinition<T>.() -> Unit

227

228

// Available options

229

fun BeanDefinition<*>.createdAtStart()

230

inline fun <reified T> BeanDefinition<out T>.bind()

231

fun BeanDefinition<*>.binds(classes: List<KClass<*>>)

232

fun BeanDefinition<*>.named(name: String)

233

inline fun <reified T> BeanDefinition<*>.named()

234

fun <T> BeanDefinition<T>.onClose(onClose: OnCloseCallback<T>)

235

```