Expand my Community achievements bar.

Avoiding Delegation Pattern Pitfalls with Core Components

Avatar

Community Advisor

9/20/23

AEM Discussions

Avoiding Delegation Pattern Pitfalls with Core Components

by Veena Vikraman

Overview

There was an issue mentioned in the forum (https://experienceleaguecommunities.adobe.com/t5/adobe-experience-manager/delegation-pattern-for-sli...)  which was caused by the Delegation Pattern. This blog explains the issues you can encounter while using the delegation pattern and how to resolve them. Read More Here 


Q&A

Please use this thread to ask questions relating to this article and tag @VeenaVikraman for quick responses.

2 Comments

Avatar

Administrator

9/21/23

@VeenaVikraman 

This is a great read on the importance of leveraging core components and delegation patterns to enhance AEM development. Thanks for sharing this with the AEM community!

One recommendation I have is to consider adding a section on the potential pitfalls of the delegation pattern. For example, if you override too many methods in a core component, it can make it difficult to maintain and upgrade your code. Additionally, it is important to carefully consider which methods to override, as modifying the behavior of a core component could have unintended consequences.

Overall, this is a well-written and informative article.

Avatar

Level 5

9/21/23

Thanks for sharing this. A few pointers, and you may ignore if I missed reading any sections that covered this.

 

For Junit to use sling mocks and delegation pattern, along with either creating the content parent resource types also should be created. E.g. A component inside a page /content/brand/site/en/home/jcr:content/root/text might be of type wknd/components/content/text which might be extending core/wcm/components/text. Now inside the mocked environment core/wcm/components/text should exist for delegation pattern to work. But that doesn't mean we have to put the entire component code into mocked environment. Ensuring a resource with such path existing is good enough for sling model mocks to work as expected.

 

Now the other one, probably is a nice to have or may be an expert like you can guide us

 

Delegation pattern works only with adaptable SlingHttpServletRequest and not Resources. This is definitely a huge downside for reusing sling model apis to full potential. E.g. I may be building some content service apis which run on a publisher and probably wanted to use a service user to obtain the resource resolver. To make it even more convincing let's say I have a scheduler that wants to consolidate output of different sling models and sent it to another endpoint external to aem e.g. Elastic search, and I already have sling model that partly has custom code and rest is inherited via delegation pattern. Now because I don't have a sling request object, or may be I have to use some factory service to obtain a request, which I am unsure how in this case, however if I have to use resource object I can't invoke the sling model that's only bound to requests. Advise is there is a better way to handle this but not using servlets