Top Banner
When Sightly Meets Slice Tomasz Niedźwiedź Software Engineer
40

When Sightly Meets Slice by Tomasz Niedźwiedź

Apr 16, 2017

Download

Technology

AEM HUB
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: When Sightly Meets Slice by Tomasz Niedźwiedź

When Sightly Meets SliceTomasz Niedźwiedź

Software Engineer

Page 2: When Sightly Meets Slice by Tomasz Niedźwiedź
Page 3: When Sightly Meets Slice by Tomasz Niedźwiedź

<%@ include file="/libs/foundation/global.jsp" %><% // Some Java %><script> //JavaScript anyone? </script><style> /* Inline CSS rules! */ </style><% // Some more Java %><h1>If you’re careful enough, you can find HTML</h1><c:if test="someVariableSetSomewhere"> <p>HTML elements are best mixed with JSP tags</p></c:if>

Java Server Pages – The Good, the Bad and the Very Ugly

Page 4: When Sightly Meets Slice by Tomasz Niedźwiedź

<%@ include file="/libs/foundation/global.jsp" %><% // Some Java %><script> //JavaScript anyone? </script><style> /* Inline CSS rules! */ </style><% // Some more Java %><h1>If you’re careful enough, you can find HTML</h1><c:if test="someVariableSetSomewhere"> <p>HTML elements are best mixed with JSP tags</p></c:if>

Java Server Pages – The Good, the Bad and the Very Ugly

Page 5: When Sightly Meets Slice by Tomasz Niedźwiedź

• Almost plain HTML• Iteration• Hiding/showing elements• Including scripts• Access to bindings• Calling backend code

A sightly, minimalistic beauty

Page 6: When Sightly Meets Slice by Tomasz Niedźwiedź

<h1 data-sly-test="${properties.jcr:title}"> ${properties.jcr:title}</h1>

${inheritedPageProperties.twitterHandle}

<h1> ${properties.jcr:title || "Default title"}</h1>

A sightly, minimalistic beautyAccess to JCR data, really basic logic

Page 7: When Sightly Meets Slice by Tomasz Niedźwiedź

<ul data-sly-list="${ currentPage.listChildren }"> <li>${item.name}</li></ul>

<div data-sly-repeat="${currentPage.listChildren}"> ${item.name}</div>

data-sly-listdata-sly-repeat (AEM 6.1)

A sightly, minimalistic beautyIteration

Page 8: When Sightly Meets Slice by Tomasz Niedźwiedź

<template data-sly-template.validateBlank="${@validatableWithBlank, name}"> <sly data-sly-test="${!validatableWithBlank.blank}" data-sly-call="${validate @ validatable=validatableWithBlank, name=name}" data-sly-unwrap /> <div data-sly-test="${validatableWithBlank.blank && wcmmode.edit}" data-sly-include="blank.html" class="blank"> </div></template>

data-sly-templatedata-sly-calldata-sly-includedata-sly-resource

A sightly, minimalistic beautyCode reuse

Page 9: When Sightly Meets Slice by Tomasz Niedźwiedź

• Markup is clearly visible and easy to understand• Easy to learn, simple, with just the necessary set of features• Cannot affect the state of the application• Good momentum, regular improvements

What I like about it

Page 10: When Sightly Meets Slice by Tomasz Niedźwiedź

Sightly

Use interface

WCM BindingsAdapter Factories

Sling ModelsSliceJSP

Page 11: When Sightly Meets Slice by Tomasz Niedźwiedź

SliceCQ 5.3CQ 5.4CQ 5.6AEM 6.0AEM 6.1...and onwards

Page 12: When Sightly Meets Slice by Tomasz Niedźwiedź

