Download - Synthetic models
![Page 1: Synthetic models](https://reader034.vdocument.in/reader034/viewer/2022042700/5594447f1a28abfa2f8b474f/html5/thumbnails/1.jpg)
SYNTHETIC MODELS
by Zoran Nikolovski
![Page 2: Synthetic models](https://reader034.vdocument.in/reader034/viewer/2022042700/5594447f1a28abfa2f8b474f/html5/thumbnails/2.jpg)
RESOURCE RESOLUTION
![Page 3: Synthetic models](https://reader034.vdocument.in/reader034/viewer/2022042700/5594447f1a28abfa2f8b474f/html5/thumbnails/3.jpg)
• Traditional web frameworks
– Resolve a Controller based on the data type
– Elaborate mechanics to load the data
• Sling
– A web framework based entirely on REST principals
– The data is at the center or the process
– Everything is a Resource
![Page 4: Synthetic models](https://reader034.vdocument.in/reader034/viewer/2022042700/5594447f1a28abfa2f8b474f/html5/thumbnails/4.jpg)
• Sling maintains a virtual tree of resources
• Each Resource type has a ResourceProvider which provides the root of its
resolution path
• Resource types in the tree
– JCR nodes
– File System
– Bundles
– Servlets
– Other (Non-JCR) Resources
System provided
![Page 5: Synthetic models](https://reader034.vdocument.in/reader034/viewer/2022042700/5594447f1a28abfa2f8b474f/html5/thumbnails/5.jpg)
• Resolving a Resource is simple
The ResourceResolver
takes in the resource path
![Page 6: Synthetic models](https://reader034.vdocument.in/reader034/viewer/2022042700/5594447f1a28abfa2f8b474f/html5/thumbnails/6.jpg)
• Resolving a Resource is simple
It identifies a suitable
ResourceProvider
The ResourceResolver
takes in the resource path
![Page 7: Synthetic models](https://reader034.vdocument.in/reader034/viewer/2022042700/5594447f1a28abfa2f8b474f/html5/thumbnails/7.jpg)
Which calls it’s getResource
method
• Resolving a Resource is simple
It identifies a suitable
ResourceProvider
The ResourceResolver
will take in the resource path
![Page 8: Synthetic models](https://reader034.vdocument.in/reader034/viewer/2022042700/5594447f1a28abfa2f8b474f/html5/thumbnails/8.jpg)
And provide us with the right
Resource
Which calls it’s getResource
method
• Resolving a Resource is simple
It identifies a suitable
ResourceProvider
The ResourceResolver
takes in the resource path
![Page 9: Synthetic models](https://reader034.vdocument.in/reader034/viewer/2022042700/5594447f1a28abfa2f8b474f/html5/thumbnails/9.jpg)
• Resource is a central concept to Sling
• Has a number of essential properties
– Including a reference to the rendering script
• Can be adapted to other compatible types
• It’s the starting point in any domain modelling in Sling
![Page 10: Synthetic models](https://reader034.vdocument.in/reader034/viewer/2022042700/5594447f1a28abfa2f8b474f/html5/thumbnails/10.jpg)
DOMAIN MODELLING
![Page 11: Synthetic models](https://reader034.vdocument.in/reader034/viewer/2022042700/5594447f1a28abfa2f8b474f/html5/thumbnails/11.jpg)
• With Sling, domain modelling should be done against resources
• Each Resource is a collection of properties
– Entirely defined in the repository
– Uniquely identified by it’s sling:resourceType
• The domain model thus is a representation of the type and properties
![Page 12: Synthetic models](https://reader034.vdocument.in/reader034/viewer/2022042700/5594447f1a28abfa2f8b474f/html5/thumbnails/12.jpg)
• There are MANY ways to implement your domain model
– Direct access to resource properties from the view
– A custom mapping framework
– Cognifide’s Slice framework
– Sling Models (available in AEM6 as a standard)
– Neba.io My new favorite!
![Page 13: Synthetic models](https://reader034.vdocument.in/reader034/viewer/2022042700/5594447f1a28abfa2f8b474f/html5/thumbnails/13.jpg)
What if your domain is NOT uniquely defined in the repository?
• Legacy Apps
• 3rd party services
• CRMs
• ERPs
![Page 14: Synthetic models](https://reader034.vdocument.in/reader034/viewer/2022042700/5594447f1a28abfa2f8b474f/html5/thumbnails/14.jpg)
Ask yourselves:
• How do we bring this data into our application?
• How do we expose it to the UI?
• How do we maintain Sling’s RESTful principles?
question mark on road: image via Flickr/ milos milosevic
![Page 15: Synthetic models](https://reader034.vdocument.in/reader034/viewer/2022042700/5594447f1a28abfa2f8b474f/html5/thumbnails/15.jpg)
SYNTHETIC MODELS
![Page 16: Synthetic models](https://reader034.vdocument.in/reader034/viewer/2022042700/5594447f1a28abfa2f8b474f/html5/thumbnails/16.jpg)
• Synthetic Models
– Define domain objects where the domain is not exclusive to the content
repository
– Their property collection is defined against all data sources
– Must be resolvable by Sling’s resource resolution mechanism
– Leverage the Presentation Model design pattern
– Name is inspired by SyntheticResource
![Page 17: Synthetic models](https://reader034.vdocument.in/reader034/viewer/2022042700/5594447f1a28abfa2f8b474f/html5/thumbnails/17.jpg)
![Page 18: Synthetic models](https://reader034.vdocument.in/reader034/viewer/2022042700/5594447f1a28abfa2f8b474f/html5/thumbnails/18.jpg)
• Synthetic Model
– They inherently rely on Sling’s resource resolution mechanism
– A ResourceProvider is required for each external domain object
• Think of this as writing a service to expose the external data
– These ResourceProviders will return either an instance of SyntheticResource or
a custom Resource implementation
• I’d advise caution when deciding on a custom Resource implementation
![Page 19: Synthetic models](https://reader034.vdocument.in/reader034/viewer/2022042700/5594447f1a28abfa2f8b474f/html5/thumbnails/19.jpg)
Writing your Synthetic Models with Neba.io
• Using Neba.io makes it easy!
– In reality you can achieve this with most of the mapping frameworks especially
Sling Models
• Annotation driven
• Integrates the Spring Framework in Sling
• Main features
– Resource Models
– Spring MVC
![Page 20: Synthetic models](https://reader034.vdocument.in/reader034/viewer/2022042700/5594447f1a28abfa2f8b474f/html5/thumbnails/20.jpg)
@ResourceModel(types = "/apps/synthetic-models/components/book")
public class Book {
@This
private Resource resource;
@Path("/content/book-store/bookinfo/${isbn}")
private Resource bookInfo;
private String isbn;
private String imagePath;
private String title;
private String description;
private String genre;
private String author;
The Synthetic Model
A JCR resource reference
External resource reference
Synthetic Model Example
![Page 21: Synthetic models](https://reader034.vdocument.in/reader034/viewer/2022042700/5594447f1a28abfa2f8b474f/html5/thumbnails/21.jpg)
@Component(metatype = false)
@Service
@Properties({
@Property(name="service.description", value="A DB backed Book Resource"),
@Property(name="service.vendor", value="Odecee Pty Ltd"),
@Property(name=ResourceProvider.ROOTS, value="/content/book-store/bookinfo"),
@Property(name=SlingConstants.PROPERTY_RESOURCE_TYPE, value="/apps/synthetic-
models/components/book/book.jsp")
})
public class BookResourceProviderImpl implements BookResourceProvider {
ResourceProvider example
![Page 22: Synthetic models](https://reader034.vdocument.in/reader034/viewer/2022042700/5594447f1a28abfa2f8b474f/html5/thumbnails/22.jpg)
DEMO
![Page 23: Synthetic models](https://reader034.vdocument.in/reader034/viewer/2022042700/5594447f1a28abfa2f8b474f/html5/thumbnails/23.jpg)
QUESTIONS?
![Page 25: Synthetic models](https://reader034.vdocument.in/reader034/viewer/2022042700/5594447f1a28abfa2f8b474f/html5/thumbnails/25.jpg)