1.1.0 add service / repository / query spring auto config function

This commit is contained in:
2022-08-18 14:35:01 +08:00
parent b7cfd0a7f9
commit 07684e814d
42 changed files with 1001 additions and 272 deletions

View File

@@ -6,22 +6,29 @@ import com.synebula.gaea.domain.model.IAggregateRoot
* 定义了提供增删改的仓储接口。
* 本接口泛型放置到方法上并需要显式提供聚合根的class对象
*/
interface IRepository {
interface IRepository<TAggregateRoot : IAggregateRoot<ID>, ID> {
/**
* 仓储的对象类
*/
var clazz: Class<TAggregateRoot>
/**
* 插入单个对象。
*
* @param obj 需要插入的对象。
* @return 返回原对象如果对象ID为自增则补充自增ID。
*/
fun <TAggregateRoot : IAggregateRoot<ID>, ID> add(obj: TAggregateRoot, clazz: Class<TAggregateRoot>)
fun add(obj: TAggregateRoot)
/**
* 插入多个个对象。
*
* @param obj 需要插入的对象。
* @param list 需要插入的对象。
* @return 返回原对象如果对象ID为自增则补充自增ID。
*/
fun <TAggregateRoot : IAggregateRoot<ID>, ID> add(obj: List<TAggregateRoot>, clazz: Class<TAggregateRoot>)
fun add(list: List<TAggregateRoot>)
/**
* 更新对象。
@@ -29,24 +36,30 @@ interface IRepository {
* @param obj 需要更新的对象。
* @return
*/
fun <TAggregateRoot : IAggregateRoot<ID>, ID> update(obj: TAggregateRoot, clazz: Class<TAggregateRoot>)
fun update(obj: TAggregateRoot)
/**
* 更新多个个对象。
*
* @param list 需要新的对象。
*/
fun update(list: List<TAggregateRoot>)
/**
* 通过id删除该条数据
*
* @param id id
* @param clazz 操作数据的类型
* @param id 对象ID。
* @return
*/
fun <TAggregateRoot : IAggregateRoot<ID>, ID> remove(id: ID, clazz: Class<TAggregateRoot>)
fun remove(id: ID)
/**
* 根据ID获取对象。
*
* @param id id
* @param clazz 操作数据的类型
* @return 聚合根
* @param id 对象ID。
* @return
*/
fun <TAggregateRoot : IAggregateRoot<ID>, ID> get(id: ID, clazz: Class<TAggregateRoot>): TAggregateRoot?
fun get(id: ID): TAggregateRoot?
/**
@@ -55,5 +68,7 @@ interface IRepository {
* @param params 查询条件。
* @return int
*/
fun <TAggregateRoot> count(params: Map<String, Any>?, clazz: Class<TAggregateRoot>): Int
fun <TAggregateRoot> count(params: Map<String, Any>?): Int
}

View File

@@ -1,61 +0,0 @@
package com.synebula.gaea.domain.repository
import com.synebula.gaea.domain.model.IAggregateRoot
/**
* 继承本接口表示对象为仓储类。
* 定义了提供增删改的仓储接口。
* 本接口泛型定义在类上方法中不需要显式提供聚合根的class对象class对象作为类的成员变量声明。
*
* @param TAggregateRoot 聚合根类型
* @author alex
*/
interface ISpecificRepository<TAggregateRoot : IAggregateRoot<ID>, ID> {
/**
* 仓储的对象类
*/
var clazz: Class<TAggregateRoot>?
/**
* 插入单个对象。
*
* @param obj 需要插入的对象。
* @return 返回原对象如果对象ID为自增则补充自增ID。
*/
fun add(obj: TAggregateRoot)
/**
* 更新对象。
*
* @param obj 需要更新的对象。
* @return
*/
fun update(obj: TAggregateRoot)
/**
* 通过id删除该条数据
*
* @param id 对象ID。
* @return
*/
fun remove(id: ID)
/**
* 根据ID获取对象。
*
* @param id 对象ID。
* @return
*/
fun get(id: ID): TAggregateRoot
/**
* 根据条件查询符合条件记录的数量
*
* @param params 查询条件。
* @return int
*/
fun <TAggregateRoot> count(params: Map<String, Any>?): Int
}

View File

@@ -0,0 +1,65 @@
package com.synebula.gaea.domain.repository
import com.synebula.gaea.domain.model.IAggregateRoot
/**
* 定义了提供增删改的仓储接口。
* 本接口泛型放置到方法上并需要显式提供聚合根的class对象
*/
interface IUniversalRepository {
/**
* 插入单个对象。
*
* @param root 需要插入的对象。
* @return 返回原对象如果对象ID为自增则补充自增ID。
*/
fun <TAggregateRoot : IAggregateRoot<ID>, ID> add(root: TAggregateRoot, clazz: Class<TAggregateRoot>)
/**
* 插入多个个对象。
*
* @param roots 需要插入的对象。
*/
fun <TAggregateRoot : IAggregateRoot<ID>, ID> add(roots: List<TAggregateRoot>, clazz: Class<TAggregateRoot>)
/**
* 更新对象。
*
* @param root 需要更新的对象。
* @return
*/
fun <TAggregateRoot : IAggregateRoot<ID>, ID> update(root: TAggregateRoot, clazz: Class<TAggregateRoot>)
/**
* 更新多个个对象。
*
* @param roots 需要更新的对象。
*/
fun <TAggregateRoot : IAggregateRoot<ID>, ID> update(roots: List<TAggregateRoot>, clazz: Class<TAggregateRoot>)
/**
* 通过id删除该条数据
*
* @param id id
* @param clazz 操作数据的类型
*/
fun <TAggregateRoot : IAggregateRoot<ID>, ID> remove(id: ID, clazz: Class<TAggregateRoot>)
/**
* 根据ID获取对象。
*
* @param id id
* @param clazz 操作数据的类型
* @return 聚合根
*/
fun <TAggregateRoot : IAggregateRoot<ID>, ID> get(id: ID, clazz: Class<TAggregateRoot>): TAggregateRoot?
/**
* 根据条件查询符合条件记录的数量
*
* @param params 查询条件。
* @return int
*/
fun <TAggregateRoot> count(params: Map<String, Any>?, clazz: Class<TAggregateRoot>): Int
}

View File

@@ -1,8 +1,6 @@
package com.synebula.gaea.domain.service
import com.synebula.gaea.data.message.DataMessage
import com.synebula.gaea.data.message.StatusMessage
import com.synebula.gaea.log.ILogger
/**
@@ -12,27 +10,39 @@ import com.synebula.gaea.log.ILogger
* @since 2016年9月18日 下午2:23:15
*/
interface IService<ID> {
/**
* 日志组件。
*/
var logger: ILogger
/**
* 增加对象
*
* @param command 增加对象命令
*/
fun add(command: ICommand): DataMessage<ID>
/**
* 增加对象
*
* @param commands 增加对象命令列表
*/
fun add(commands: List<ICommand>)
/**
* 更新对象
*
* @param id 对象ID
* @param command 更新对象命令
*/
fun update(id: ID, command: ICommand)
/**
* 批量更新对象
*
* @param commands 更新对象命令列表
*/
fun update(commands: List<ICommand>)
/**
* 增加对象
* @param id 对象ID
*/
fun remove(id: ID)
/**
* 添加一个删除对象前执行监听器。
* @param key 监听器标志。
* @param func 监听方法。
*/
fun addBeforeRemoveListener(key: String, func: (id: ID) -> StatusMessage)
/**
* 移除一个删除对象前执行监听器。
* @param key 监听器标志。
*/
fun removeBeforeRemoveListener(key: String)
}

View File

@@ -1,7 +1,6 @@
package com.synebula.gaea.domain.service
import com.synebula.gaea.data.message.DataMessage
import com.synebula.gaea.data.message.StatusMessage
import com.synebula.gaea.domain.model.IAggregateRoot
import com.synebula.gaea.log.ILogger
@@ -18,22 +17,38 @@ interface ISimpleService<TAggregateRoot : IAggregateRoot<ID>, ID> {
*/
var logger: ILogger
/**
* 增加对象
*
* @param root 增加对象命令
*/
fun add(root: TAggregateRoot): DataMessage<ID>
/**
* 增加对象
*
* @param roots 增加对象命令列表
*/
fun add(roots: List<TAggregateRoot>)
/**
* 更新对象
*
* @param id 对象ID
* @param root 更新对象命令
*/
fun update(id: ID, root: TAggregateRoot)
/**
* 批量更新对象
*
* @param roots 更新对象命令列表
*/
fun update(roots: List<TAggregateRoot>)
/**
* 增加对象
* @param id 对象ID
*/
fun remove(id: ID)
/**
* 添加一个删除对象前执行监听器。
* @param key 监听器标志。
* @param func 监听方法。
*/
fun addBeforeRemoveListener(key: String, func: (id: ID) -> StatusMessage)
/**
* 移除一个删除对象前执行监听器。
* @param key 监听器标志。
*/
fun removeBeforeRemoveListener(key: String)
}

View File

@@ -1,78 +1,80 @@
package com.synebula.gaea.domain.service
import com.synebula.gaea.data.message.DataMessage
import com.synebula.gaea.data.message.StatusMessage
import com.synebula.gaea.data.serialization.IObjectMapper
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对象
* 依赖了IRepository仓储借口的服务实现类 GenericsService
* 该类依赖仓储接口 @see IGenericsRepository, 需要显式提供聚合根的class对象
*
* @param repository 仓储对象
* @param clazz 聚合根类对象
* @param deserializer 对象转换组件
* @param logger 日志组件
* @param repo 仓储对象
* @param mapper 对象转换组件
* @author alex
* @version 0.1
* @since 2020-05-17
*/
open class Service<TAggregateRoot : IAggregateRoot<ID>, ID>(
protected open var clazz: Class<TAggregateRoot>,
protected open var repository: IRepository,
protected open var deserializer: IObjectMapper,
override var logger: ILogger,
open class Service<TRoot : IAggregateRoot<ID>, ID>(
protected open var clazz: Class<TRoot>,
protected open var repo: IRepository<TRoot, ID>,
protected open var mapper: IObjectMapper,
) : IService<ID> {
/**
* 删除对象前执行监听器。
*/
protected val beforeRemoveListeners = mutableMapOf<String, (id: ID) -> StatusMessage>()
/**
* 添加一个删除对象前执行监听器。
* @param key 监听器标志。
* @param func 监听方法。
* 增加对象
*
* @param command 增加对象命令
*/
override fun addBeforeRemoveListener(key: String, func: (id: ID) -> StatusMessage) {
this.beforeRemoveListeners[key] = func
}
/**
* 移除一个删除对象前执行监听器。
* @param key 监听器标志。
*/
override fun removeBeforeRemoveListener(key: String) {
this.beforeRemoveListeners.remove(key)
}
override fun add(command: ICommand): DataMessage<ID> {
val msg = DataMessage<ID>()
val root = this.deserialize(command)
this.repository.add(root, this.clazz)
val root = this.map(command)
this.repo.add(root)
msg.data = root.id
return msg
}
override fun update(id: ID, command: ICommand) {
val root = this.deserialize(command)
root.id = id
this.repository.update(root, this.clazz)
/**
* 增加对象
*
* @param commands 增加对象命令列表
*/
override fun add(commands: List<ICommand>) {
val roots = commands.map { this.map(it) }
this.repo.add(roots)
}
/**
* 更新对象
*
* @param id 对象ID
* @param command 更新对象命令
*/
override fun update(id: ID, command: ICommand) {
val root = this.map(command)
root.id = id
this.repo.update(root)
}
/**
* 批量更新对象
*
* @param commands 更新对象命令列表
*/
override fun update(commands: List<ICommand>) {
val roots = commands.map { this.map(it) }
this.repo.update(roots)
}
/**
* 增加对象
* @param id 对象ID
*/
override fun remove(id: ID) {
val functions = this.beforeRemoveListeners.values
var msg: StatusMessage
for (func in functions) {
msg = func(id)
if (!msg.success()) {
throw IllegalStateException(msg.message)
}
}
this.repository.remove(id, this.clazz)
this.repo.remove(id)
}
/**
@@ -81,9 +83,9 @@ open class Service<TAggregateRoot : IAggregateRoot<ID>, ID>(
* @param command 需要转换的命令
* @return 聚合根
*/
protected open fun deserialize(command: ICommand): TAggregateRoot {
protected open fun map(command: ICommand): TRoot {
try {
return deserializer.deserialize(command, this.clazz)
return mapper.deserialize(command, this.clazz)
} catch (ex: Exception) {
throw RuntimeException("command not match aggregate root", ex)
}

View File

@@ -0,0 +1,16 @@
package com.synebula.gaea.domain.service
import com.synebula.gaea.domain.model.IAggregateRoot
import com.synebula.gaea.domain.repository.IRepository
import kotlin.reflect.KClass
/**
* 声明服务的依赖项,若服务没有实现类则可以根据依赖项自动组装服务。
*
* @param clazz 依赖的聚合根类型
* @param repo 依赖的[IRepository]类型
*/
annotation class ServiceDependency(
val clazz: KClass<out IAggregateRoot<*>>,
val repo: KClass<out IRepository<*, *>>,
)

View File

@@ -1,15 +1,14 @@
package com.synebula.gaea.domain.service
import com.synebula.gaea.data.message.DataMessage
import com.synebula.gaea.data.message.StatusMessage
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对象
* 依赖了IRepository仓储借口的服务实现类 GenericsService
* 该类依赖仓储接口 @see IGenericsRepository, 需要显式提供聚合根的class对象
*
* @param repository 仓储对象
* @param clazz 聚合根类对象
@@ -20,53 +19,41 @@ import com.synebula.gaea.log.ILogger
*/
open class SimpleService<TAggregateRoot : IAggregateRoot<ID>, ID>(
protected open var clazz: Class<TAggregateRoot>,
protected open var repository: IRepository,
protected open var repository: IRepository<TAggregateRoot, ID>,
override var logger: ILogger,
) : ISimpleService<TAggregateRoot, ID> {
/**
* 删除对象前执行监听器。
*/
protected val beforeRemoveListeners = mutableMapOf<String, (id: ID) -> StatusMessage>()
/**
* 添加一个删除对象前执行监听器。
* @param key 监听器标志。
* @param func 监听方法。
*/
override fun addBeforeRemoveListener(key: String, func: (id: ID) -> StatusMessage) {
this.beforeRemoveListeners[key] = func
}
/**
* 移除一个删除对象前执行监听器。
* @param key 监听器标志。
*/
override fun removeBeforeRemoveListener(key: String) {
this.beforeRemoveListeners.remove(key)
}
override fun add(root: TAggregateRoot): DataMessage<ID> {
val msg = DataMessage<ID>()
this.repository.add(root, this.clazz)
this.repository.add(root)
msg.data = root.id
return msg
}
override fun update(id: ID, root: TAggregateRoot) {
root.id = id
this.repository.update(root, this.clazz)
this.repository.update(root)
}
override fun remove(id: ID) {
val functions = this.beforeRemoveListeners.values
var msg: StatusMessage
for (func in functions) {
msg = func(id)
if (!msg.success()) {
throw IllegalStateException(msg.message)
}
}
this.repository.remove(id, this.clazz)
this.repository.remove(id)
}
/**
* 增加对象
*
* @param roots 增加对象命令列表
*/
override fun add(roots: List<TAggregateRoot>) {
this.repository.add(roots)
}
/**
* 批量更新对象
*
* @param roots 更新对象命令列表
*/
override fun update(roots: List<TAggregateRoot>) {
this.repository.update(roots)
}
}

View File

@@ -5,22 +5,27 @@ package com.synebula.gaea.query
*
* @author alex
*/
interface IQuery {
interface IQuery<TView, ID> {
/**
* 仓储的视图类
*/
var clazz: Class<TView>
/**
* 根据Key获取对象。
*
* @param id 对象Key。
* @return 视图结果
*/
fun <TView, ID> get(id: ID, clazz: Class<TView>): TView?
fun get(id: ID): TView?
/**
* 根据实体类条件查询所有符合条件记录
*
*`
* @param params 查询条件。
* @return 视图列表
*/
fun <TView> list(params: Map<String, Any>?, clazz: Class<TView>): List<TView>
fun list(params: Map<String, Any>?): List<TView>
/**
* 根据条件查询符合条件记录的数量
@@ -28,7 +33,7 @@ interface IQuery {
* @param params 查询条件。
* @return 数量
*/
fun <TView> count(params: Map<String, Any>?, clazz: Class<TView>): Int
fun count(params: Map<String, Any>?): Int
/**
* 根据实体类条件查询所有符合条件记录(分页查询)
@@ -36,7 +41,7 @@ interface IQuery {
* @param params 分页条件
* @return 分页数据
*/
fun <TView> paging(params: Params, clazz: Class<TView>): Page<TView>
fun paging(params: Params): Page<TView>
/**
* 查询条件范围内数据。
@@ -45,5 +50,5 @@ interface IQuery {
*
* @return 视图列表
*/
fun <TView> range(field: String, params: List<Any>, clazz: Class<TView>): List<TView>
fun range(field: String, params: List<Any>): List<TView>
}

View File

@@ -1,18 +1,18 @@
package com.synebula.gaea.query
/**
* 查询基接口
* 本接口泛型定义在类上方法中不需要显式提供聚合根的class对象class对象作为类的成员变量声明
* 查询基接口, 其中方法都指定了查询的视图类型
*
* @author alex
*/
interface ISpecificQuery<TView> {
interface IUniversalQuery {
/**
* 根据Key获取对象
*
* @param id 对象Key
* @return 视图结果
*/
fun <TView, ID> get(id: ID): TView?
fun <TView, ID> get(id: ID, clazz: Class<TView>): TView?
/**
* 根据实体类条件查询所有符合条件记录
@@ -20,7 +20,7 @@ interface ISpecificQuery<TView> {
* @param params 查询条件
* @return 视图列表
*/
fun <TView> list(params: Map<String, Any>?): List<TView>
fun <TView> list(params: Map<String, Any>?, clazz: Class<TView>): List<TView>
/**
* 根据条件查询符合条件记录的数量
@@ -28,7 +28,7 @@ interface ISpecificQuery<TView> {
* @param params 查询条件
* @return 数量
*/
fun <TView> count(params: Map<String, Any>?): Int
fun <TView> count(params: Map<String, Any>?, clazz: Class<TView>): Int
/**
* 根据实体类条件查询所有符合条件记录分页查询
@@ -36,7 +36,7 @@ interface ISpecificQuery<TView> {
* @param params 分页条件
* @return 分页数据
*/
fun <TView> paging(params: Params): Page<TView>
fun <TView> paging(params: Params, clazz: Class<TView>): Page<TView>
/**
* 查询条件范围内数据
@@ -45,5 +45,5 @@ interface ISpecificQuery<TView> {
*
* @return 视图列表
*/
fun <TView> range(field: String, params: List<Any>): List<TView>
fun <TView> range(field: String, params: List<Any>, clazz: Class<TView>): List<TView>
}

View File

@@ -1,4 +1,4 @@
package com.synebula.gaea.query.type
package com.synebula.gaea.query
enum class Operator {
/**

View File

@@ -1,4 +1,4 @@
package com.synebula.gaea.query.type
package com.synebula.gaea.query
/**
* class OrderType

View File

@@ -1,7 +1,5 @@
package com.synebula.gaea.query
import com.synebula.gaea.query.type.Order
/**
* class 分页参数信息
*

View File

@@ -1,3 +1,3 @@
package com.synebula.gaea.query.annotation
package com.synebula.gaea.query
annotation class Table(val name: String = "")

View File

@@ -1,6 +1,4 @@
package com.synebula.gaea.query.annotation
import com.synebula.gaea.query.type.Operator
package com.synebula.gaea.query
/**
* 字段注解规定字段的查询方式

View File

@@ -1,7 +1,5 @@
package com.synebula.gaea.reflect
import com.synebula.gaea.bus.SubscriberRegistry
object Types {
/**
* 获取类的所有父类型。
@@ -9,15 +7,15 @@ object Types {
fun supertypes(clazz: Class<*>): Set<Class<*>> {
val supertypes = mutableSetOf<Class<*>>()
supertypes.add(clazz)
if (clazz.superclass != null)
supertypes.addAll(SubscriberRegistry.flattenHierarchy(clazz.superclass))
if (clazz.interfaces.isNotEmpty()) {
supertypes.addAll(clazz.interfaces.map { SubscriberRegistry.flattenHierarchy(it) }.reduce { r, c ->
supertypes.addAll(clazz.interfaces.map { supertypes(it) }.reduce { r, c ->
val all = r.toMutableSet()
all.addAll(c)
all
})
}
if (clazz.superclass != null)
supertypes.addAll(supertypes(clazz.superclass))
return supertypes
}
}