magento2-blog-module-tutorial
magento2-blog-module-tutorial copied to clipboard
Follow best practises around data models
We should follow best practises set out by Magento by using data models to access our blog posts.
I'm going to implement the following
-
Ashsmith\Blog\Api\Data\PostInterface
(this already exists) -
Ashsmith\Blog\Api\PostRepositoryInterface
-
Ashsmith\Blog\Model\PostRepository
-
Ashsmith\Blog\Model\Data\Post
Then modify Ashsmith\Blog\Model\Post
so that it no longer uses the interface, and has the method getDataModel
.
PostRepositoryInterface
should define the following methods:
-
getById($postId)
-
getList(\Magento\Framework\Api\SearchCriteriaInterface $searchCriteria)
-
delete(\Ashsmith\Blog\Api\Data\PostInterface $post)
-
deleteById($postId)
Any reference to loading the collections, or the original Post model directly will be switched out for the PostRepository.