Expand my Community achievements bar.

Dive into Adobe Summit 2024! Explore curated list of AEM sessions & labs, register, connect with experts, ask questions, engage, and share insights. Don't miss the excitement.

Injecting a QueryBuilder vs adapting from a resource resolver?

Avatar

Level 5

When using the querybuilder, what's the difference if any, between injecting he querybuilder services, versus adapting from a resource resolver?

Since you must call createQuery() with a Session, I think the result will likely be the same for both of these cases. Is there any particular version that considered "best practice?"

@Model(adaptables = SlingHttpServletRequest.class) public class MyClass { @OSGiService QueryBuilder qb; @Inject SlingHttpServletRequest request; public void myMethod() { //...build out predicates ResourceResolver resourceResolver = request.getResourceResolver(); Session session = resourceResolver.adaptTo(Session.class); Query query = qb.createQuery(pg,session); } }

vs

@Model(adaptables = SlingHttpServletRequest.class) public class MyClass { @Inject SlingHttpServletRequest request; public void myMethod() { //...build out predicates ResourceResolver resourceResolver = request.getResourceResolver(); Session session = resourceResolver.adaptTo(Session.class); QueryBuilder qb = resourceResolver.adaptTo(Querybuilder.class); Query query = qb.createQuery(pg,session); } }

Thanks

3 Replies

Avatar

Level 10

You are correct - they both give you access to the JCR in order to use Query Builder.

Dependency Injection is a standard when using OSGi. 

Here is a good community article on it. 

https://www.credera.com/blog/custom-application-development/dependency-injection-part-1-need/

 I do not think there is a performance gain either way. 

Avatar

Level 10

Hi,

Pls let us know, if your issue has been solved.

Thanks.

Avatar

Level 1

Hi, I would also recommend this article: https://www.cleveroad.com/blog/custom-mobile-app-development/
Here you'll find all the peculiarities of custom application development: from app types to development stages and cost. You'll also learn why personalized software benefits from ready-made one.