0.3.0 完成可以开发使用的库

This commit is contained in:
2020-05-17 22:08:54 +08:00
parent 0857b7194b
commit 5a77de1666
36 changed files with 1058 additions and 191 deletions

View File

@@ -1,3 +1,5 @@
package com.synebula.gaea.domain.model
abstract class AggregateRoot<TKey> : Entity<TKey>(), IAggregateRoot<TKey>
abstract class AggregateRoot<TKey> : Entity<TKey>(), IAggregateRoot<TKey> {
override var alive: Boolean = true
}

View File

@@ -5,4 +5,10 @@ package com.synebula.gaea.domain.model
*
* @author alex
**/
interface IAggregateRoot<TKey> : IEntity<TKey>
interface IAggregateRoot<TKey> : IEntity<TKey> {
/**
* 实体对象是否有效。
*/
var alive: Boolean
}

View File

@@ -12,10 +12,6 @@ interface IEntity<TKey> {
/**
* 实体ID
*/
var id: TKey
var id: TKey?
/**
* 实体对象是否有效。
*/
var alive: Boolean
}

View File

@@ -3,5 +3,5 @@ package com.synebula.gaea.domain.model.complex
import com.synebula.gaea.domain.model.IEntity
interface IComplexEntity<TKey, TSecond> : IEntity<TKey> {
var secondary: TSecond
var secondary: TSecond?
}

View File

