4

Why we extends WCMUsepojo abstract class in our program while working with AEM?

What is the advantage of using this class?

Rashid
  • 343
  • 4
  • 14

2 Answers2

7

Because in AEM’s component development mechanism, you likely need a way to provide back-end logic to components.

That's because Sightly (when used as a rendering script language instead of the JSP pages) is on purpose a limited template language that allows to do only small basic operations, and the heavy lifting logic should be done inside a Java class or a server-side JS (that you refer using the data-sly-use element inside the Sightly script).

This provide better decoupling of the business logic and so your code will be more easily maintainable and also easier to debug.

In order to abstract component Java class with page context or binding objects, previously Adobe’s WCMUse was used, or a custom implementation of Use class. If your working with AEM 6.1 or 6.2 WCMUsePojo class is used (or even Sling Models). With the release of AEM 6.3 and AEM Core WCM Components, we see that using Sling Models have been advocated by Adobe as the best practice.

iusting
  • 7,850
  • 2
  • 22
  • 30
  • What are the differences in WCMUsePojo class and server-side JS in terms of usage? I understand that both can be used to write some business logic and return data to html using sightly syntax, but in which scenario shall I prefer WCMUsePojo and where exactly server-side Js will be more suitable? – Abie Nov 29 '17 at 14:18
  • 1
    I don't know if there is common opinion on using one (ServerSide JS) or another (Sling Models), but I personally prefer using the Sling Models. From the top of my mind I would say I prefer it because: 1) I can run unit tests on it. 2) It helps not to have a strange combination of JS and Java objects in the same file, which would not be possible in the server side JS. 3) Sling is built on Java so from a business logic perspective, I think you'll get the best results by using Java. 4) Also depends on the architecture of your application: if you use Node JS, probably the ServerSide JS is better – iusting Nov 29 '17 at 16:21
  • I also think that your comment is an completely different question, posted inside a question that was made almost 6 months ago. :) So I encourage you to post a new question with your doubt, because it will attract more interest and subsequently a better answer from the community. – iusting Nov 29 '17 at 16:22
  • @iusting- Thanks for clarification. Yea I could have posted a fresh question, posted here coz I was reading and had a thought so posted it here only. I will make sure to have different agenda in a new thread. – Abie Nov 30 '17 at 10:59
  • So that people aren't confused by "server-side JS" (which is a phrase that has appeared in Adobe documentation), Adobe has implemented Rhino into AEM to interpret JS and convert it into Java where it is processed server-side. So you essentially write JS as you would Java. It's not true server-side JS and is essentially a slower solution than if you were to use a Java Object. – Arielle Adams Feb 22 '19 at 18:46
2

While the previous answer gives a pretty good explanation, I will write my own - brief one:

  1. You can use simple Pojo (do not extend any Adobe's class) with java-use-api. But in this case, you will not be able to easily access resources/services.
  2. You can extend WCMUsePojo to get the ability to use resources/services.
  3. Also, you can go with Sling Models way which will give you more flexibility.
Oleksandr Tarasenko
  • 1,454
  • 15
  • 21