From 248d9a57d457550214276e9d384a93dc3e4e34a5 Mon Sep 17 00:00:00 2001 From: alex Date: Tue, 22 Mar 2022 09:57:44 +0800 Subject: [PATCH] add lazy app module --- .../com/synebula/gaea/app/LazyApplication.kt | 30 ++++++++ .../synebula/gaea/app/cmd/ILazyCommandApp.kt | 50 +++++++++++++ .../synebula/gaea/app/cmd/LazyCommandApp.kt | 25 +++++++ .../gaea/domain/service/ILazyService.kt | 39 ++++++++++ .../gaea/domain/service/LazyService.kt | 73 +++++++++++++++++++ 5 files changed, 217 insertions(+) create mode 100644 src/gaea.app/src/main/kotlin/com/synebula/gaea/app/LazyApplication.kt create mode 100644 src/gaea.app/src/main/kotlin/com/synebula/gaea/app/cmd/ILazyCommandApp.kt create mode 100644 src/gaea.app/src/main/kotlin/com/synebula/gaea/app/cmd/LazyCommandApp.kt create mode 100644 src/gaea/src/main/kotlin/com/synebula/gaea/domain/service/ILazyService.kt create mode 100644 src/gaea/src/main/kotlin/com/synebula/gaea/domain/service/LazyService.kt diff --git a/src/gaea.app/src/main/kotlin/com/synebula/gaea/app/LazyApplication.kt b/src/gaea.app/src/main/kotlin/com/synebula/gaea/app/LazyApplication.kt new file mode 100644 index 0000000..628a9e9 --- /dev/null +++ b/src/gaea.app/src/main/kotlin/com/synebula/gaea/app/LazyApplication.kt @@ -0,0 +1,30 @@ +package com.synebula.gaea.app + +import com.synebula.gaea.app.cmd.ILazyCommandApp +import com.synebula.gaea.app.query.IQueryApp +import com.synebula.gaea.data.serialization.json.IJsonSerializer +import com.synebula.gaea.domain.model.IAggregateRoot +import com.synebula.gaea.domain.service.ILazyService +import com.synebula.gaea.log.ILogger +import com.synebula.gaea.query.IQuery +import javax.annotation.Resource + +/** + * 联合服务,同时实现了ILazyCommandApp和IQueryApp接口 + * + * @param name 业务名称 + * @param service 业务domain服务 + * @param query 业务查询服务 + * @param logger 日志组件 + */ +open class LazyApplication, TKey>( + override var name: String, + override var clazz: Class, //view class type + override var service: ILazyService, + override var query: IQuery, + override var logger: ILogger? +) : ILazyCommandApp, IQueryApp { + + @Resource + override var jsonSerializer: IJsonSerializer? = null +} \ No newline at end of file diff --git a/src/gaea.app/src/main/kotlin/com/synebula/gaea/app/cmd/ILazyCommandApp.kt b/src/gaea.app/src/main/kotlin/com/synebula/gaea/app/cmd/ILazyCommandApp.kt new file mode 100644 index 0000000..b3e1aec --- /dev/null +++ b/src/gaea.app/src/main/kotlin/com/synebula/gaea/app/cmd/ILazyCommandApp.kt @@ -0,0 +1,50 @@ +package com.synebula.gaea.app.cmd + +import com.synebula.gaea.app.IApplication +import com.synebula.gaea.app.component.aop.annotation.MethodName +import com.synebula.gaea.app.struct.HttpMessage +import com.synebula.gaea.data.message.Status +import com.synebula.gaea.data.serialization.json.IJsonSerializer +import com.synebula.gaea.domain.model.IAggregateRoot +import com.synebula.gaea.domain.service.ILazyService +import org.springframework.web.bind.annotation.* + +/** + * 直接使用实体对象提供服务的api + * + * @author alex + * @version 0.1 + * @since 2020-05-15 + */ +interface ILazyCommandApp, TKey> : IApplication { + var jsonSerializer: IJsonSerializer? + + var service: ILazyService + + @PostMapping + @MethodName("添加") + fun add(@RequestBody entity: TRoot): HttpMessage { + return HttpMessage(service.add(entity)) + } + + @PutMapping("/{id:.+}") + @MethodName("更新") + fun update(@PathVariable id: TKey, @RequestBody entity: TRoot): HttpMessage { + this.service.update(id, entity) + return HttpMessage() + } + + @DeleteMapping("/{id:.+}") + @MethodName("删除") + fun remove(@PathVariable id: TKey): HttpMessage { + val msg = HttpMessage() + try { + msg.data = this.service.remove(id) + } catch (ex: IllegalStateException) { + msg.status = Status.Error + msg.message = ex.message ?: "" + } + + return msg + } +} diff --git a/src/gaea.app/src/main/kotlin/com/synebula/gaea/app/cmd/LazyCommandApp.kt b/src/gaea.app/src/main/kotlin/com/synebula/gaea/app/cmd/LazyCommandApp.kt new file mode 100644 index 0000000..46abef3 --- /dev/null +++ b/src/gaea.app/src/main/kotlin/com/synebula/gaea/app/cmd/LazyCommandApp.kt @@ -0,0 +1,25 @@ +package com.synebula.gaea.app.cmd + +import com.synebula.gaea.data.serialization.json.IJsonSerializer +import com.synebula.gaea.domain.model.IAggregateRoot +import com.synebula.gaea.domain.service.ICommand +import com.synebula.gaea.domain.service.ILazyService +import com.synebula.gaea.domain.service.IService +import com.synebula.gaea.log.ILogger +import javax.annotation.Resource + +/** + * 指令服务,同时实现ICommandApp + * + * @param name 业务名称 + * @param service 业务domain服务 + * @param logger 日志组件 + */ +open class LazyCommandApp, TKey>( + override var name: String, + override var service: ILazyService, + override var logger: ILogger? +) : ILazyCommandApp { + @Resource + override var jsonSerializer: IJsonSerializer? = null +} \ No newline at end of file diff --git a/src/gaea/src/main/kotlin/com/synebula/gaea/domain/service/ILazyService.kt b/src/gaea/src/main/kotlin/com/synebula/gaea/domain/service/ILazyService.kt new file mode 100644 index 0000000..2d2efec --- /dev/null +++ b/src/gaea/src/main/kotlin/com/synebula/gaea/domain/service/ILazyService.kt @@ -0,0 +1,39 @@ +package com.synebula.gaea.domain.service + +import com.synebula.gaea.data.message.DataMessage +import com.synebula.gaea.data.message.Message +import com.synebula.gaea.domain.model.IAggregateRoot +import com.synebula.gaea.log.ILogger + + +/** + * 继承该接口,表明对象为领域服务。 + * @author alex + * @version 0.0.1 + * @since 2016年9月18日 下午2:23:15 + */ +interface ILazyService, TKey> { + /** + * 日志组件。 + */ + var logger: ILogger + + fun add(root: TAggregateRoot): DataMessage + + fun update(id: TKey, root: TAggregateRoot) + + fun remove(id: TKey) + + /** + * 添加一个删除对象前执行监听器。 + * @param key 监听器标志。 + * @param func 监听方法。 + */ + fun addBeforeRemoveListener(key: String, func: (id: TKey) -> Message) + + /** + * 移除一个删除对象前执行监听器。 + * @param key 监听器标志。 + */ + fun removeBeforeRemoveListener(key: String) +} diff --git a/src/gaea/src/main/kotlin/com/synebula/gaea/domain/service/LazyService.kt b/src/gaea/src/main/kotlin/com/synebula/gaea/domain/service/LazyService.kt new file mode 100644 index 0000000..a0f6f4b --- /dev/null +++ b/src/gaea/src/main/kotlin/com/synebula/gaea/domain/service/LazyService.kt @@ -0,0 +1,73 @@ +package com.synebula.gaea.domain.service + +import com.synebula.gaea.data.IObjectConverter +import com.synebula.gaea.data.message.DataMessage +import com.synebula.gaea.data.message.Message +import com.synebula.gaea.domain.model.IAggregateRoot +import com.synebula.gaea.domain.repository.IRepository +import com.synebula.gaea.log.ILogger + + +/** + * 依赖了IRepository仓储借口的服务实现类 Service + * 该类依赖仓储接口 @see IRepository, 需要显式提供聚合根的class对象 + * + * @param repository 仓储对象 + * @param clazz 聚合根类对象 + * @param logger 日志组件 + * @author alex + * @version 0.1 + * @since 2020-05-17 + */ +open class LazyService, TKey>( + protected open var clazz: Class, + protected open var repository: IRepository, + override var logger: ILogger +) : ILazyService { + + /** + * 删除对象前执行监听器。 + */ + protected val beforeRemoveListeners = mutableMapOf Message>() + + /** + * 添加一个删除对象前执行监听器。 + * @param key 监听器标志。 + * @param func 监听方法。 + */ + override fun addBeforeRemoveListener(key: String, func: (id: TKey) -> Message) { + this.beforeRemoveListeners[key] = func + } + + /** + * 移除一个删除对象前执行监听器。 + * @param key 监听器标志。 + */ + override fun removeBeforeRemoveListener(key: String) { + this.beforeRemoveListeners.remove(key) + } + + override fun add(root: TAggregateRoot): DataMessage { + val msg = DataMessage() + this.repository.add(root, this.clazz) + msg.data = root.id + return msg + } + + override fun update(id: TKey, root: TAggregateRoot) { + root.id = id + this.repository.update(root, this.clazz) + } + + override fun remove(id: TKey) { + val functions = this.beforeRemoveListeners.values + var msg: Message + for (func in functions) { + msg = func(id) + if (!msg.success()) { + throw IllegalStateException(msg.message) + } + } + this.repository.remove(id, this.clazz) + } +}