I have gone through a couple of tutorials in Magento 2, and this confuses me a little. I can see there are basically two ways by which we can read/write business entities:
Retrieve Data
Using A Factory Approach
$object = $this->myFactory->create();
$object->load($myId);
Using A Repository Approach
$repo = $this->myRepository();
$object = $repo->getById($myId);
Save Data
Using A Factory Approach
$object = $this->myFactory->create();
$object->load($myId);
$object->setData('something', 'somethingDifferent')->save();
Using A Repository Approach
$repo = $this->myRepository();
$object = $repo->getById($myId);
$object->setData('something', 'somethingDifferent');
$repo->save($object);
I can also see that, both a repository and a factory class can be injected using dependency injection. This is confusing at least for me.
When should we use a repository approach and a factory approach? What is the best practice we need to follow?