1.1.2 优化代码

This commit is contained in:
2022-08-19 17:00:09 +08:00
parent b09e0c0f03
commit 3c23c26a64
8 changed files with 68 additions and 101 deletions

View File

@@ -13,7 +13,7 @@
*/
package com.synebula.gaea.bus
import com.synebula.gaea.reflect.Types
import com.synebula.gaea.reflect.supertypes
import java.lang.reflect.Method
import java.util.concurrent.ConcurrentHashMap
import java.util.concurrent.CopyOnWriteArraySet
@@ -184,7 +184,7 @@ open class SubscriberRegistry<T : Any>(private val bus: IBus<T>) {
fun flattenHierarchy(clazz: Class<*>): Set<Class<*>> {
var supertypes = flattenHierarchyCache[clazz]
if (supertypes == null) {
supertypes = Types.supertypes(clazz)
supertypes = clazz.supertypes()
flattenHierarchyCache[clazz] = supertypes
}
return supertypes

View File

@@ -11,7 +11,7 @@ import com.synebula.gaea.domain.repository.IRepository
* 该类依赖仓储接口 @see IGenericsRepository, 需要显式提供聚合根的class对象
*
* @param clazz 聚合根类对象
* @param repo 仓储对象
* @param repository 仓储对象
* @param mapper 对象转换组件
* @author alex
* @version 0.1
@@ -19,11 +19,10 @@ import com.synebula.gaea.domain.repository.IRepository
*/
open class Service<TRoot : IAggregateRoot<ID>, ID>(
protected open var clazz: Class<TRoot>,
protected open var repo: IRepository<TRoot, ID>,
protected open var repository: IRepository<TRoot, ID>,
protected open var mapper: IObjectMapper,
) : IService<ID> {
/**
* 增加对象
*
@@ -32,7 +31,7 @@ open class Service<TRoot : IAggregateRoot<ID>, ID>(
override fun add(command: ICommand): DataMessage<ID> {
val msg = DataMessage<ID>()
val root = this.map(command)
this.repo.add(root)
this.repository.add(root)
msg.data = root.id
return msg
}
@@ -44,7 +43,7 @@ open class Service<TRoot : IAggregateRoot<ID>, ID>(
*/
override fun add(commands: List<ICommand>) {
val roots = commands.map { this.map(it) }
this.repo.add(roots)
this.repository.add(roots)
}
/**
@@ -56,7 +55,7 @@ open class Service<TRoot : IAggregateRoot<ID>, ID>(
override fun update(id: ID, command: ICommand) {
val root = this.map(command)
root.id = id
this.repo.update(root)
this.repository.update(root)
}
/**
@@ -66,7 +65,7 @@ open class Service<TRoot : IAggregateRoot<ID>, ID>(
*/
override fun update(commands: List<ICommand>) {
val roots = commands.map { this.map(it) }
this.repo.update(roots)
this.repository.update(roots)
}
/**
@@ -74,7 +73,7 @@ open class Service<TRoot : IAggregateRoot<ID>, ID>(
* @param id 对象ID
*/
override fun remove(id: ID) {
this.repo.remove(id)
this.repository.remove(id)
}
/**

View File

@@ -1,21 +1,28 @@
package com.synebula.gaea.reflect
object Types {
/**
* 获取类的所有父类型。
*/
fun supertypes(clazz: Class<*>): Set<Class<*>> {
val supertypes = mutableSetOf<Class<*>>()
supertypes.add(clazz)
if (clazz.interfaces.isNotEmpty()) {
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
import java.lang.reflect.ParameterizedType
/**
* 获取类的所有父类型。
*/
fun Class<*>.supertypes(): Set<Class<*>> {
val supertypes = mutableSetOf<Class<*>>()
supertypes.add(this)
if (this.interfaces.isNotEmpty()) {
supertypes.addAll(this.interfaces.map { it.supertypes() }.reduce { r, c ->
val all = r.toMutableSet()
all.addAll(c)
all
})
}
}
if (this.superclass != null)
supertypes.addAll(this.superclass.supertypes())
return supertypes
}
fun Class<*>.getGenericInterface(interfaceClazz: Class<*>): ParameterizedType? {
val type = this.genericInterfaces.find { it.typeName.startsWith(interfaceClazz.typeName) }
return if (type == null) null
else type as ParameterizedType
}