Skip to content

Commit

Permalink
feat(core): Support ReadOnlyStateAggregateAware api (#1081)
Browse files Browse the repository at this point in the history
* feat(ReadOnlyStateAggregateAware): add ReadOnlyStateAggregateAware

* feat(core): Support ReadOnlyStateAggregateAware api
  • Loading branch information
Ahoo-Wang authored Dec 25, 2024
1 parent 6f25194 commit ac2755a
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,12 @@

package me.ahoo.wow.tck.mock

import com.fasterxml.jackson.annotation.JsonIgnore
import me.ahoo.wow.api.annotation.CreateAggregate
import me.ahoo.wow.api.annotation.OnCommand
import me.ahoo.wow.modeling.annotation.aggregateMetadata
import me.ahoo.wow.modeling.state.ReadOnlyStateAggregate
import me.ahoo.wow.modeling.state.ReadOnlyStateAggregateAware

val MOCK_AGGREGATE_METADATA = aggregateMetadata<MockCommandAggregate, MockStateAggregate>()

Expand Down Expand Up @@ -44,15 +47,21 @@ class MockCommandAggregate(val state: MockStateAggregate) {
}
}

data class MockStateAggregate(val id: String) {
data class MockStateAggregate(val id: String) : ReadOnlyStateAggregateAware<MockStateAggregate> {
var data: String = ""
private set

@field:JsonIgnore
private var readOnlyStateAggregate: ReadOnlyStateAggregate<MockStateAggregate>? = null
private fun onSourcing(aggregateCreated: MockAggregateCreated) {
data = aggregateCreated.data
}

private fun onSourcing(changed: MockAggregateChanged) {
data = changed.data
}

override fun setReadOnlyStateAggregate(readOnlyStateAggregate: ReadOnlyStateAggregate<MockStateAggregate>) {
this.readOnlyStateAggregate = readOnlyStateAggregate
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class GivenReadOnlyStateAggregate<S : Any>(
override val firstEventTime: Long,
override val eventTime: Long
) : ReadOnlyStateAggregate<S> {

companion object {
fun Any.toReadOnlyStateAggregate(domainEvent: DomainEvent<*>): ReadOnlyStateAggregate<*> {
if (this is ReadOnlyStateAggregate<*>) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*
* Copyright [2021-present] [ahoo wang <[email protected]> (https://github.com/Ahoo-Wang)].
* 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 me.ahoo.wow.modeling.state

interface ReadOnlyStateAggregateAware<S : Any> {
fun setReadOnlyStateAggregate(readOnlyStateAggregate: ReadOnlyStateAggregate<S>)
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ object ConstructorStateAggregateFactory : StateAggregateFactory {
if (log.isDebugEnabled) {
log.debug("Create {}.", aggregateId)
}
return SimpleStateAggregate(
val stateAggregate = SimpleStateAggregate(
aggregateId = aggregateId,
metadata = metadata,
state = state,
Expand All @@ -76,6 +76,12 @@ object ConstructorStateAggregateFactory : StateAggregateFactory {
eventTime = eventTime,
deleted = deleted,
)
if (state is ReadOnlyStateAggregateAware<*>) {
@Suppress("UNCHECKED_CAST")
val aware = state as ReadOnlyStateAggregateAware<S>
aware.setReadOnlyStateAggregate(stateAggregate)
}
return stateAggregate
}

private fun <S : Any> StateAggregateMetadata<S>.constructState(aggregateId: AggregateId): S {
Expand Down

0 comments on commit ac2755a

Please sign in to comment.