Skip to content

Getting a Product model from db

I’ve just started in the world of Magento and throughout the process of many questions I have came across one its proving dificult to find the best description for it.

Considering the following snippet, which of the methods is preferable in terms of more optimised?
Can I also assume both would equaly consume 1 db call?

Thanks

<?php

use MagentoFrameworkAppHelperAbstractHelper;

class MyHelper extends AbstractHelper
{  
  protected $_productRepository;
  protected $_modelFactory;

  public function __construct (
    MagentoFrameworkAppHelperContext $context,
    MagentoCatalogModelProductRepository $productRepository, 
    MagentoCatalogModelFactory $modelFactory
  ) {
    $this->_productRepository = $productRepository;
    $this->_modelFactory = $modelFactory;

    parent::__construct($context);
  }
  
  private function getProductByIdWithRepository ($id)
  {
    return $this->$productRepository->getById($id);
  }
  private function getProductByIdWithFactory ($id)
  {
    return $this->$_modelFactory->create('MagentoCatalogModelProduct')->load($id);
  }
}

?>