Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WCM use class can be used wherever we can use sling models.Which one should be preferred and why?

If given an option to use Sling Models or WCM use class which one should be preferred when and why?

Is either of them better performance wise?

Thanks in Advance

like image 587
Shreya Verma Avatar asked Apr 28 '16 09:04

Shreya Verma


People also ask

What is the use of sling models?

Sling Models are annotation driven Java “POJO's” (Plain Old Java Objects) that facilitate the mapping of data from the JCR to Java variables, and provide a number of other niceties when developing in the context of AEM.

What is the difference between WCMUsePojo and sling models?

WCMUsePojo will need to be extend from that class, whereas Sling Models can be standalone class with @Model annotation and no keyword. With Sling Models, it's simpler and cleaner to retrieve common objects or property values, instead of writing more line of code to use API.


1 Answers

Sling models are saving you a lot of time for accessing simple objects as the current page/resource, injecting some properties or services, adapting from resource or sling http request to your model. Sure with the use the plain API your code will execute a little bit faster, because you initialize only the objects you really need, but you have to do all that things "manually". I think that this sightly introduction is giving a good overview of all possible implementation you can go with. You can also have a look at the sightly official documentation. Below you can find a quick overview of the what you can expect and hopefully make your decision easier (quoted from the offical sightly documentation).

Java Use Provider

Advantages Use-objects provided through bundles:

  • faster to initialise and execute than Sling Models for similar code
  • easy to extend from other similar Use-objects
  • simple setup for unit testing

Use-objects backed by Resources:

  • faster to initialise and execute than Sling Models for similar code
  • easy to override from inheriting components through search path overlay or by using the sling:resourceSuperType property, allowing for greater flexibility
  • business logic for components sits next to the Sightly scripts where the objects are used

Disadvantages Use-objects provided through bundles:

  • lacks flexibility in terms of component overlaying

Use-objects backed by Resources:

  • cannot extend other Java objects
  • the Java project might need a different setup to allow running unit tests, since the objects will be deployed like content

Sling Models Use Provider

Advantages

  • convenient injection annotations for data retrieval
  • easy to extend from other Sling Models
  • simple setup for unit testing

Disadvantages

  • lacks flexibility in terms of component overlaying, relying on service.ranking configurations

If you ask me I would always take a framework as sling models or slice which makes the development easier and faster. At the end the performance impact by using a framework is not really a problem, would be not the only one third party framework in the project. But if your project is performance oriented probably you could make some tests with all possibilities you have and decide if such a framework suits your needs (or just mix both).

like image 150
d33t Avatar answered Nov 28 '22 05:11

d33t