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

1

6

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

Hild answered 28/4, 2016 at 9:11 Comment(1)
Good question, for which I am also interested in an answer. Out of my guts I would go with Sling Models as it is more generic and you do not need to inherit the WCMUse class.Peon
K
6

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).

Kloster answered 28/4, 2016 at 12:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.