1.增加删除前监听事件 2.修改关键字key为id
This commit is contained in:
@@ -19,7 +19,20 @@ interface IService<TKey> {
|
||||
|
||||
fun add(command: ICommand): Message<TKey>
|
||||
|
||||
fun update(key: TKey, command: ICommand)
|
||||
fun update(id: TKey, command: ICommand)
|
||||
|
||||
fun remove(key: TKey)
|
||||
fun remove(id: TKey)
|
||||
|
||||
/**
|
||||
* 添加一个删除对象前执行监听器。
|
||||
* @param key 监听器标志。
|
||||
* @param func 监听方法。
|
||||
*/
|
||||
fun addBeforeRemoveListener(key: String, func: (id: TKey) -> Boolean)
|
||||
|
||||
/**
|
||||
* 移除一个删除对象前执行监听器。
|
||||
* @param key 监听器标志。
|
||||
*/
|
||||
fun removeBeforeRemoveListener(key: String)
|
||||
}
|
||||
|
||||
@@ -20,12 +20,34 @@ import com.synebula.gaea.log.ILogger
|
||||
* @since 2020-05-17
|
||||
*/
|
||||
open class Service<TAggregateRoot : IAggregateRoot<TKey>, TKey>(
|
||||
protected open var clazz: Class<TAggregateRoot>,
|
||||
protected open var repository: IRepository,
|
||||
protected open var converter: IObjectConverter,
|
||||
override var logger: ILogger
|
||||
protected open var clazz: Class<TAggregateRoot>,
|
||||
protected open var repository: IRepository,
|
||||
protected open var converter: IObjectConverter,
|
||||
override var logger: ILogger
|
||||
) : IService<TKey> {
|
||||
|
||||
/**
|
||||
* 删除对象前执行监听器。
|
||||
*/
|
||||
protected val beforeRemoveListeners = mutableMapOf<String, (id: TKey) -> Boolean>()
|
||||
|
||||
/**
|
||||
* 添加一个删除对象前执行监听器。
|
||||
* @param key 监听器标志。
|
||||
* @param func 监听方法。
|
||||
*/
|
||||
override fun addBeforeRemoveListener(key: String, func: (id: TKey) -> Boolean) {
|
||||
this.beforeRemoveListeners[key] = func
|
||||
}
|
||||
|
||||
/**
|
||||
* 移除一个删除对象前执行监听器。
|
||||
* @param key 监听器标志。
|
||||
*/
|
||||
override fun removeBeforeRemoveListener(key: String) {
|
||||
this.beforeRemoveListeners.remove(key)
|
||||
}
|
||||
|
||||
override fun add(command: ICommand): Message<TKey> {
|
||||
val msg = Message<TKey>()
|
||||
val root = this.convert(command)
|
||||
@@ -34,14 +56,21 @@ open class Service<TAggregateRoot : IAggregateRoot<TKey>, TKey>(
|
||||
return msg
|
||||
}
|
||||
|
||||
override fun update(key: TKey, command: ICommand) {
|
||||
override fun update(id: TKey, command: ICommand) {
|
||||
val root = this.convert(command)
|
||||
root.id = key
|
||||
root.id = id
|
||||
this.repository.update(root, this.clazz)
|
||||
}
|
||||
|
||||
override fun remove(key: TKey) {
|
||||
this.repository.remove(key, this.clazz)
|
||||
override fun remove(id: TKey) {
|
||||
var exec = true
|
||||
val functions = this.beforeRemoveListeners.values
|
||||
for (func in functions) {
|
||||
exec = func(id)
|
||||
if (!exec) break
|
||||
}
|
||||
if (exec)
|
||||
this.repository.remove(id, this.clazz)
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -20,16 +20,38 @@ import com.synebula.gaea.log.ILogger
|
||||
* @since 2020-05-15
|
||||
*/
|
||||
open class SpecificService<TAggregateRoot : IAggregateRoot<TKey>, TKey>(
|
||||
protected var clazz: Class<TAggregateRoot>,
|
||||
protected var repository: ISpecificRepository<TAggregateRoot, TKey>,
|
||||
protected var converter: IObjectConverter,
|
||||
override var logger: ILogger
|
||||
protected var clazz: Class<TAggregateRoot>,
|
||||
protected var repository: ISpecificRepository<TAggregateRoot, TKey>,
|
||||
protected var converter: IObjectConverter,
|
||||
override var logger: ILogger
|
||||
) : IService<TKey> {
|
||||
|
||||
init {
|
||||
this.repository.clazz = clazz
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除对象前执行监听器。
|
||||
*/
|
||||
protected val beforeRemoveListeners = mutableMapOf<String, (id: TKey) -> Boolean>()
|
||||
|
||||
/**
|
||||
* 添加一个删除对象前执行监听器。
|
||||
* @param key 监听器标志。
|
||||
* @param func 监听方法。
|
||||
*/
|
||||
override fun addBeforeRemoveListener(key: String, func: (id: TKey) -> Boolean) {
|
||||
this.beforeRemoveListeners[key] = func
|
||||
}
|
||||
|
||||
/**
|
||||
* 移除一个删除对象前执行监听器。
|
||||
* @param key 监听器标志。
|
||||
*/
|
||||
override fun removeBeforeRemoveListener(key: String) {
|
||||
this.beforeRemoveListeners.remove(key)
|
||||
}
|
||||
|
||||
override fun add(command: ICommand): Message<TKey> {
|
||||
val msg = Message<TKey>()
|
||||
val root = this.convert(command)
|
||||
@@ -38,18 +60,25 @@ open class SpecificService<TAggregateRoot : IAggregateRoot<TKey>, TKey>(
|
||||
return msg
|
||||
}
|
||||
|
||||
override fun update(key: TKey, command: ICommand) {
|
||||
override fun update(id: TKey, command: ICommand) {
|
||||
val root = this.convert(command)
|
||||
root.id = key
|
||||
root.id = id
|
||||
this.repository.update(root)
|
||||
}
|
||||
|
||||
override fun remove(key: TKey) {
|
||||
this.repository.remove(key)
|
||||
override fun remove(id: TKey) {
|
||||
var exec = true
|
||||
val functions = this.beforeRemoveListeners.values
|
||||
for (func in functions) {
|
||||
exec = func(id)
|
||||
if (!exec) break
|
||||
}
|
||||
if (exec)
|
||||
this.repository.remove(id)
|
||||
}
|
||||
|
||||
fun get(key: TKey): TAggregateRoot {
|
||||
return this.repository.get(key)
|
||||
fun get(id: TKey): TAggregateRoot {
|
||||
return this.repository.get(id)
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -12,7 +12,7 @@ interface IQuery {
|
||||
* @param key 对象Key。
|
||||
* @return
|
||||
*/
|
||||
fun <TView, TKey> get(key: TKey, clazz: Class<TView>): TView?
|
||||
fun <TView, TKey> get(id: TKey, clazz: Class<TView>): TView?
|
||||
|
||||
/**
|
||||
* 根据实体类条件查询所有符合条件记录
|
||||
|
||||
@@ -14,7 +14,7 @@ interface ISpecificQuery<TView, TKey> {
|
||||
* @param key 对象Key。
|
||||
* @return
|
||||
*/
|
||||
fun get(key: TKey): TView?
|
||||
fun get(id: TKey): TView?
|
||||
|
||||
/**
|
||||
* 根据实体类条件查询所有符合条件记录
|
||||
|
||||
Reference in New Issue
Block a user