Skip to content

Commit

Permalink
Ktor requestscope function dsl
Browse files Browse the repository at this point in the history
  • Loading branch information
arnaudgiuliani committed Dec 13, 2024
1 parent ae8f1f3 commit 3225d97
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
package org.koin.sample

import org.koin.core.module.KoinDslMarker
import org.koin.core.module.Module
import org.koin.core.module.dsl.bind
import org.koin.core.module.dsl.createdAtStart
import org.koin.core.module.dsl.scopedOf
import org.koin.core.module.dsl.singleOf
import org.koin.core.qualifier.TypeQualifier
import org.koin.dsl.ScopeDSL
import org.koin.dsl.module
import org.koin.ktor.plugin.RequestScope
import org.koin.module.requestScope

val appModule = module {
singleOf(::HelloServiceImpl){
Expand All @@ -14,7 +19,7 @@ val appModule = module {
}
singleOf(::HelloRepository)

scope<RequestScope>{
requestScope {
scopedOf(::ScopeComponent)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,9 @@ class Module(
get() = mappings.isNotEmpty()


@PublishedApi
internal val scopes = LinkedHashSet<Qualifier>()
@KoinInternalApi
// used by Ktor ext
val scopes = LinkedHashSet<Qualifier>()

@KoinInternalApi
val includedModules = mutableListOf<Module>()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright 2017-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.koin.module

import org.koin.core.annotation.KoinInternalApi
import org.koin.core.module.KoinDslMarker
import org.koin.core.module.Module
import org.koin.core.qualifier.TypeQualifier
import org.koin.dsl.ScopeDSL
import org.koin.ktor.plugin.RequestScope

/**
* Declare a scope<RequestScope> section, to define definitions in Request scope
*
* @author Arnaud Giuliani
*/
@OptIn(KoinInternalApi::class)
@KoinDslMarker
inline fun Module.requestScope(scopeSet: ScopeDSL.() -> Unit) {
val qualifier = TypeQualifier(RequestScope::class)
ScopeDSL(qualifier, this).apply(scopeSet)
scopes.add(qualifier)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright 2017-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.koin.module

import org.koin.core.annotation.KoinInternalApi
import org.koin.core.module.KoinDslMarker
import org.koin.core.module.Module
import org.koin.core.qualifier.TypeQualifier
import org.koin.dsl.ScopeDSL
import org.koin.ktor.plugin.RequestScope

/**
* Declare a scope<RequestScope> section, to define definitions in Request scope
*
* @author Arnaud Giuliani
*/
@OptIn(KoinInternalApi::class)
@KoinDslMarker
inline fun Module.requestScope(scopeSet: ScopeDSL.() -> Unit) {
val qualifier = TypeQualifier(RequestScope::class)
ScopeDSL(qualifier, this).apply(scopeSet)
scopes.add(qualifier)
}

0 comments on commit 3225d97

Please sign in to comment.