@@ -10,6 +10,11 @@ import com.synebula.gaea.domain.model.IAggregateRoot
*/
interface IRepository<TAggregateRoot : IAggregateRoot<TKey>, TKey> {
/**
* 仓储的对象类
*/
var clazz: Class<TAggregateRoot>?
/**
* 插入单个对象。
*
@@ -40,5 +45,15 @@ interface IRepository<TAggregateRoot : IAggregateRoot<TKey>, TKey> {
* @param id 对象ID。
* @return
*/
operator fun get(id: TKey): TAggregateRoot
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

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

View File

@@ -2,14 +2,14 @@ package com.synebula.gaea.domain.repository
import com.synebula.gaea.domain.model.IAggregateRoot
interface ITypedRepository<TAggregateRoot : IAggregateRoot<TKey>, TKey> {
interface IRepositoryTyped {
/**
* 插入单个对象
*
* @param obj 需要插入的对象
* @return 返回原对象如果对象ID为自增则补充自增ID
*/
fun add(obj: TAggregateRoot)
fun <TAggregateRoot : IAggregateRoot<TKey>, TKey> add(obj: TAggregateRoot, clazz: Class<TAggregateRoot>)
/**
* 更新对象
@@ -17,7 +17,7 @@ interface ITypedRepository<TAggregateRoot : IAggregateRoot<TKey>, TKey> {
* @param obj 需要更新的对象
* @return
*/
fun update(obj: TAggregateRoot)
fun <TAggregateRoot : IAggregateRoot<TKey>, TKey> update(obj: TAggregateRoot, clazz: Class<TAggregateRoot>)
/**
* 通过id删除该条数据
@@ -25,7 +25,7 @@ interface ITypedRepository<TAggregateRoot : IAggregateRoot<TKey>, TKey> {
* @param id id
* @param clazz 操作数据的类型
*/
fun remove(id: TKey, clazz: Class<TAggregateRoot>)
fun <TAggregateRoot : IAggregateRoot<TKey>, TKey> remove(id: TKey, clazz: Class<TAggregateRoot>)
/**
* 根据ID获取对象
@@ -34,5 +34,5 @@ interface ITypedRepository<TAggregateRoot : IAggregateRoot<TKey>, TKey> {
* @param clazz 操作数据的类型
* @return 聚合根
*/
fun get(id: TKey, clazz: Class<TAggregateRoot>): TAggregateRoot
fun <TAggregateRoot : IAggregateRoot<TKey>, TKey> get(id: TKey, clazz: Class<TAggregateRoot>): TAggregateRoot
}

View File

@@ -1,6 +1,7 @@
package com.synebula.gaea.domain.service
import com.synebula.gaea.data.message.Message
import com.synebula.gaea.domain.model.IAggregateRoot
import com.synebula.gaea.log.ILogger
@@ -21,4 +22,6 @@ interface IService<TKey> {
fun update(key: TKey, command: ICommand)
fun remove(key: TKey)
fun <TAggregateRoot : IAggregateRoot<TKey>> get(key: TKey, clazz: Class<TAggregateRoot>): TAggregateRoot
}

View File

@@ -9,7 +9,7 @@ import com.synebula.gaea.data.message.Message
* @version 0.1
* @since 2018 18-2-8
*/
interface IComplexService<TKey, TSecond> {
interface IServiceComplex<TKey, TSecond> {
fun add(command: ICommand): Message<Pair<TKey, TSecond>>

View File

@@ -10,20 +10,28 @@ import com.synebula.gaea.log.ILogger
/**
* class FlatService
*
* @param repository 仓储对象
* @param rootClass 聚合根类对象
* @param converter 对象转换组件
* @param logger 日志组件
* @author alex
* @version 0.1
* @since 2018 18-2-8
*/
open class Service<TAggregateRoot : IAggregateRoot<TKey>, TKey>
(override var logger: ILogger,
protected var repository: IRepository<TAggregateRoot, TKey>,
protected var converter: IObjectConverter,
protected var aggregateRootClass: Class<TAggregateRoot>) : IService<TKey> {
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
}
override fun add(command: ICommand): Message<TKey> {
val msg = Message<TKey>()
val root = this.convert(command)
repository.add(root)
this.repository.add(root)
msg.data = root.id
return msg
}
@@ -31,11 +39,19 @@ open class Service<TAggregateRoot : IAggregateRoot<TKey>, TKey>
override fun update(key: TKey, command: ICommand) {
val root = this.convert(command)
root.id = key
repository.update(root)
this.repository.update(root)
}
override fun remove(key: TKey) {
repository.remove(key)
this.repository.remove(key)
}
fun get(key: TKey): TAggregateRoot {
return this.repository.get(key)
}
override fun <T : IAggregateRoot<TKey>> get(key: TKey, clazz: Class<T>): T {
return this.repository.get(key, clazz)
}
/**
@@ -46,7 +62,7 @@ open class Service<TAggregateRoot : IAggregateRoot<TKey>, TKey>
*/
protected fun convert(command: ICommand): TAggregateRoot {
try {
return converter.convert(command, aggregateRootClass)
return converter.convert(command, rootClass)
} catch (ex: Exception) {
throw RuntimeException("command not match aggregate root", ex)
}

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.IComplexRepository
import com.synebula.gaea.domain.repository.IRepositoryComplex
import com.synebula.gaea.log.ILogger
/**
@@ -13,16 +13,16 @@ import com.synebula.gaea.log.ILogger
* @version 0.1
* @since 2018 18-2-8
*/
open class ComplexService<TAggregateRoot : IComplexAggregateRoot<TKey, TSecond>, TKey, TSecond>
(var logger: ILogger, protected var repository: IComplexRepository<TAggregateRoot, TKey, TSecond>,
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>)
: IComplexService<TKey, TSecond> {
: IServiceComplex<TKey, TSecond> {
override fun add(command: ICommand): Message<Pair<TKey, TSecond>> {
val msg = Message<Pair<TKey, TSecond>>()
val root = this.convert(command)
repository.add(root)
msg.data = Pair<TKey, TSecond>(root.id, root.secondary)
msg.data = Pair<TKey, TSecond>(root.id!!, root.secondary!!)
return msg
}

View File

@@ -0,0 +1,62 @@
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.log.ILogger
/**
* class FlatService
*
* @param repository 仓储对象
* @param rootClass 聚合根类对象
* @param converter 对象转换组件
* @param logger 日志组件
* @author alex
* @version 0.1
* @since 2018 18-2-8
*/
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> {
override fun add(command: ICommand): Message<TKey> {
val msg = Message<TKey>()
val root = this.convert(command)
this.repository.add(root, rootClass)
msg.data = root.id
return msg
}
override fun update(key: TKey, command: ICommand) {
val root = this.convert(command)
root.id = key
this.repository.update(root, rootClass)
}
override fun remove(key: TKey) {
this.repository.remove(key, rootClass)
}
override fun <TAggregateRoot : IAggregateRoot<TKey>> get(key: TKey, clazz: Class<TAggregateRoot>): TAggregateRoot {
return this.repository.get(key, clazz)
}
/**
* 转换ICommand类型到聚合根类型默认实现根据需要进行覆写。
*
* @param command
* @return
*/
protected fun convert(command: ICommand): TAggregateRoot {
try {
return converter.convert(command, rootClass)
} catch (ex: Exception) {
throw RuntimeException("command not match aggregate root", ex)
}
}
}

View File

@@ -0,0 +1,9 @@
package com.synebula.gaea.extension
fun String.firstCharLowerCase(): String {
if (Character.isLowerCase(this.elementAt(0)))
return this
else
return StringBuilder().append(Character.toLowerCase(this.elementAt(0)))
.append(this.substring(1)).toString()
}

View File

@@ -27,43 +27,103 @@ class NullLogger : ILogger {
override val name: String
get() = NullLogger::class.simpleName!!
override fun trace(t: Throwable) {}
override fun trace(t: Throwable) {
override fun trace(format: String, vararg args: Any) {}
}
override fun trace(t: Throwable, format: String, vararg args: Any) {}
override fun trace(format: String, vararg args: Any) {
override fun trace(obj: Any, t: Throwable, format: String, vararg args: Any) {}
}
override fun debug(t: Throwable) {}
override fun trace(t: Throwable, format: String, vararg args: Any) {
override fun debug(format: String, vararg args: Any) {}
}
override fun debug(t: Throwable, format: String, vararg args: Any) {}
override fun trace(obj: Any, format: String, vararg args: Any) {
override fun debug(obj: Any, t: Throwable, format: String, vararg args: Any) {}
}
override fun info(t: Throwable) {}
override fun trace(obj: Any, t: Throwable?, format: String, vararg args: Any) {
override fun info(format: String, vararg args: Any) {}
}
override fun info(t: Throwable, format: String, vararg args: Any) {}
override fun debug(t: Throwable) {
override fun info(obj: Any, t: Throwable, format: String, vararg args: Any) {}
}
override fun warn(t: Throwable) {}
override fun debug(format: String, vararg args: Any) {
override fun warn(format: String, vararg args: Any) {}
}
override fun warn(t: Throwable, format: String, vararg args: Any) {}
override fun debug(t: Throwable, format: String, vararg args: Any) {
override fun warn(obj: Any, t: Throwable, format: String, vararg args: Any) {}
}
override fun error(t: Throwable) {}
override fun debug(obj: Any, format: String, vararg args: Any) {
override fun error(format: String, vararg args: Any) {}
}
override fun error(t: Throwable, format: String, vararg args: Any) {}
override fun debug(obj: Any, t: Throwable?, format: String, vararg args: Any) {
override fun error(obj: Any, t: Throwable, format: String, vararg args: Any) {}
}
override fun info(t: Throwable) {
}
override fun info(format: String, vararg args: Any) {
}
override fun info(t: Throwable, format: String, vararg args: Any) {
}
override fun info(obj: Any, format: String, vararg args: Any) {
}
override fun info(obj: Any, t: Throwable?, format: String, vararg args: Any) {
}
override fun warn(t: Throwable) {
}
override fun warn(format: String, vararg args: Any) {
}
override fun warn(t: Throwable, format: String, vararg args: Any) {
}
override fun warn(obj: Any, format: String, vararg args: Any) {
}
override fun warn(obj: Any, t: Throwable?, format: String, vararg args: Any) {
}
override fun error(t: Throwable) {
}
override fun error(format: String, vararg args: Any) {
}
override fun error(t: Throwable, format: String, vararg args: Any) {
}
override fun error(obj: Any, format: String, vararg args: Any) {
}
override fun error(obj: Any, t: Throwable?, format: String, vararg args: Any) {
}
}

View File

@@ -35,6 +35,15 @@ interface IDebugLogger {
*/
fun debug(t: Throwable, format: String, vararg args: Any)
/**
* 打印 DEBUG 等级的日志
*
* @param obj 输出错误对象
* @param format 消息模板
* @param args 参数
*/
fun debug(obj: Any, format: String, vararg args: Any)
/**
* 打印 DEBUG 等级的日志
*
@@ -43,5 +52,5 @@ interface IDebugLogger {
* @param format 消息模板
* @param args 参数
*/
fun debug(obj: Any, t: Throwable, format: String, vararg args: Any)
fun debug(obj: Any, t: Throwable?, format: String, vararg args: Any)
}

View File

@@ -34,6 +34,15 @@ interface IErrorLogger {
*/
fun error(t: Throwable, format: String, vararg args: Any)
/**
* 打印 ERROR 等级的日志
*
* @param obj 输出错误对象
* @param format 消息模板
* @param args 参数
*/
fun error(obj: Any, format: String, vararg args: Any)
/**
* 打印 ERROR 等级的日志
*
@@ -42,5 +51,5 @@ interface IErrorLogger {
* @param format 消息模板
* @param args 参数
*/
fun error(obj: Any, t: Throwable, format: String, vararg args: Any)
fun error(obj: Any, t: Throwable?, format: String, vararg args: Any)
}

View File

@@ -34,6 +34,15 @@ interface IInfoLogger {
*/
fun info(t: Throwable, format: String, vararg args: Any)
/**
* 打印 INFO 等级的日志
*
* @param obj 输出错误对象
* @param format 消息模板
* @param args 参数
*/
fun info(obj: Any, format: String, vararg args: Any)
/**
* 打印 INFO 等级的日志
*
@@ -42,5 +51,5 @@ interface IInfoLogger {
* @param format 消息模板
* @param args 参数
*/
fun info(obj: Any, t: Throwable, format: String, vararg args: Any)
fun info(obj: Any, t: Throwable?, format: String, vararg args: Any)
}

View File

@@ -35,6 +35,16 @@ interface ITraceLogger {
*/
fun trace(t: Throwable, format: String, vararg args: Any)
/**
* 打印 TRACE 等级的日志
*
* @param obj 输出错误对象
* @param format 消息模板
* @param args 参数
*/
fun trace(obj: Any, format: String, vararg args: Any)
/**
* 打印 TRACE 等级的日志
*
@@ -43,6 +53,6 @@ interface ITraceLogger {
* @param format 消息模板
* @param args 参数
*/
fun trace(obj: Any, t: Throwable, format: String, vararg args: Any)
fun trace(obj: Any, t: Throwable?, format: String, vararg args: Any)
}

View File

@@ -34,6 +34,16 @@ interface IWarnLogger {
*/
fun warn(t: Throwable, format: String, vararg args: Any)
/**
* 打印 WARN 等级的日志
*
* @param obj 输出错误对象
* @param format 消息模板
* @param args 参数
*/
fun warn(obj: Any, format: String, vararg args: Any)
/**
* 打印 WARN 等级的日志
*
@@ -42,5 +52,5 @@ interface IWarnLogger {
* @param format 消息模板
* @param args 参数
*/
fun warn(obj: Any, t: Throwable, format: String, vararg args: Any)
fun warn(obj: Any, t: Throwable?, format: String, vararg args: Any)
}

View File

@@ -0,0 +1,42 @@
package com.synebula.gaea.query
/**
* 查询基接口。
*
* @author alex
*/
interface ITypedQuery<TView, TKey> {
/**
* 根据Key获取对象。
*
* @param key 对象Key。
* @return
*/
fun get(key: TKey, clazz: Class<TView>): TView?
/**
* 根据实体类条件查询所有符合条件记录
*
* @param params 查询条件。
* @return list
*/
fun list(params: Map<String, Any>?, clazz: Class<TView>): List<TView>
/**
* 根据条件查询符合条件记录的数量
*
* @param params 查询条件。
* @return int
*/
fun count(params: Map<String, Any>?, clazz: Class<TView>): Int
/**
* 根据实体类条件查询所有符合条件记录(分页查询)
*
* @param params 分页条件
* @return 分页数据
*/
fun paging(params: PagingParam, clazz: Class<TView>): PagingData<TView>
}