Separate your data access logic from your controllers with the Repository pattern.
@Repository
public class ArticleRepository {
public LazyList<Article> findAll() {
return Article.findAll();
}
public LazyList<Article> findPublished() {
return Article.where("status = ?", 1);
}
public LazyList<Article> findByAuthor(Integer userId) {
return Article.where("user_id = ?", userId);
}
public Article findById(Integer id) {
return Article.findById(id);
}
public void create(String title, String content) {
Article article = new Article();
article.setTitle(title);
article.setContent(content);
article.setStatus(1);
article.saveIt();
}
}
Use repositories for any data retrieval or persistence logic. It keeps your controllers clean and makes testing easier. Any class annotated with @Repository is automatically available for injection via @Inject or as a method parameter.