You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@grails.apache.org by bo...@apache.org on 2026/06/04 05:25:26 UTC
(grails-core) 04/16: refactor(gorm): decompose GormRegistry to extract DynamicFinderCreator
This is an automated email from the ASF dual-hosted git repository.
borinquenkid pushed a commit to branch 8.0.x-hibernate7.gorm-registry-refactor
in repository https://gitbox.apache.org/repos/asf/grails-core.git
commit 17bd08f5e9b9e8c1282dd640b7c3bcbcdcee8703
Author: Walter Duque de Estrada <wb...@mac.com>
AuthorDate: Wed Jun 3 17:58:32 2026 -0500
refactor(gorm): decompose GormRegistry to extract DynamicFinderCreator
---
.../datastore/gorm/DynamicFinderCreator.groovy | 57 ++++++++++++++++
.../org/grails/datastore/gorm/GormRegistry.groovy | 22 ++----
.../datastore/gorm/DynamicFinderCreatorSpec.groovy | 78 ++++++++++++++++++++++
3 files changed, 139 insertions(+), 18 deletions(-)
diff --git a/grails-datamapping-core/src/main/groovy/org/grails/datastore/gorm/DynamicFinderCreator.groovy b/grails-datamapping-core/src/main/groovy/org/grails/datastore/gorm/DynamicFinderCreator.groovy
new file mode 100644
index 0000000000..cf55c28f95
--- /dev/null
+++ b/grails-datamapping-core/src/main/groovy/org/grails/datastore/gorm/DynamicFinderCreator.groovy
@@ -0,0 +1,57 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * 'License'); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * 'AS IS' BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.grails.datastore.gorm
+
+import groovy.transform.CompileStatic
+import org.grails.datastore.gorm.finders.FinderMethod
+import org.grails.datastore.mapping.core.Datastore
+import org.grails.datastore.mapping.core.connections.ConnectionSource
+import org.grails.datastore.mapping.model.MappingContext
+
+/**
+ * Creates dynamic finders for GORM datastores.
+ * Extracted from GormRegistry to improve single responsibility and unit testability.
+ *
+ * @author Walter Duque de Estrada
+ * @since 8.0.0
+ */
+@CompileStatic
+@SuppressWarnings(['unused', 'DuplicatedCode'])
+class DynamicFinderCreator {
+
+ private final GormRegistry registry
+
+ DynamicFinderCreator(GormRegistry registry) {
+ this.registry = registry
+ }
+
+ List<FinderMethod> createDynamicFinders(Datastore targetDatastore) {
+ return createDynamicFinders({ -> targetDatastore } as DatastoreResolver, targetDatastore.getMappingContext())
+ }
+
+ List<FinderMethod> createDynamicFinders(DatastoreResolver resolver, MappingContext mappingContext) {
+ Datastore ds = resolver.resolve()
+ return ds ? registry.getApiFactory(ds).createDynamicFinders(resolver, mappingContext) : []
+ }
+
+ DatastoreResolver createClassDatastoreResolver(Class cls, String qualifier = ConnectionSource.DEFAULT) {
+ String normalizedQualifier = registry.normalizeQualifier(qualifier)
+ return { -> registry.apiResolver.findDatastore(cls, normalizedQualifier) } as DatastoreResolver
+ }
+}
diff --git a/grails-datamapping-core/src/main/groovy/org/grails/datastore/gorm/GormRegistry.groovy b/grails-datamapping-core/src/main/groovy/org/grails/datastore/gorm/GormRegistry.groovy
index 7faef341ec..5d17a9d223 100644
--- a/grails-datamapping-core/src/main/groovy/org/grails/datastore/gorm/GormRegistry.groovy
+++ b/grails-datamapping-core/src/main/groovy/org/grails/datastore/gorm/GormRegistry.groovy
@@ -56,6 +56,7 @@ class GormRegistry {
final GormInstanceApiRegistry instanceApiRegistry = entityApiRegistry.instanceApiRegistry
final GormValidationApiRegistry validationApiRegistry = entityApiRegistry.validationApiRegistry
final TransactionResolver transactionResolver = new TransactionResolver(this)
+ final DynamicFinderCreator dynamicFinderCreator = new DynamicFinderCreator(this)
final DatastoreDiscovery datastoreDiscovery = new DatastoreDiscovery()
final Map<String, Datastore> datastoresByQualifier = datastoreDiscovery.datastoresByQualifier
@@ -453,12 +454,7 @@ class GormRegistry {
* @return List of finder methods
*/
List<FinderMethod> createDynamicFinders(Datastore targetDatastore) {
- createDynamicFinders(new DatastoreResolver() {
- @Override
- Datastore resolve() {
- targetDatastore
- }
- }, targetDatastore.getMappingContext())
+ dynamicFinderCreator.createDynamicFinders(targetDatastore)
}
/**
@@ -469,24 +465,14 @@ class GormRegistry {
* @return List of finder methods
*/
List<FinderMethod> createDynamicFinders(DatastoreResolver resolver, MappingContext mappingContext) {
- Datastore ds = resolver.resolve()
- if (ds != null) {
- return getApiFactory(ds).createDynamicFinders(resolver, mappingContext)
- }
- return []
+ dynamicFinderCreator.createDynamicFinders(resolver, mappingContext)
}
/**
* Create a DatastoreResolver for a class and optional qualifier.
*/
DatastoreResolver createClassDatastoreResolver(Class cls, String qualifier = ConnectionSource.DEFAULT) {
- String normalizedQualifier = normalizeQualifier(qualifier)
- return new DatastoreResolver() {
- @Override
- Datastore resolve() {
- apiResolver.findDatastore(cls, normalizedQualifier)
- }
- }
+ dynamicFinderCreator.createClassDatastoreResolver(cls, qualifier)
}
/**
diff --git a/grails-datamapping-core/src/test/groovy/org/grails/datastore/gorm/DynamicFinderCreatorSpec.groovy b/grails-datamapping-core/src/test/groovy/org/grails/datastore/gorm/DynamicFinderCreatorSpec.groovy
new file mode 100644
index 0000000000..b56c992111
--- /dev/null
+++ b/grails-datamapping-core/src/test/groovy/org/grails/datastore/gorm/DynamicFinderCreatorSpec.groovy
@@ -0,0 +1,78 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * 'License'); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * 'AS IS' BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.grails.datastore.gorm
+
+import org.grails.datastore.gorm.finders.FinderMethod
+import org.grails.datastore.mapping.core.Datastore
+import org.grails.datastore.mapping.core.connections.ConnectionSource
+import org.grails.datastore.mapping.model.MappingContext
+import spock.lang.Specification
+
+class DynamicFinderCreatorSpec extends Specification {
+
+ void "test create dynamic finders for datastore"() {
+ given:
+ def registry = GormRegistry.instance
+ registry.reset()
+
+ def creator = registry.dynamicFinderCreator
+ def mappingContext = Stub(MappingContext)
+ def datastore = Stub(Datastore) {
+ getMappingContext() >> mappingContext
+ }
+ def apiFactory = Mock(GormApiFactory)
+ def finder = Stub(FinderMethod)
+
+ when:
+ registry.registerApiFactory(datastore.getClass(), apiFactory)
+ registry.initializeDatastore(datastore)
+ def finders = creator.createDynamicFinders(datastore)
+
+ then:
+ 1 * apiFactory.createDynamicFinders(_, mappingContext) >> [finder]
+ finders == [finder]
+
+ cleanup:
+ registry.reset()
+ }
+
+ void "test create class datastore resolver"() {
+ given:
+ def registry = GormRegistry.instance
+ registry.reset()
+
+ def creator = registry.dynamicFinderCreator
+ def datastore = Stub(Datastore)
+
+ when:
+ registry.initializeDatastore(datastore)
+ registry.registerEntityDatastore(DummyEntity.name, ConnectionSource.DEFAULT, datastore)
+ def resolver = creator.createClassDatastoreResolver(DummyEntity)
+
+ then:
+ resolver.resolve() == datastore
+
+ cleanup:
+ registry.reset()
+ }
+
+ static class DummyEntity {
+ Long id
+ }
+}