Akash Gupta
Akash Gupta

Reputation: 21

Usage of sling model

Which one of the following a better way of defining a sling model and why?

@Model(adaptables=Resource.class)   
public interface MyModel {  

     @Inject   
     String getPropertyName();   
}  

OR

@Model(adaptables=Resource.class)  
public class MyModel {  

     @Inject  
     private String propertyName;   
}  

Can you tell me a defined use case for using an interface as a model when all the methods are to be overridden in all the implementation classes?

Upvotes: 2

Views: 597

Answers (2)

Tim
Tim

Reputation: 327

It strongly depends on the usage. In the case where you add the annotation to the getter you could also go with an interface instead of a class.

When you want to get a data attribute and manipulate it e.g. shorten a string or something, then it makes sense to inject it in a variable and then use a getter to return the shortened string.

Upvotes: 0

Florian Salihovic
Florian Salihovic

Reputation: 3951

Use an interface when you access values of the ValueMap without any need to provide an additional view of the data. Class based models are used, when you need to apply transformations to the data or add additional data (via OSGI services etc).

Upvotes: 2

Related Questions