Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

ModelProvider (described in details at ModelProvider) is a core interface in Slice and is used for providing objects (models). Its functionality is very similar to injector's getInstance(); however it is extended by a resource, so that object fields can be mapped using specified resource (or path). A simple use of ModelProvider:

 

Code Block
languagejava
linenumberstrue
ModelProvider modelProvider = injector.getInstance(ModelProvider.class);
TextModel model = modelProvider.get(TextModel.class, request.getResource());
 

ModelProvider is accessible through injector (line 1). The whole magic is done in line 2. modelProvider#get takes two parameters:

...

The @SliceResource annotation is used for marking classes which should be mapped from current resource by built-in object-resource Mapper. An example class looks like this:

...

Code Block
languagejava
linenumberstrue
@SliceResource
public class TextModel {
 
    @JcrProperty
    private String text;
 
    public String getText() {
        return text;
    }
}
 

Fields annotated by @JcrProperty will be mapped with corresponding properties from current resource. This way, once modelProvider#get method has been called (as in example above), the returned object is mapped and its text field stores a value of the current resource's text property.

Summary

The three subsections above described very briefly core mechanisms of Slice. To sum it up, take a look at the image below which put all these things together.

...