From 53709b6fc3041fcf853da71b513f562c9f948492 Mon Sep 17 00:00:00 2001 From: alex Date: Mon, 22 Aug 2022 15:16:41 +0800 Subject: [PATCH] =?UTF-8?q?1.2.0=20=E5=A2=9E=E5=8A=A0=20IRepository=20IQue?= =?UTF-8?q?ry=E7=9A=84=E5=B7=A5=E5=8E=82=E6=96=B9=E6=B3=95=EF=BC=8C?= =?UTF-8?q?=E9=81=BF=E5=85=8D=E6=97=A0=E7=94=A8=E7=9A=84=E7=A9=BA=E7=99=BD?= =?UTF-8?q?=E6=8E=A5=E5=8F=A3=E7=BC=96=E5=86=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build.gradle | 2 +- .../app/autoconfig/service/ServiceProxy.kt | 24 +++++++++++++------ .../mongodb/autoconfig/MongodbRepoRegister.kt | 12 ++++++++++ .../gaea/mongodb/query/MongodbQueryFactory.kt | 23 ++++++++++++++++++ .../repository/MongodbRepositoryFactory.kt | 24 +++++++++++++++++++ .../domain/repository/IRepositoryFactory.kt | 19 +++++++++++++++ .../synebula/gaea/domain/service/Domain.kt | 13 ++++++++++ .../gaea/domain/service/ServiceDependency.kt | 16 ------------- .../com/synebula/gaea/query/IQueryFactory.kt | 18 ++++++++++++++ 9 files changed, 127 insertions(+), 24 deletions(-) create mode 100644 src/gaea.mongodb/src/main/kotlin/com/synebula/gaea/mongodb/query/MongodbQueryFactory.kt create mode 100644 src/gaea.mongodb/src/main/kotlin/com/synebula/gaea/mongodb/repository/MongodbRepositoryFactory.kt create mode 100644 src/gaea/src/main/kotlin/com/synebula/gaea/domain/repository/IRepositoryFactory.kt create mode 100644 src/gaea/src/main/kotlin/com/synebula/gaea/domain/service/Domain.kt delete mode 100644 src/gaea/src/main/kotlin/com/synebula/gaea/domain/service/ServiceDependency.kt create mode 100644 src/gaea/src/main/kotlin/com/synebula/gaea/query/IQueryFactory.kt diff --git a/build.gradle b/build.gradle index 00cd1c5..128548a 100644 --- a/build.gradle +++ b/build.gradle @@ -17,7 +17,7 @@ buildscript { subprojects { group 'com.synebula' - version '1.1.2' + version '1.2.0' buildscript { repositories { diff --git a/src/gaea.app/src/main/kotlin/com/synebula/gaea/app/autoconfig/service/ServiceProxy.kt b/src/gaea.app/src/main/kotlin/com/synebula/gaea/app/autoconfig/service/ServiceProxy.kt index 3920630..05ec640 100644 --- a/src/gaea.app/src/main/kotlin/com/synebula/gaea/app/autoconfig/service/ServiceProxy.kt +++ b/src/gaea.app/src/main/kotlin/com/synebula/gaea/app/autoconfig/service/ServiceProxy.kt @@ -2,16 +2,18 @@ package com.synebula.gaea.app.autoconfig.service import com.synebula.gaea.data.serialization.IObjectMapper import com.synebula.gaea.domain.repository.IRepository +import com.synebula.gaea.domain.repository.IRepositoryFactory +import com.synebula.gaea.domain.service.Domain import com.synebula.gaea.domain.service.IService import com.synebula.gaea.domain.service.Service -import com.synebula.gaea.domain.service.ServiceDependency import com.synebula.gaea.spring.autoconfig.Proxy import org.springframework.beans.factory.BeanFactory import java.io.InvalidClassException import java.lang.reflect.Method class ServiceProxy( - private var supertype: Class<*>, private var beanFactory: BeanFactory, implementBeanNames: Array = arrayOf() + private var supertype: Class<*>, + private var beanFactory: BeanFactory, implementBeanNames: Array = arrayOf() ) : Proxy() { private var service: IService<*> @@ -20,18 +22,26 @@ class ServiceProxy( // 如果没有实现类, 使用Service类代理 if (implementBeanNames.isEmpty()) { // 如果没有实现类并且没有ServiceDependency注解, 则抛出异常 - if (!this.supertype.declaredAnnotations.any { it.annotationClass == ServiceDependency::class }) { + if (!this.supertype.declaredAnnotations.any { it.annotationClass == Domain::class }) { throw InvalidClassException( - "interface ${this.supertype.name} must has implementation class or annotation by ${ServiceDependency::class.qualifiedName}" + "interface ${this.supertype.name} must has implementation class or annotation by ${Domain::class.qualifiedName}" ) } - val serviceDependency = this.supertype.getDeclaredAnnotation(ServiceDependency::class.java) - val repo = this.beanFactory.getBean(serviceDependency.repo.java) + val domain = this.supertype.getDeclaredAnnotation(Domain::class.java) + + // repository工厂对象 + val defaultRepositoryFactory = this.beanFactory.getBean(IRepositoryFactory::class.java) val mapper = this.beanFactory.getBean(IObjectMapper::class.java) + val constructor = Service::class.java.getConstructor( Class::class.java, IRepository::class.java, IObjectMapper::class.java ) - this.service = constructor.newInstance(serviceDependency.clazz.java, repo, mapper) + this.service = + constructor.newInstance( + domain.clazz.java, + defaultRepositoryFactory.createRawRepository(domain.clazz.java), + mapper + ) } else { this.service = this.beanFactory.getBean(implementBeanNames[0]) as IService<*> } diff --git a/src/gaea.mongodb/src/main/kotlin/com/synebula/gaea/mongodb/autoconfig/MongodbRepoRegister.kt b/src/gaea.mongodb/src/main/kotlin/com/synebula/gaea/mongodb/autoconfig/MongodbRepoRegister.kt index f62cf4c..55d6bda 100644 --- a/src/gaea.mongodb/src/main/kotlin/com/synebula/gaea/mongodb/autoconfig/MongodbRepoRegister.kt +++ b/src/gaea.mongodb/src/main/kotlin/com/synebula/gaea/mongodb/autoconfig/MongodbRepoRegister.kt @@ -51,4 +51,16 @@ class MongodbRepoRegister : Register() { } return result } + + private fun addDefaultProxyBean(result: MutableMap) { + // IRepository proxy + val builder = BeanDefinitionBuilder.genericBeanDefinition(IRepository::class.java) + builder.addConstructorArgValue(IRepository::class.java) + builder.addConstructorArgValue(this._beanFactory) + builder.addConstructorArgValue(emptyArray()) + val definition = builder.rawBeanDefinition as GenericBeanDefinition + definition.beanClass = MongodbRepoFactory::class.java + definition.autowireMode = GenericBeanDefinition.AUTOWIRE_BY_TYPE + result[IRepository::class.java.name] = definition + } } \ No newline at end of file diff --git a/src/gaea.mongodb/src/main/kotlin/com/synebula/gaea/mongodb/query/MongodbQueryFactory.kt b/src/gaea.mongodb/src/main/kotlin/com/synebula/gaea/mongodb/query/MongodbQueryFactory.kt new file mode 100644 index 0000000..ca4c351 --- /dev/null +++ b/src/gaea.mongodb/src/main/kotlin/com/synebula/gaea/mongodb/query/MongodbQueryFactory.kt @@ -0,0 +1,23 @@ +package com.synebula.gaea.mongodb.query + +import com.synebula.gaea.query.IQuery +import com.synebula.gaea.query.IQueryFactory +import org.springframework.data.mongodb.core.MongoTemplate + +class MongodbQueryFactory(var template: MongoTemplate) : IQueryFactory { + + /** + * 创建IQuery接口类型 + */ + override fun createRawQuery(clazz: Class<*>): IQuery<*, *> { + val constructor = MongodbQuery::class.java.getConstructor(Class::class.java, MongoTemplate::class.java) + return constructor.newInstance(clazz, this.template) + } + + /** + * 创建IQuery接口类型 + */ + override fun createQuery(clazz: Class): IQuery { + return MongodbQuery(clazz, template) + } +} \ No newline at end of file diff --git a/src/gaea.mongodb/src/main/kotlin/com/synebula/gaea/mongodb/repository/MongodbRepositoryFactory.kt b/src/gaea.mongodb/src/main/kotlin/com/synebula/gaea/mongodb/repository/MongodbRepositoryFactory.kt new file mode 100644 index 0000000..012db8f --- /dev/null +++ b/src/gaea.mongodb/src/main/kotlin/com/synebula/gaea/mongodb/repository/MongodbRepositoryFactory.kt @@ -0,0 +1,24 @@ +package com.synebula.gaea.mongodb.repository + +import com.synebula.gaea.domain.model.IAggregateRoot +import com.synebula.gaea.domain.repository.IRepository +import com.synebula.gaea.domain.repository.IRepositoryFactory +import org.springframework.data.mongodb.core.MongoTemplate + +class MongodbRepositoryFactory(var template: MongoTemplate) : IRepositoryFactory { + + /** + * 创建IRepository接口类型 + */ + override fun createRawRepository(clazz: Class<*>): IRepository<*, *> { + val constructor = MongodbRepository::class.java.getConstructor(Class::class.java, MongoTemplate::class.java) + return constructor.newInstance(clazz, this.template) + } + + /** + * 创建IRepository接口类型 + */ + override fun , I> createRepository(clazz: Class): IRepository { + return MongodbRepository(clazz, template) + } +} \ No newline at end of file diff --git a/src/gaea/src/main/kotlin/com/synebula/gaea/domain/repository/IRepositoryFactory.kt b/src/gaea/src/main/kotlin/com/synebula/gaea/domain/repository/IRepositoryFactory.kt new file mode 100644 index 0000000..20fb622 --- /dev/null +++ b/src/gaea/src/main/kotlin/com/synebula/gaea/domain/repository/IRepositoryFactory.kt @@ -0,0 +1,19 @@ +package com.synebula.gaea.domain.repository + +import com.synebula.gaea.domain.model.IAggregateRoot + +/** + * Repository 工厂接口。 定义了Repository的创建方法。 + */ +interface IRepositoryFactory { + + /** + * 创建原始类型的IRepository接口类型 + */ + fun createRawRepository(clazz: Class<*>): IRepository<*, *> + + /** + * 创建指定类型的IRepository接口类型 + */ + fun , I> createRepository(clazz: Class): IRepository +} \ No newline at end of file diff --git a/src/gaea/src/main/kotlin/com/synebula/gaea/domain/service/Domain.kt b/src/gaea/src/main/kotlin/com/synebula/gaea/domain/service/Domain.kt new file mode 100644 index 0000000..16f787c --- /dev/null +++ b/src/gaea/src/main/kotlin/com/synebula/gaea/domain/service/Domain.kt @@ -0,0 +1,13 @@ +package com.synebula.gaea.domain.service + +import com.synebula.gaea.domain.model.IAggregateRoot +import kotlin.reflect.KClass + +/** + * 声明服务依赖的聚合根,若服务没有实现类则可以根据依赖项自动组装服务。 + * + * @param clazz 依赖的聚合根类型 + */ +@Target(AnnotationTarget.CLASS) +@Retention(AnnotationRetention.RUNTIME) +annotation class Domain(val clazz: KClass>) diff --git a/src/gaea/src/main/kotlin/com/synebula/gaea/domain/service/ServiceDependency.kt b/src/gaea/src/main/kotlin/com/synebula/gaea/domain/service/ServiceDependency.kt deleted file mode 100644 index 42e0c2b..0000000 --- a/src/gaea/src/main/kotlin/com/synebula/gaea/domain/service/ServiceDependency.kt +++ /dev/null @@ -1,16 +0,0 @@ -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>, - val repo: KClass>, -) diff --git a/src/gaea/src/main/kotlin/com/synebula/gaea/query/IQueryFactory.kt b/src/gaea/src/main/kotlin/com/synebula/gaea/query/IQueryFactory.kt new file mode 100644 index 0000000..f89d856 --- /dev/null +++ b/src/gaea/src/main/kotlin/com/synebula/gaea/query/IQueryFactory.kt @@ -0,0 +1,18 @@ +package com.synebula.gaea.query + + +/** + * Query 工厂接口。 定义了Query的创建方法。 + */ +interface IQueryFactory { + + /** + * 创建原始类型的IQuery接口类型 + */ + fun createRawQuery(clazz: Class<*>): IQuery<*, *> + + /** + * 创建指定类型的IQuery接口类型 + */ + fun createQuery(clazz: Class): IQuery +} \ No newline at end of file