add lazy app module
This commit is contained in:
@@ -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<TRoot : IAggregateRoot<TKey>, TKey>(
|
||||||
|
override var name: String,
|
||||||
|
override var clazz: Class<TRoot>, //view class type
|
||||||
|
override var service: ILazyService<TRoot, TKey>,
|
||||||
|
override var query: IQuery,
|
||||||
|
override var logger: ILogger?
|
||||||
|
) : ILazyCommandApp<TRoot, TKey>, IQueryApp<TRoot, TKey> {
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
override var jsonSerializer: IJsonSerializer? = null
|
||||||
|
}
|
||||||
@@ -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<TRoot : IAggregateRoot<TKey>, TKey> : IApplication {
|
||||||
|
var jsonSerializer: IJsonSerializer?
|
||||||
|
|
||||||
|
var service: ILazyService<TRoot, TKey>
|
||||||
|
|
||||||
|
@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
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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<TRoot : IAggregateRoot<TKey>, TKey>(
|
||||||
|
override var name: String,
|
||||||
|
override var service: ILazyService<TRoot, TKey>,
|
||||||
|
override var logger: ILogger?
|
||||||
|
) : ILazyCommandApp<TRoot, TKey> {
|
||||||
|
@Resource
|
||||||
|
override var jsonSerializer: IJsonSerializer? = null
|
||||||
|
}
|
||||||
@@ -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<TAggregateRoot : IAggregateRoot<TKey>, TKey> {
|
||||||
|
/**
|
||||||
|
* 日志组件。
|
||||||
|
*/
|
||||||
|
var logger: ILogger
|
||||||
|
|
||||||
|
fun add(root: TAggregateRoot): DataMessage<TKey>
|
||||||
|
|
||||||
|
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)
|
||||||
|
}
|
||||||
@@ -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<TAggregateRoot : IAggregateRoot<TKey>, TKey>(
|
||||||
|
protected open var clazz: Class<TAggregateRoot>,
|
||||||
|
protected open var repository: IRepository,
|
||||||
|
override var logger: ILogger
|
||||||
|
) : ILazyService<TAggregateRoot, TKey> {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 删除对象前执行监听器。
|
||||||
|
*/
|
||||||
|
protected val beforeRemoveListeners = mutableMapOf<String, (id: TKey) -> 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<TKey> {
|
||||||
|
val msg = DataMessage<TKey>()
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user