From d05bf62bf0eaffbfa6ce0c13f384dcf8acee43db Mon Sep 17 00:00:00 2001 From: Fabrice Lecomte Date: Sat, 5 Apr 2025 04:46:02 +0200 Subject: [PATCH] Add name to InMemory services --- .../infrastructureLayer/event/GameEventBusInMemory.kt | 2 +- .../event/projection/GameListRepositoryInMemory.kt | 1 + .../event/projection/GameProjectionBusInMemory.kt | 2 +- .../event/projection/GameStateRepositoryInMemory.kt | 1 + src/main/kotlin/eventDemo/libs/bus/BusInMemory.kt | 8 +++++++- .../projection/ProjectionSnapshotRepositoryInMemory.kt | 4 +++- 6 files changed, 14 insertions(+), 4 deletions(-) diff --git a/src/main/kotlin/eventDemo/adapter/infrastructureLayer/event/GameEventBusInMemory.kt b/src/main/kotlin/eventDemo/adapter/infrastructureLayer/event/GameEventBusInMemory.kt index a0848de..b57d6a3 100644 --- a/src/main/kotlin/eventDemo/adapter/infrastructureLayer/event/GameEventBusInMemory.kt +++ b/src/main/kotlin/eventDemo/adapter/infrastructureLayer/event/GameEventBusInMemory.kt @@ -8,7 +8,7 @@ import java.util.UUID class GameEventBusInMemory : GameEventBus, - Bus by BusInMemory(), + Bus by BusInMemory(GameEventBusInMemory::class), Comparable { private val instanceId: UUID = UUID.randomUUID() diff --git a/src/main/kotlin/eventDemo/adapter/infrastructureLayer/event/projection/GameListRepositoryInMemory.kt b/src/main/kotlin/eventDemo/adapter/infrastructureLayer/event/projection/GameListRepositoryInMemory.kt index a5d06e4..fc2c97e 100644 --- a/src/main/kotlin/eventDemo/adapter/infrastructureLayer/event/projection/GameListRepositoryInMemory.kt +++ b/src/main/kotlin/eventDemo/adapter/infrastructureLayer/event/projection/GameListRepositoryInMemory.kt @@ -21,6 +21,7 @@ class GameListRepositoryInMemory( ) : GameListRepository { private val projectionsSnapshot = ProjectionSnapshotRepositoryInMemory( + name = GameListRepositoryInMemory::class, eventStore = eventStore, snapshotCacheConfig = snapshotConfig, applyToProjection = GameList::apply, diff --git a/src/main/kotlin/eventDemo/adapter/infrastructureLayer/event/projection/GameProjectionBusInMemory.kt b/src/main/kotlin/eventDemo/adapter/infrastructureLayer/event/projection/GameProjectionBusInMemory.kt index 3437988..9a3c2d2 100644 --- a/src/main/kotlin/eventDemo/adapter/infrastructureLayer/event/projection/GameProjectionBusInMemory.kt +++ b/src/main/kotlin/eventDemo/adapter/infrastructureLayer/event/projection/GameProjectionBusInMemory.kt @@ -9,7 +9,7 @@ import java.util.UUID class GameProjectionBusInMemory : GameProjectionBus, - Bus> by BusInMemory(), + Bus> by BusInMemory(GameProjectionBusInMemory::class), Comparable { private val instanceId: UUID = UUID.randomUUID() diff --git a/src/main/kotlin/eventDemo/adapter/infrastructureLayer/event/projection/GameStateRepositoryInMemory.kt b/src/main/kotlin/eventDemo/adapter/infrastructureLayer/event/projection/GameStateRepositoryInMemory.kt index 417714b..7303f67 100644 --- a/src/main/kotlin/eventDemo/adapter/infrastructureLayer/event/projection/GameStateRepositoryInMemory.kt +++ b/src/main/kotlin/eventDemo/adapter/infrastructureLayer/event/projection/GameStateRepositoryInMemory.kt @@ -21,6 +21,7 @@ class GameStateRepositoryInMemory( ) : GameStateRepository { private val projectionsSnapshot = ProjectionSnapshotRepositoryInMemory( + name = GameStateRepositoryInMemory::class, eventStore = eventStore, snapshotCacheConfig = snapshotConfig, applyToProjection = GameState::apply, diff --git a/src/main/kotlin/eventDemo/libs/bus/BusInMemory.kt b/src/main/kotlin/eventDemo/libs/bus/BusInMemory.kt index c71655f..a0f13a4 100644 --- a/src/main/kotlin/eventDemo/libs/bus/BusInMemory.kt +++ b/src/main/kotlin/eventDemo/libs/bus/BusInMemory.kt @@ -1,13 +1,19 @@ package eventDemo.libs.bus +import io.github.oshai.kotlinlogging.KotlinLogging import io.github.oshai.kotlinlogging.withLoggingContext import kotlinx.coroutines.coroutineScope +import kotlin.reflect.KClass -class BusInMemory : Bus { +class BusInMemory( + val name: KClass<*> = BusInMemory::class, +) : Bus { + private val logger = KotlinLogging.logger(name.qualifiedName.toString()) private val subscribers: MutableList Unit>> = mutableListOf() override suspend fun publish(item: E) { withLoggingContext("busItem" to item.toString()) { + logger.info { "Item sent to the bus: $item" } subscribers .sortedByDescending { (priority, _) -> priority } .forEach { (_, block) -> diff --git a/src/main/kotlin/eventDemo/libs/event/projection/ProjectionSnapshotRepositoryInMemory.kt b/src/main/kotlin/eventDemo/libs/event/projection/ProjectionSnapshotRepositoryInMemory.kt index be85e49..db82251 100644 --- a/src/main/kotlin/eventDemo/libs/event/projection/ProjectionSnapshotRepositoryInMemory.kt +++ b/src/main/kotlin/eventDemo/libs/event/projection/ProjectionSnapshotRepositoryInMemory.kt @@ -10,15 +10,17 @@ import kotlinx.datetime.Clock import kotlinx.datetime.Instant import java.util.concurrent.ConcurrentHashMap import java.util.concurrent.ConcurrentLinkedQueue +import kotlin.reflect.KClass class ProjectionSnapshotRepositoryInMemory, P : Projection, ID : AggregateId>( + val name: KClass<*> = ProjectionSnapshotRepositoryInMemory::class, private val eventStore: EventStore, private val initialStateBuilder: (aggregateId: ID) -> P, private val snapshotCacheConfig: SnapshotConfig = SnapshotConfig(), private val applyToProjection: P.(event: E) -> P, ) : ProjectionSnapshotRepository { private val projectionsSnapshot: ConcurrentHashMap>> = ConcurrentHashMap() - private val logger = KotlinLogging.logger { } + private val logger = KotlinLogging.logger(name.qualifiedName.toString()) /** * Create a snapshot for the event