Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Sling models

65 views

Published on

Sling Models 101, all you need to know to get started with Sling models.

Published in: Software
  • Be the first to comment

  • Be the first to like this

Sling models

  1. 1. SLING MODELS VOL I - BASICS By Ankit Gubrani
  2. 2. .about-me{ name: Ankit Gubrani; title: Sr. Software Engineer; working at: Playstation; email-id: Ankit.gubrani90@gmail.com; linkedIn: linkedin.com/in/ankitgubrani; twitter: @ankitgubrani90; blog: codebrains.co.in; }
  3. 3. AGENDA š What are Sling Models? š Why Sling Models? š WCMPojo VS Sling Models? š Getting started with Sling Models š Demo š Basic Sling Model Annotations š Available Injectors š Sling Models Exporter
  4. 4. BASIC AEM TERMINOLOGIES š OSGi - Open System Gateway Initiative š JCR - Java Content Repository š Apache Sling
  5. 5. WHAT ARE SLING MODELS? Before jumping on to Sling Models directly, let's see what are models?
  6. 6. MODELS ARE : š classes which encapsulate closely related items for eg: ProductDataModel š classes which usually are used to define the domain objects š classes which lets us implement MVC design patterns
  7. 7. SLING MODELS ARE : š pure POJO classes, driven by annotations š classes which OOTB, support resource properties (via ValueMap), SlingBindings, OSGi services, request attributes š classes which lets us adapt multiple objects - minimal required Resource and SlingHttpServletRequest š Adapter Pattern: The Adapter pattern allows otherwise incompatible classes to work together by converting the interface of one class into an interface expected by the clients.
  8. 8. Sling models are POJO classes in which properties of the adaptable interfaces/classes can be injects and hence reducing the boilerplate code
  9. 9. BRIEF HISTORY š Before sling Models, we used have problems like boiler plate code & reusing code was difficult š while sling Models were initially introduced to make Adapter pattern easier to implement with custom code š Around the same time as Sling models a new templating framework called “Sightly” was introduced Taglibs/Java Models-> WCMUsePojo -> Sling Models
  10. 10. QUICK DEMO OF CODE BEFORE SLING MODELS
  11. 11. WHY SLING MODELS? š Reduces code Redundancy š Makes code more maintainable š Reduced coding efforts
  12. 12. GETTING STARTED WITH SLING MODELS 1. Add the sling model dependency 2. Create a package under which all the Sling Models will reside 3. Add package path as “Sling-Model-Packages” property of “maven-bundle-plugin” 4. Next create a Class or Interface in the newly created package & add the “@Model” annotation on Class 5. Use @Inject annotation to inject a field from adaptable class defined in @Model annotation
  13. 13. DEMO
  14. 14. WCMPOJO V/S SLING MODELS š WCMUsePojo: š WCMPojo is an abstract class which implements Use interface š Class extending WCMPojo abstract class is initialised with the scripting bindings, providing convenience methods for accessing commonly used objects š Sling Models helps you reduce the code even futher using @Inject, hence making your code even more maintainable š Sling Models can be even used outside of HTL/Sightly, which makes Sling Models even more reusable
  15. 15. BASIC SLING ANNOTATIONS 1. @Model 2. @Inject 3. @Named 4. @Default 5. @Optional 6. @PostContruct 7. @Via 8. @Self 9. @OSGiService For details of each annoation please refer the documentation
  16. 16. QUICK INTRO TO SLING MODEL EXPORTER FRAMEWORK Along with Sling Models 1.3.0 there was a new framework introduced known as Sling Model Exporter Framework. Sling Model Exporter framework comes with Jackson Exporter that automatically serializes an “Ordinary” sling model object into JSON
  17. 17. THANK YOU Contact Us : LinkedIn : Ankit Gubrani Twitter : @ankitgubrani90 Email-ID : ankit.gubrani90@gmail.com Blog : www.codebrains.co.in

×