As an architect with Adobe Consulting Services, I have the opportunity to see a wide variety of projects. Integrating domain model classes into AEM/CQ projects remains one of the repeated struggles I’ve seen over the past few years. In many cases, such classes are not necessary – the Sling Resource and JCR APIs provide a sufficient set of domain classes. However, many projects, especially those with sufficient complexity, are well served by a rich domain model.
Back in December, I introduced a new sub-project within the Apache Sling project to deal with this problem. Developed first under the working name of YAMF (for Yet Another Model Factory), and then renamed to the more formal Sling Models, this module allows you to use a mix of standard and custom annotations to easily adapt standard Sling objects—such as Resource and SlingHttpServletRequest objects—into domain classes without any boilerplate code. You write your classes or interfaces, annotate them, and the necessary plumbing is created dynamically for you.
Sling Models allows you to write readable, highly testable code, and eliminates the need for creating mock Resource or Node objects. Although the primary use case for Sling Models is performing mapping Sling Resource objects, as a framework it is not limited to a particular type of source object. Resource and SlingHttpServletRequest are supported using the existing injectors and new injectors can be created for additional types as necessary.