Repository access@SliceResourcepublic class ArticleModel {

@JcrProperty("jcr:title") private String title;

@JcrProperty private String text;

//getters}

Mapping content to POJOs...

Page 13: When Sightly Meets Slice by Tomasz Niedźwiedź

Repository access@SliceResourcepublic class ArticleModel {

@JcrProperty private PublicationMetadata publicationMetadata;

@JcrProperty @Children(Author.class) private List<Author> authors;

//getters}

Slice

Page 14: When Sightly Meets Slice by Tomasz Niedźwiedź

Repository access@Model(adaptables = Resource.class)public class ArticleModel {

@Inject private PublicationMetadata publicationMetadata;

@Inject private List<Author> authors;

//getters}

Sling Models

Page 15: When Sightly Meets Slice by Tomasz Niedźwiedź

Repository accessNeat! But...

Page 16: When Sightly Meets Slice by Tomasz Niedźwiedź

Repository accessNeat! But...

tomasz.niedzwiedz
Consider removing
Page 17: When Sightly Meets Slice by Tomasz Niedźwiedź

Slice – Does it still count?The landscape has changed

Sling Models provide an excellent way to supply components with data

Using Java classes in scipts is easier than ever

Page 18: When Sightly Meets Slice by Tomasz Niedźwiedź

Simple models are

not really the point!

Page 19: When Sightly Meets Slice by Tomasz Niedźwiedź

The killer feature of Slice?

Page 20: When Sightly Meets Slice by Tomasz Niedźwiedź

The killer feature of Slice?It’s built around Guice

Page 21: When Sightly Meets Slice by Tomasz Niedźwiedź

• The focus is not on repository access.

• It’s about a clear, well-established dependency injection model based on industry standards.

• Guice provides a consistent framework for the entire application.

• JSR 330

The killer feature of Slice?It’s built around Guice

Page 22: When Sightly Meets Slice by Tomasz Niedźwiedź

Proven to speed up development in many projects over the last several years.

The killer feature of Slice?

Page 23: When Sightly Meets Slice by Tomasz Niedźwiedź

• How many objects are to be created?

• What’s allowed to share instances?• How much control does the

programmer have?

Dependency injection with GuiceScopes

tomasz.niedzwiedz
Too much detail - replace with links?
Page 24: When Sightly Meets Slice by Tomasz Niedźwiedź

• Method interception

Dependency injection with GuiceAspect Oriented Programming

tomasz.niedzwiedz
Too much detail - replace with links?
Page 25: When Sightly Meets Slice by Tomasz Niedźwiedź

Dependency injection with GuiceBindings

tomasz.niedzwiedz
Too much detail - replace with links?
Page 26: When Sightly Meets Slice by Tomasz Niedźwiedź

• Slice core• Slice Addons• CQ 5.5• CQ 5.6• AEM 6.0 (also compatible with

6.1)

Slice brings Guice to AEM

Page 27: When Sightly Meets Slice by Tomasz Niedźwiedź

1. Instantiate an injector in your bundle activator

2. Register OOTB modules3. Create a new module for each

logical unit of functionality4. Let the injector take care of

wiring up all of the dependencies in your whole project

How to set it up

tomasz.niedzwiedz
Wyrównać to
Page 28: When Sightly Meets Slice by Tomasz Niedźwiedź

• Verbose• Complex• Exposes services to the whole

environment.• Sometimes you want simple things that

you want to hide and yet be able to inject them.

What about OSGi?It does have certain dependency injection capabilities.

Page 29: When Sightly Meets Slice by Tomasz Niedźwiedź

Peaberry used before Slice 4.1

@OsgiService annotation in newer versions

Slice & OSGi

public class MyClass {

@Inject @OsgiService ResourceResolverFactory factory; //...}

OSGi services can be injected with minimal effort

Page 30: When Sightly Meets Slice by Tomasz Niedźwiedź

• Object trees can be built thanks to dependency injection

• Not as powerful as Guice• AOP• Context management• Caching

• Less information available online on good practices and how the model works

DI in Sling ModelsPerfectly sufficient for backing your sightly scripts... but

Page 31: When Sightly Meets Slice by Tomasz Niedźwiedź

• Not a mature library• Closed• Competes with Sightly or Sling

Misconceptions

Page 32: When Sightly Meets Slice by Tomasz Niedźwiedź

Sightly & SliceCan they work together?

Page 33: When Sightly Meets Slice by Tomasz Niedźwiedź

Sightly & SliceCan they work together?

YES

Page 34: When Sightly Meets Slice by Tomasz Niedźwiedź

Using Slice in Sightly

<div data-sly-use.model="com.example.app.ArticleModel"> <span class="text">${model.text}</span>     <ul data-sly-list="${model.links}">         <li><a href="${item.path}">${item.title}</a></li>     </ul></div>

Sightly without Slice

Page 35: When Sightly Meets Slice by Tomasz Niedźwiedź

Using Slice in Sightly

<div data-sly-use.model="com.example.app.ArticleModel"> <span class="text">${model.text}</span>     <ul data-sly-list="${model.links}">         <li><a href="${item.path}">${item.title}</a></li>     </ul></div>

Sightly WITH Slice... Exactly the same

Page 36: When Sightly Meets Slice by Tomasz Niedźwiedź

Slice & Sightly• Very clear presentation layer• Visibility of markup semantics and

structure• Easy for frontent developers• Easily manageable architecture of

the AEM project• Minimal boilerplate code• Fine-grained control over objects

Page 37: When Sightly Meets Slice by Tomasz Niedźwiedź

Learn MoreSlice 4.3 (Q3 2015)• Slice persistence• New Felix console tools

Slice 5.0 (Q1 2016)• Easier consumption of models in

Servlets and Services• Mapper improvements

http://cognifide.github.io/slice/

Page 38: When Sightly Meets Slice by Tomasz Niedźwiedź

Learn MoreSlice your code in Sightly - http://goo.gl/5HTKalA presentation introducing Google Guice - https://youtu.be/hBVJbzAagfs

Page 39: When Sightly Meets Slice by Tomasz Niedźwiedź

Q&A

Page 40: When Sightly Meets Slice by Tomasz Niedźwiedź

Tomasz NiedźwiedźSoftware Engineer at Cognifide

[email protected] @toniedzwiedz

Thanks for listening!