AEM Sling Models for factory and adaptable workflows
AEM Sling Models: Advanced Use Cases with Factory and Adaptable Annotations moves beyond the usual resource-to-component mapping and into explicit boundaries between content, services, and rendering code. A factory can construct a deliberate, typed view of a resource, while an adaptable model lets Sling resolve that view through adaptation. Together, they reduce hidden dependencies while preserving the speed of content-driven delivery. Learn more about Agenda.
For teams in Australia, these patterns are especially relevant when Sydney and Melbourne agencies operate across Adobe platforms while delivery teams in Brisbane, Adelaide, and Perth coordinate across AEST and AWST. Local procurement commonly considers AUD budgets and GST, but technical decisions must also fit the Java baseline of each AEM deployment. The resulting code should be understandable to an author working in Melbourne CBD and maintainable by a contractor in Sydney several years later.
Why factories matter in real components
Components often accumulate responsibilities through chains such as request.getResource().getChild("promotion"), followed by service lookups, locale checks, and fallback logic. A factory creates a boundary around that work. Its inputs are explicit, its output has a stable contract, and callers do not need to understand where each value came from.
Factories are particularly valuable for promotions, search results, personalised banners, and other views assembled from several sources. They can enforce site and locale rules, reject incomplete content consistently, and prevent a component from depending directly on an implementation service. The factory should remain focused on creation rather than becoming a container for business logic.
Historical material from the two-day CIRCUIT conference in Chicago remains useful when evaluating these design options. Teams can use the CIRCUIT app to revisit the original developer-focused programme before modernising a component.
Building a typed factory boundary
Start by defining the contract in terms the caller understands, rather than exposing Sling Models directly:
public interface PromotionFactory {
Promotion create(Resource resource, PromotionContext context);
}
The context can carry the site, locale, requested experience, and already-resolved dependencies. The implementation can be registered as an OSGi service using @Component(service = PromotionFactory.class). It then performs a narrow adaptation, maps the result into an immutable value object, and returns it to the component.
Keeping the returned Promotion separate from PromotionModel provides room to change the backing implementation without changing the rendering layer. It also makes unit testing straightforward because callers can provide a test double for the factory instead of constructing a complete request and resource tree.
The implementation must respect the platform baseline. An AEM 6.5 estate may still restrict the project to Java 8-compatible language features, whereas a current AEM as a Cloud Service deployment may support newer releases. Records, sealed classes, and newer collection APIs should only be introduced after the deployment baseline has been confirmed.
Understanding adaptable model metadata
Factory is not a standard Sling Models annotation, and adaptable configuration normally lives inside @Model rather than in a separate annotation. Custom adaptation uses ModelAdapterFactory, while an application-level factory usually remains an ordinary service interface. This distinction prevents teams from creating annotations that the Sling runtime cannot process.
A resource-oriented model commonly looks like this:
@Model(adaptables = { SlingConstants.ADAPTABLE_RESOURCE_TYPE })
public class PromotionModel {
@ValueMapValue
private String eyebrow;
@ValueMapValue
private String title;
}
Within @Model, adaptables describes the accepted source types, while adapters declares additional targets to which the model can be adapted. The resource-type marker gives Sling a selection rule based on content structure, avoiding a broad model that adapts successfully from every resource.
When the source is not a resource, such as a configuration object or domain service, a custom ModelAdapterFactory must support the source-and-target pair. That factory should have a narrow purpose and deterministic behaviour. Personalisation introduces further boundaries between authored content and machine-derived context; the AEM machine learning personalization discussion provides a useful adjacent example of why those concerns benefit from clear interfaces.
Injecting dependencies without hidden coupling
Sling Models offer convenient injection annotations. @ValueMapValue retrieves JCR properties, @SlingObject exposes the current resource, and @OSGiService supplies an OSGi dependency. Custom injectors can use @SelfInjected with @Named to select a particular value, while @PostConstruct is suitable for inexpensive derived fields and validation.
Convenience does not mean dependencies should be invisible. A factory can accept required collaborators through its context and pass them to internal adapters. This makes requirements visible at the boundary and gives tests precise control over each dependency. Network calls and remote service requests should not happen inside a constructor or post-construct method merely to populate a model.
The same separation appears in gateway design. The Ocelot gateway review illustrates how routing and service aggregation can be kept out of individual consumers, just as a Sling factory keeps service composition out of components.
Proving behaviour with focused tests
AEM Mocks and AemContext make it practical to test adaptation without starting an AEM instance. Register the model classes, adapter factories, and required OSGi services, then exercise the behaviour through a real resource and request context.
Tests should cover more than a successful happy path. They should confirm that a resource type does not adapt to an unrelated model, missing properties produce the intended fallback, malformed values are rejected consistently, and an unavailable service does not leave a partially constructed object. Factory tests should also verify that locale, site, or tenant context reaches the resulting value.
Continuous learning is valuable when teams support several Adobe codebases. A community programme such as the DevPulse programme can help practitioners share testing patterns beyond a single project. Australian teams often use informal phrases such as “no worries” or “fair enough” in stand-ups; that shorthand works well in conversation, while API names, test names, and exception messages should remain unambiguous.
Comparing the two modelling approaches
Neither approach is universally superior. A factory is strongest when creation involves several resources, services, or contextual rules. An adaptable model is strongest when a component needs a conventional, focused representation of one resource and the framework’s adaptation mechanism already provides the desired contract.
The decision should consider failure behaviour and maintenance cost as well as brevity. A factory that merely wraps one adaptTo call may add ceremony without creating a useful boundary, while a broad adaptable model can conceal dependencies that consumers do not realise they are using.
| Concern | Factory-led design | Adaptable-led design |
|---|---|---|
| Creation boundary | Explicit factory method with typed inputs | Framework-driven adaptTo call |
| Strongest use | Multi-source composition and contextual rules | Focused mapping from one resource type |
| Dependencies | Supplied through the factory or context | Declared through model annotations and injectors |
| Missing data | Explicit validation, fallback, or exception | Adaptation failure or model-level validation |
| Testing | Mock the factory and its collaborators | Mock resources, context, and registered services |
| Main risk | Excessive ceremony or hidden service-locator behavior | Broad adaptables and concealed coupling |
Make the next component a contract
Choose one component whose data currently crosses several layers, such as a personalised promotion. Write down its inputs, required context, expected output, and failure policy before changing its implementation. Then select either a factory-led or adaptable-led design and explain that choice in the component’s engineering documentation.
Implement the selected pattern behind the existing rendering API, add tests for missing content and unavailable dependencies, and publish the result in your team’s AEM repository. Share the contract at your next Sydney or Melbourne developer meet-up so authors and support engineers can use it confidently. Make the boundary visible, prove it with focused tests, and keep the next migration uneventful.