重新命名类型和泛型相关的repo/srv/query

This commit is contained in:
2020-05-22 16:31:26 +08:00
parent f4db771261
commit 2d0de09816
24 changed files with 423 additions and 412 deletions

View File

@@ -1,7 +1,7 @@
package com.synebula.gaea.app
import com.synebula.gaea.app.cmd.ICommandApp
import com.synebula.gaea.app.query.IQueryGenericApp
import com.synebula.gaea.app.query.IQueryTypedApp
import com.synebula.gaea.data.serialization.json.IJsonSerializer
import com.synebula.gaea.domain.service.ICommand
import com.synebula.gaea.domain.service.IService
@@ -18,11 +18,12 @@ import javax.annotation.Resource
* @param logger 日志组件
*/
open class UnionApp<TCommand : ICommand, TView, TKey>(
override var name: String,
override var service: IService<TKey>?,
override var query: IQuery<TView, TKey>?,
override var logger: ILogger)
: ICommandApp<TCommand, TKey>, IQueryGenericApp<TView, TKey> {
override var name: String,
override var clazz: Class<TView>,
override var service: IService<TKey>?,
override var query: IQuery?,
override var logger: ILogger)
: ICommandApp<TCommand, TKey>, IQueryTypedApp<TView, TKey> {
@Resource
override var jsonSerializer: IJsonSerializer? = null

View File

@@ -2,13 +2,11 @@ package com.synebula.gaea.app
import com.synebula.gaea.app.cmd.ICommandApp
import com.synebula.gaea.app.query.IQueryGenericApp
import com.synebula.gaea.app.query.IQueryTypedApp
import com.synebula.gaea.data.serialization.json.IJsonSerializer
import com.synebula.gaea.domain.service.ICommand
import com.synebula.gaea.domain.service.IService
import com.synebula.gaea.log.ILogger
import com.synebula.gaea.query.IQuery
import com.synebula.gaea.query.IQueryTyped
import com.synebula.gaea.query.IGenericQuery
import javax.annotation.Resource
/**
@@ -19,13 +17,12 @@ import javax.annotation.Resource
* @param query 业务查询服务
* @param logger 日志组件
*/
open class UnionTypedApp<TCommand : ICommand, TView, TKey>(
override var name: String,
override var viewClass: Class<TView>,
override var service: IService<TKey>?,
override var query: IQueryTyped?,
override var logger: ILogger)
: ICommandApp<TCommand, TKey>, IQueryTypedApp<TView, TKey> {
open class UnionGenericApp<TCommand : ICommand, TView, TKey>(
override var name: String,
override var service: IService<TKey>?,
override var query: IGenericQuery<TView, TKey>?,
override var logger: ILogger
) : ICommandApp<TCommand, TKey>, IQueryGenericApp<TView, TKey> {
@Resource
override var jsonSerializer: IJsonSerializer? = null

View File

@@ -1,13 +1,9 @@
package com.synebula.gaea.app.query
import com.synebula.gaea.app.IApplication
import com.synebula.gaea.app.component.HttpMessage
import com.synebula.gaea.data.message.Status
import com.synebula.gaea.query.IQuery
import com.synebula.gaea.query.IGenericQuery
import com.synebula.gaea.query.PagingParam
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.PathVariable
import org.springframework.web.bind.annotation.RequestParam
/**
* 应用类接口提供实现Query服务的接口
@@ -21,7 +17,7 @@ interface IQueryGenericApp<TView, TKey> : IQueryApp<TView, TKey> {
/**
* 查询服务
*/
var query: IQuery<TView, TKey>?
var query: IGenericQuery<TView, TKey>?
/**

View File

@@ -1,9 +1,8 @@
package com.synebula.gaea.app.query
import com.synebula.gaea.app.IApplication
import com.synebula.gaea.app.component.HttpMessage
import com.synebula.gaea.data.message.Status
import com.synebula.gaea.query.IQueryTyped
import com.synebula.gaea.query.IQuery
import com.synebula.gaea.query.PagingParam
/**
@@ -14,26 +13,26 @@ import com.synebula.gaea.query.PagingParam
* @version 0.1
* @since 2020-05-15
*/
interface IQueryTypedApp<TView, TKey> : IApplication, IQueryApp<TView, TKey> {
interface IQueryTypedApp<TView, TKey> : IQueryApp<TView, TKey> {
/**
* 查询服务
*/
var query: IQueryTyped?
var query: IQuery?
/**
* 查询的View类型
*/
var viewClass: Class<TView>
var clazz: Class<TView>
override fun doGet(key: TKey): HttpMessage {
return this.doQuery("获取${this.name}数据失败") {
this.query!!.get(key, viewClass)
this.query!!.get(key, clazz)
}
}
override fun doList(params: Map<String, Any>): HttpMessage {
return this.doQuery("获取${this.name}列表数据失败") {
this.query!!.list(params, viewClass)
this.query!!.list(params, clazz)
}
}
@@ -41,7 +40,7 @@ interface IQueryTypedApp<TView, TKey> : IApplication, IQueryApp<TView, TKey> {
return this.doQuery("获取${this.name}分页数据[条数:$size,页码:$page]失败") {
val data = PagingParam(page, size)
data.parameters = params
this.query!!.paging(data, viewClass)
this.query!!.paging(data, clazz)
}
}

View File

@@ -1,17 +1,18 @@
package com.synebula.gaea.app.query
import com.synebula.gaea.log.ILogger
import com.synebula.gaea.query.IQuery
import com.synebula.gaea.query.IGenericQuery
/**
* 联合服务同时实现了ICommandApp和IQueryApp接口
*
* @param name 业务名称
* @param query 业务查询服务
* @param genericQuery 业务查询服务
* @param logger 日志组件
*/
open class QueryGenericApp<TView, TKey>(
override var name: String,
override var query: IQuery<TView, TKey>?,
override var logger: ILogger) : IQueryGenericApp<TView, TKey> {
override var name: String,
override var query: IGenericQuery<TView, TKey>?,
override var logger: ILogger
) : IQueryGenericApp<TView, TKey> {
}

View File

@@ -1,7 +1,7 @@
package com.synebula.gaea.app.query
import com.synebula.gaea.log.ILogger
import com.synebula.gaea.query.IQueryTyped
import com.synebula.gaea.query.IQuery
/**
* 联合服务同时实现了ICommandApp和IQueryApp接口
@@ -11,8 +11,8 @@ import com.synebula.gaea.query.IQueryTyped
* @param logger 日志组件
*/
open class QueryTypedApp<TView, TKey>(
override var name: String,
override var viewClass: Class<TView>,
override var query: IQueryTyped?,
override var logger: ILogger) : IQueryTypedApp<TView, TKey> {
override var name: String,
override var clazz: Class<TView>,
override var query: IQuery?,
override var logger: ILogger) : IQueryTypedApp<TView, TKey> {
}

View File

@@ -41,7 +41,7 @@ fun Query.where(params: Map<String, Any>?): Query {
*
* @param id 业务ID
*/
fun <TKey> whereId(id: TKey): Query = Query(Criteria("_id").isEqualTo(id))
fun <TKey> whereId(id: TKey): Query = Query.query(Criteria.where("_id").`is`(id))
/**
* 获取排序对象

View File

@@ -0,0 +1,130 @@
package com.synebula.gaea.mongo.query
import com.synebula.gaea.extension.fields
import com.synebula.gaea.extension.firstCharLowerCase
import com.synebula.gaea.log.ILogger
import com.synebula.gaea.mongo.order
import com.synebula.gaea.mongo.select
import com.synebula.gaea.mongo.where
import com.synebula.gaea.mongo.whereId
import com.synebula.gaea.query.IGenericQuery
import com.synebula.gaea.query.PagingData
import com.synebula.gaea.query.PagingParam
import org.springframework.data.mongodb.core.MongoTemplate
import org.springframework.data.mongodb.core.query.Query
/**
* 实现IQuery的Mongo查询类
* @param template MongoRepo对象
* @param logger 日志组件
*/
open class MongoGenericQuery<TView>(var template: MongoTemplate, var logger: ILogger? = null) : IGenericQuery<TView, String> {
/**
* 查询的对象类
*/
var clazz: Class<TView>? = null
private var _collection = ""
/**
* 查询的集合名称
*/
var collection: String
set(value) {
this._collection = value
}
get() = if (this._collection.isNotEmpty())
this._collection
else {
if (this.clazz != null)
this.clazz!!.simpleName.removeSuffix("View").firstCharLowerCase()
else
""
}
/**
* 构造方法
*
* @param clazz 视图对象类型
* @param query MongoRepo对象
*/
constructor(clazz: Class<TView>, query: MongoTemplate)
: this(query) {
this.clazz = clazz
}
/**
* 构造方法
*
* @param collection 查询的集合名称
* @param query MongoRepo对象
*/
constructor(collection: String, query: MongoTemplate)
: this(query) {
this.collection = collection
}
/**
* 构造方法
*
* @param collection 查询的集合名称
* @param clazz 视图对象类型
* @param query MongoRepo对象
*/
constructor(collection: String, clazz: Class<TView>, query: MongoTemplate)
: this(clazz, query) {
this.collection = collection
}
override fun list(params: Map<String, Any>?): List<TView> {
this.check()
return if (this.clazz != null) {
val fields = this.clazz!!.fields()
val query = Query()
query.select(fields.toTypedArray())
query.where(params)
this.template.find(query, this.clazz!!, this.collection)
} else listOf()
}
override fun count(params: Map<String, Any>?): Int {
this.check()
return if (this.clazz != null) {
val query = Query()
this.template.count(query.where(params), this.collection).toInt()
} else 0
}
override fun paging(params: PagingParam): PagingData<TView> {
this.check()
return if (this.clazz != null) {
val query = Query()
val fields = this.clazz!!.fields()
val result = PagingData<TView>(params.page, params.size)
query.where(params.parameters)
result.total = this.count(params.parameters)
query.select(fields.toTypedArray())
query.with(order(params.orderBy))
query.skip(params.index).limit(params.size)
result.data = this.template.find(query, this.clazz!!, this.collection)
result
} else PagingData(1, 10)
}
override fun get(key: String): TView? {
this.check()
return if (this.clazz != null) {
val view = this.template.findOne(whereId(key), this.clazz!!, this.collection)
view
} else null
}
protected fun check() {
if (this.clazz == null)
throw RuntimeException("[${this.javaClass.name}] 没有声明查询View的类型")
if (this._collection.isEmpty())
this.logger?.warn(this, "查询集合参数[collection]值为空, 尝试使用View<${this.clazz?.name}>名称解析集合")
}
}

View File

@@ -12,119 +12,73 @@ import com.synebula.gaea.query.PagingData
import com.synebula.gaea.query.PagingParam
import org.springframework.data.mongodb.core.MongoTemplate
import org.springframework.data.mongodb.core.query.Query
import java.lang.RuntimeException
/**
* 实现IQuery的Mongo查询类
* @param template MongoRepo对象
* @param logger 日志组件
* @param repo MongoRepo对象
*/
open class MongoQuery<TView>(var template: MongoTemplate, var logger: ILogger? = null) : IQuery<TView, String> {
/**
* 查询的对象类
*/
var clazz: Class<TView>? = null
private var _collection = ""
open class MongoQuery(var repo: MongoTemplate, var logger: ILogger? = null) : IQuery {
/**
* 查询的集合名称
*
* 若没有为成员变量collection赋值会尝试使用View名称解析集合名称。
* 规则为移除View后缀并首字母小写此时使用
*/
var collection: String
set(value) {
this._collection = value
}
get() = if (this._collection.isNotEmpty())
this._collection
else {
if (this.clazz != null)
this.clazz!!.simpleName.removeSuffix("View").firstCharLowerCase()
else
""
}
var collection: String = ""
/**
* 构造方法
*
* @param clazz 视图对象类型
* @param query MongoRepo对象
* 使用View解析是collection时是否校验存在默认不校验
*/
constructor(clazz: Class<TView>, query: MongoTemplate)
: this(query) {
this.clazz = clazz
var validViewCollection = false
override fun <TView> list(params: Map<String, Any>?, clazz: Class<TView>): List<TView> {
val fields = clazz.fields()
val query = Query()
query.where(params)
query.select(fields.toTypedArray())
return this.repo.find(query, clazz, this.collection(clazz))
}
override fun <TView> count(params: Map<String, Any>?, clazz: Class<TView>): Int {
val query = Query()
return this.repo.count(query.where(params), this.collection(clazz)).toInt()
}
override fun <TView> paging(params: PagingParam, clazz: Class<TView>): PagingData<TView> {
val fields = clazz.fields()
val result = PagingData<TView>(1, 10)
result.size = params.size
result.page = params.page
val query = Query()
query.where(params.parameters)
result.total = this.count(params.parameters, clazz)
query.select(fields.toTypedArray())
query.with(order(params.orderBy))
query.skip(params.index).limit(params.size)
result.data = this.repo.find(query, clazz, this.collection(clazz))
return result
}
override fun <TView, TKey> get(key: TKey, clazz: Class<TView>): TView? {
return this.repo.findOne(whereId(key), clazz, this.collection(clazz))
}
/**
* 构造方法
*
* @param collection 查询的集合名称
* @param query MongoRepo对象
* 获取collection
*/
constructor(collection: String, query: MongoTemplate)
: this(query) {
this.collection = collection
protected fun <TView> collection(clazz: Class<TView>): String {
return if (this.collection.isEmpty()) {
this.logger?.info(this, "查询集合参数[collection]值为空, 尝试使用View<${clazz.name}>名称解析集合")
val collection = clazz.simpleName.removeSuffix("View").firstCharLowerCase()
if (!validViewCollection || this.repo.collectionExists(collection))
collection
else {
throw RuntimeException("找不到名为[$collection]的集合")
}
} else
this.collection
}
/**
* 构造方法
*
* @param collection 查询的集合名称
* @param clazz 视图对象类型
* @param query MongoRepo对象
*/
constructor(collection: String, clazz: Class<TView>, query: MongoTemplate)
: this(clazz, query) {
this.collection = collection
}
override fun list(params: Map<String, Any>?): List<TView> {
this.check()
return if (this.clazz != null) {
val fields = this.clazz!!.fields()
val query = Query()
query.select(fields.toTypedArray())
query.where(params)
this.template.find(query, this.clazz!!, this.collection)
} else listOf()
}
override fun count(params: Map<String, Any>?): Int {
this.check()
return if (this.clazz != null) {
val query = Query()
this.template.count(query.where(params), this.collection).toInt()
} else 0
}
override fun paging(params: PagingParam): PagingData<TView> {
this.check()
return if (this.clazz != null) {
val query = Query()
val fields = this.clazz!!.fields()
val result = PagingData<TView>(params.page, params.size)
query.where(params.parameters)
result.total = this.count(params.parameters)
query.select(fields.toTypedArray())
query.with(order(params.orderBy))
query.skip(params.index).limit(params.size)
result.data = this.template.find(query, this.clazz!!, this.collection)
result
} else PagingData(1, 10)
}
override fun get(key: String): TView? {
this.check()
return if (this.clazz != null) {
val view = this.template.findOne(whereId(key), this.clazz!!, this.collection)
view
} else null
}
protected fun check() {
if (this.clazz == null)
throw RuntimeException("[${this.javaClass.name}] 没有声明查询View的类型")
if (this._collection.isEmpty())
this.logger?.warn(this, "查询集合参数[collection]值为空, 尝试使用View<${this.clazz?.name}>名称解析集合")
}
}

View File

@@ -1,84 +0,0 @@
package com.synebula.gaea.mongo.query
import com.synebula.gaea.extension.fields
import com.synebula.gaea.extension.firstCharLowerCase
import com.synebula.gaea.log.ILogger
import com.synebula.gaea.mongo.order
import com.synebula.gaea.mongo.select
import com.synebula.gaea.mongo.where
import com.synebula.gaea.mongo.whereId
import com.synebula.gaea.query.IQueryTyped
import com.synebula.gaea.query.PagingData
import com.synebula.gaea.query.PagingParam
import org.springframework.data.mongodb.core.MongoTemplate
import org.springframework.data.mongodb.core.query.Query
import java.lang.RuntimeException
/**
* 实现IQuery的Mongo查询类
* @param repo MongoRepo对象
*/
open class MongoQueryTyped(var repo: MongoTemplate, var logger: ILogger? = null) : IQueryTyped {
/**
* 查询的集合名称
*
* 若没有为成员变量collection赋值会尝试使用View名称解析集合名称。
* 规则为移除View后缀并首字母小写此时使用
*/
var collection: String = ""
/**
* 使用View解析是collection时是否校验存在默认不校验
*/
var validViewCollection = false
override fun <TView> list(params: Map<String, Any>?, clazz: Class<TView>): List<TView> {
val fields = clazz.fields()
val query = Query()
query.where(params)
query.select(fields.toTypedArray())
return this.repo.find(query, clazz, this.collection(clazz))
}
override fun <TView> count(params: Map<String, Any>?, clazz: Class<TView>): Int {
val query = Query()
return this.repo.count(query.where(params), this.collection(clazz)).toInt()
}
override fun <TView> paging(params: PagingParam, clazz: Class<TView>): PagingData<TView> {
val fields = clazz.fields()
val result = PagingData<TView>(1, 10)
result.size = params.size
result.page = params.page
val query = Query()
query.where(params.parameters)
result.total = this.count(params.parameters, clazz)
query.select(fields.toTypedArray())
query.with(order(params.orderBy))
query.skip(params.index).limit(params.size)
result.data = this.repo.find(query, clazz, this.collection(clazz))
return result
}
override fun <TView, TKey> get(key: TKey, clazz: Class<TView>): TView? {
return this.repo.findOne(whereId(key), clazz, this.collection(clazz))
}
/**
* 获取collection
*/
protected fun <TView> collection(clazz: Class<TView>): String {
return if (this.collection.isEmpty()) {
this.logger?.info(this, "查询集合参数[collection]值为空, 尝试使用View<${clazz.name}>名称解析集合")
val collection = clazz.simpleName.removeSuffix("View").firstCharLowerCase()
if (!validViewCollection || this.repo.collectionExists(collection))
collection
else {
throw RuntimeException("找不到名为[$collection]的集合")
}
} else
this.collection
}
}

View File

@@ -0,0 +1,51 @@
package com.synebula.gaea.mongo.repository
import com.synebula.gaea.domain.model.IAggregateRoot
import com.synebula.gaea.domain.repository.IGenericRepository
import com.synebula.gaea.mongo.whereId
import org.springframework.data.mongodb.core.MongoTemplate
/**
* 实现IAggregateRoot的mongo仓储类
* @param repo MongoRepo对象
*/
class MongoGenericRepository<TAggregateRoot : IAggregateRoot<String>>(private var repo: MongoTemplate) :
IGenericRepository<TAggregateRoot, String> {
/**
* 仓储的对象类
*/
override var clazz: Class<TAggregateRoot>? = null
/**
* 构造
* @param clazz 仓储Domain对象
* @param repo MongoRepo对象
*/
constructor(clazz: Class<TAggregateRoot>, repo: MongoTemplate) : this(repo) {
this.clazz = clazz
}
override fun add(obj: TAggregateRoot) {
this.repo.save(obj)
}
override fun update(obj: TAggregateRoot) {
this.repo.save(obj)
}
override fun remove(id: String) {
this.repo.remove(whereId(id), this.clazz!!)
}
override fun get(id: String): TAggregateRoot {
return this.repo.findOne(whereId(id), clazz!!) as TAggregateRoot
}
override fun <TAggregateRoot : IAggregateRoot<TKey>, TKey> get(
id: TKey,
clazz: Class<TAggregateRoot>
): TAggregateRoot {
return this.repo.findOne(whereId(id.toString()), clazz) as TAggregateRoot
}
}

View File

@@ -6,43 +6,30 @@ import com.synebula.gaea.mongo.whereId
import org.springframework.data.mongodb.core.MongoTemplate
/**
* 实现IAggregateRoot的mongo仓储类
* 实现ITypedRepository的mongo仓储类
* @param repo MongoRepo对象
*/
class MongoRepository<TAggregateRoot : IAggregateRoot<String>>(private var repo: MongoTemplate)
: IRepository<TAggregateRoot, String> {
open class MongoRepository(private var repo: MongoTemplate) : IRepository {
/**
* 仓储的对象类
*/
override var clazz: Class<TAggregateRoot>? = null
/**
* 构造
* @param clazz 仓储Domain对象
* @param repo MongoRepo对象
*/
constructor(clazz: Class<TAggregateRoot>, repo: MongoTemplate) : this(repo) {
this.clazz = clazz
override fun <TAggregateRoot : IAggregateRoot<TKey>, TKey> remove(id: TKey, clazz: Class<TAggregateRoot>) {
this.repo.remove(whereId(id), clazz)
}
override fun add(obj: TAggregateRoot) {
override fun <TAggregateRoot : IAggregateRoot<TKey>, TKey> get(
id: TKey,
clazz: Class<TAggregateRoot>
): TAggregateRoot {
return this.repo.findOne(whereId(id), clazz) as TAggregateRoot
}
override fun <TAggregateRoot : IAggregateRoot<TKey>, TKey> update(
obj: TAggregateRoot,
clazz: Class<TAggregateRoot>
) {
this.repo.save(obj)
}
override fun update(obj: TAggregateRoot) {
override fun <TAggregateRoot : IAggregateRoot<TKey>, TKey> add(obj: TAggregateRoot, clazz: Class<TAggregateRoot>) {
this.repo.save(obj)
}
override fun remove(id: String) {
this.repo.remove(whereId(id), this.clazz!!)
}
override fun get(id: String): TAggregateRoot {
return this.repo.findOne(whereId(id), clazz!!) as TAggregateRoot
}
override fun <TAggregateRoot : IAggregateRoot<TKey>, TKey> get(id: TKey, clazz: Class<TAggregateRoot>): TAggregateRoot {
return this.repo.findOne(whereId(id.toString()), clazz) as TAggregateRoot
}
}

View File

@@ -1,30 +0,0 @@
package com.synebula.gaea.mongo.repository
import com.synebula.gaea.domain.model.IAggregateRoot
import com.synebula.gaea.domain.repository.IRepositoryTyped
import com.synebula.gaea.mongo.whereId
import org.springframework.data.mongodb.core.MongoTemplate
/**
* 实现ITypedRepository的mongo仓储类
* @param repo MongoRepo对象
*/
open class MongoRepositoryTyped(private var repo: MongoTemplate)
: IRepositoryTyped {
override fun <TAggregateRoot : IAggregateRoot<TKey>, TKey> remove(id: TKey, clazz: Class<TAggregateRoot>) {
this.repo.remove(whereId(id), clazz)
}
override fun <TAggregateRoot : IAggregateRoot<TKey>, TKey> get(id: TKey, clazz: Class<TAggregateRoot>): TAggregateRoot {
return this.repo.findOne(whereId(id), clazz) as TAggregateRoot
}
override fun <TAggregateRoot : IAggregateRoot<TKey>, TKey> update(obj: TAggregateRoot, clazz: Class<TAggregateRoot>) {
this.repo.save(obj)
}
override fun <TAggregateRoot : IAggregateRoot<TKey>, TKey> add(obj: TAggregateRoot, clazz: Class<TAggregateRoot>) {
this.repo.save(obj)
}
}

View File

@@ -8,7 +8,7 @@ import com.synebula.gaea.domain.model.complex.IComplexAggregateRoot
* @param <TAggregateRoot> this T is the parameter
* @author alex
*/
interface IRepositoryComplex<TAggregateRoot : IComplexAggregateRoot<TKey, TSecond>, TKey, TSecond> {
interface IComplexRepository<TAggregateRoot : IComplexAggregateRoot<TKey, TSecond>, TKey, TSecond> {
/**
* 插入单个对象

View File

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

View File

@@ -3,27 +3,17 @@ package com.synebula.gaea.domain.repository
import com.synebula.gaea.domain.model.IAggregateRoot
/**
* 继承本接口表示对象为仓储类。
* 定义了提供增删改的仓储接口。
* 本接口泛型定义在类上,需要显式提供聚合根的class对象class对象作为类的成员变量声明。
*
* @param <TAggregateRoot> this T is the parameter
* @author alex
* 本接口泛型放置到方法上,需要显式提供聚合根的class对象
*/
interface IRepository<TAggregateRoot : IAggregateRoot<TKey>, TKey> {
/**
* 仓储的对象类
*/
var clazz: Class<TAggregateRoot>?
interface IRepository {
/**
* 插入单个对象。
*
* @param obj 需要插入的对象。
* @return 返回原对象如果对象ID为自增则补充自增ID。
*/
fun add(obj: TAggregateRoot)
fun <TAggregateRoot : IAggregateRoot<TKey>, TKey> add(obj: TAggregateRoot, clazz: Class<TAggregateRoot>)
/**
* 更新对象。
@@ -31,23 +21,15 @@ interface IRepository<TAggregateRoot : IAggregateRoot<TKey>, TKey> {
* @param obj 需要更新的对象。
* @return
*/
fun update(obj: TAggregateRoot)
fun <TAggregateRoot : IAggregateRoot<TKey>, TKey> update(obj: TAggregateRoot, clazz: Class<TAggregateRoot>)
/**
* 通过id删除该条数据
*
* @param id
* @return
* @param id id
* @param clazz 操作数据的类型
*/
fun remove(id: TKey)
/**
* 根据ID获取对象。
*
* @param id 对象ID。
* @return
*/
fun get(id: TKey): TAggregateRoot
fun <TAggregateRoot : IAggregateRoot<TKey>, TKey> remove(id: TKey, clazz: Class<TAggregateRoot>)
/**
* 根据ID获取对象。
@@ -56,6 +38,5 @@ interface IRepository<TAggregateRoot : IAggregateRoot<TKey>, TKey> {
* @param clazz 操作数据的类型
* @return 聚合根
*/
fun <T : IAggregateRoot<TKey>, TKey> get(id: TKey, clazz: Class<T>): T
fun <TAggregateRoot : IAggregateRoot<TKey>, TKey> get(id: TKey, clazz: Class<TAggregateRoot>): TAggregateRoot
}

View File

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

View File

@@ -3,7 +3,7 @@ package com.synebula.gaea.domain.service
import com.synebula.gaea.data.IObjectConverter
import com.synebula.gaea.data.message.Message
import com.synebula.gaea.domain.model.complex.IComplexAggregateRoot
import com.synebula.gaea.domain.repository.IRepositoryComplex
import com.synebula.gaea.domain.repository.IComplexRepository
import com.synebula.gaea.log.ILogger
/**
@@ -13,10 +13,12 @@ import com.synebula.gaea.log.ILogger
* @version 0.1
* @since 2020-05-15
*/
open class ServiceComplex<TAggregateRoot : IComplexAggregateRoot<TKey, TSecond>, TKey, TSecond>
(var logger: ILogger, protected var repository: IRepositoryComplex<TAggregateRoot, TKey, TSecond>,
protected var converter: IObjectConverter, protected var aggregateRootClass: Class<TAggregateRoot>)
: IServiceComplex<TKey, TSecond> {
open class ComplexService<TAggregateRoot : IComplexAggregateRoot<TKey, TSecond>, TKey, TSecond>(
protected var clazz: Class<TAggregateRoot>,
protected var repository: IComplexRepository<TAggregateRoot, TKey, TSecond>,
protected var converter: IObjectConverter,
override var logger: ILogger
) : IComplexService<TKey, TSecond> {
override fun add(command: ICommand): Message<Pair<TKey, TSecond>> {
val msg = Message<Pair<TKey, TSecond>>()
@@ -45,7 +47,7 @@ open class ServiceComplex<TAggregateRoot : IComplexAggregateRoot<TKey, TSecond>,
*/
protected fun convert(command: ICommand): TAggregateRoot {
try {
return converter.convert(command, aggregateRootClass)
return converter.convert(command, clazz)
} catch (ex: Exception) {
throw RuntimeException("command not match aggregate root", ex)
}

View File

@@ -3,32 +3,36 @@ package com.synebula.gaea.domain.service
import com.synebula.gaea.data.IObjectConverter
import com.synebula.gaea.data.message.Message
import com.synebula.gaea.domain.model.IAggregateRoot
import com.synebula.gaea.domain.repository.IRepositoryTyped
import com.synebula.gaea.domain.repository.IGenericRepository
import com.synebula.gaea.log.ILogger
/**
* 依赖了IRepositoryTyped仓储借口的服务实现类 ServiceTyped
* 该类依赖仓储接口 @see IRepositoryTyped ,需要显式提供聚合根的class对象
* class FlatService
*
* @param repository 仓储对象
* @param rootClass 聚合根类对象
* @param clazz 聚合根类对象
* @param converter 对象转换组件
* @param logger 日志组件
* @author alex
* @version 0.1
* @since 2020-05-17
* @since 2020-05-15
*/
open class ServiceTyped<TAggregateRoot : IAggregateRoot<TKey>, TKey>(
protected var rootClass: Class<TAggregateRoot>,
protected var repository: IRepositoryTyped,
protected var converter: IObjectConverter,
override var logger: ILogger) : IService<TKey> {
open class GenericService<TAggregateRoot : IAggregateRoot<TKey>, TKey>(
protected var clazz: Class<TAggregateRoot>,
protected var repository: IGenericRepository<TAggregateRoot, TKey>,
protected var converter: IObjectConverter,
override var logger: ILogger
) : IService<TKey> {
init {
this.repository.clazz = clazz
}
override fun add(command: ICommand): Message<TKey> {
val msg = Message<TKey>()
val root = this.convert(command)
this.repository.add(root, rootClass)
this.repository.add(root)
msg.data = root.id
return msg
}
@@ -36,24 +40,29 @@ open class ServiceTyped<TAggregateRoot : IAggregateRoot<TKey>, TKey>(
override fun update(key: TKey, command: ICommand) {
val root = this.convert(command)
root.id = key
this.repository.update(root, rootClass)
this.repository.update(root)
}
override fun remove(key: TKey) {
this.repository.remove(key, rootClass)
this.repository.remove(key)
}
fun get(key: TKey): TAggregateRoot {
return this.repository.get(key)
}
/**
* 转换ICommand类型到聚合根类型默认实现根据需要进行覆写
*
* @param command 需要转换的命令
* @return 聚合根
* @param command
* @return
*/
protected fun convert(command: ICommand): TAggregateRoot {
try {
return converter.convert(command, rootClass)
return converter.convert(command, this.clazz)
} catch (ex: Exception) {
throw RuntimeException("command not match aggregate root", ex)
}
}
}

View File

@@ -1,6 +1,7 @@
package com.synebula.gaea.domain.service
import com.synebula.gaea.data.message.Message
import com.synebula.gaea.log.ILogger
/**
* class IFlatService
@@ -9,7 +10,11 @@ import com.synebula.gaea.data.message.Message
* @version 0.1
* @since 2020-05-15
*/
interface IServiceComplex<TKey, TSecond> {
interface IComplexService<TKey, TSecond> {
/**
* 日志组件
*/
var logger: ILogger
fun add(command: ICommand): Message<Pair<TKey, TSecond>>

View File

@@ -13,7 +13,7 @@ import com.synebula.gaea.log.ILogger
*/
interface IService<TKey> {
/**
* 日志组件。若无日志组件则默认为NullLogger对象不会为null。
* 日志组件。
*/
var logger: ILogger

View File

@@ -8,30 +8,28 @@ import com.synebula.gaea.log.ILogger
/**
* class FlatService
* 依赖了IRepositoryTyped仓储借口的服务实现类 ServiceTyped
* 该类依赖仓储接口 @see IRepositoryTyped ,需要显式提供聚合根的class对象
*
* @param repository 仓储对象
* @param rootClass 聚合根类对象
* @param clazz 聚合根类对象
* @param converter 对象转换组件
* @param logger 日志组件
* @author alex
* @version 0.1
* @since 2020-05-15
* @since 2020-05-17
*/
open class Service<TAggregateRoot : IAggregateRoot<TKey>, TKey>(
protected var rootClass: Class<TAggregateRoot>,
protected var repository: IRepository<TAggregateRoot, TKey>,
protected var converter: IObjectConverter,
override var logger: ILogger) : IService<TKey> {
init {
this.repository.clazz = rootClass
}
protected var clazz: Class<TAggregateRoot>,
protected var repository: IRepository,
protected var converter: IObjectConverter,
override var logger: ILogger
) : IService<TKey> {
override fun add(command: ICommand): Message<TKey> {
val msg = Message<TKey>()
val root = this.convert(command)
this.repository.add(root)
this.repository.add(root, this.clazz)
msg.data = root.id
return msg
}
@@ -39,29 +37,24 @@ open class Service<TAggregateRoot : IAggregateRoot<TKey>, TKey>(
override fun update(key: TKey, command: ICommand) {
val root = this.convert(command)
root.id = key
this.repository.update(root)
this.repository.update(root, this.clazz)
}
override fun remove(key: TKey) {
this.repository.remove(key)
}
fun get(key: TKey): TAggregateRoot {
return this.repository.get(key)
this.repository.remove(key, this.clazz)
}
/**
* 转换ICommand类型到聚合根类型默认实现根据需要进行覆写。
*
* @param command
* @return
* @param command 需要转换的命令
* @return 聚合根
*/
protected fun convert(command: ICommand): TAggregateRoot {
try {
return converter.convert(command, rootClass)
return converter.convert(command, this.clazz)
} catch (ex: Exception) {
throw RuntimeException("command not match aggregate root", ex)
}
}
}

View File

@@ -1,18 +1,20 @@
package com.synebula.gaea.query
/**
* 查询基接口, 其中方法都指定了查询的视图类型
* 查询基接口
*
* @author alex
*/
interface IQueryTyped {
interface IGenericQuery<TView, TKey> {
/**
* 根据Key获取对象
*
* @param key 对象Key
* @return
*/
fun <TView, TKey> get(key: TKey, clazz: Class<TView>): TView?
fun get(key: TKey): TView?
/**
* 根据实体类条件查询所有符合条件记录
@@ -20,7 +22,7 @@ interface IQueryTyped {
* @param params 查询条件
* @return list
*/
fun <TView> list(params: Map<String, Any>?, clazz: Class<TView>): List<TView>
fun list(params: Map<String, Any>?): List<TView>
/**
* 根据条件查询符合条件记录的数量
@@ -28,7 +30,7 @@ interface IQueryTyped {
* @param params 查询条件
* @return int
*/
fun <TView> count(params: Map<String, Any>?, clazz: Class<TView>): Int
fun count(params: Map<String, Any>?): Int
/**
* 根据实体类条件查询所有符合条件记录分页查询
@@ -36,5 +38,5 @@ interface IQueryTyped {
* @param params 分页条件
* @return 分页数据
*/
fun <TView> paging(params: PagingParam, clazz: Class<TView>): PagingData<TView>
fun paging(params: PagingParam): PagingData<TView>
}

View File

@@ -1,20 +1,18 @@
package com.synebula.gaea.query
/**
* 查询基接口。
* 查询基接口, 其中方法都指定了查询的视图类型
*
* @author alex
*/
interface IQuery<TView, TKey> {
interface IQuery {
/**
* 根据Key获取对象。
*
* @param key 对象Key。
* @return
*/
fun get(key: TKey): TView?
fun <TView, TKey> get(key: TKey, clazz: Class<TView>): TView?
/**
* 根据实体类条件查询所有符合条件记录
@@ -22,7 +20,7 @@ interface IQuery<TView, TKey> {
* @param params 查询条件。
* @return list
*/
fun list(params: Map<String, Any>?): List<TView>
fun <TView> list(params: Map<String, Any>?, clazz: Class<TView>): List<TView>
/**
* 根据条件查询符合条件记录的数量
@@ -30,7 +28,7 @@ interface IQuery<TView, TKey> {
* @param params 查询条件。
* @return int
*/
fun count(params: Map<String, Any>?): Int
fun <TView> count(params: Map<String, Any>?, clazz: Class<TView>): Int
/**
* 根据实体类条件查询所有符合条件记录(分页查询)
@@ -38,5 +36,5 @@ interface IQuery<TView, TKey> {
* @param params 分页条件
* @return 分页数据
*/
fun paging(params: PagingParam): PagingData<TView>
fun <TView> paging(params: PagingParam, clazz: Class<TView>): PagingData<TView>
}