Skip to content

Can anyone suggest how to remove deprecated registry class and what are the alternative methods?

I’m performing an upgrade to Magento 2.4 and the registry class has been deprecated but it’s being used in some custom modules, please see the code below:

<?php declare(strict_types=1);

namespace TPCarsModel;

use TPCarsModelResourceModelFacilityCollection;
use TPCarsApiApiDataFacilityInterface;
use TPCarsApiApiDataFacilityInterfaceFactory;
use MagentoFrameworkApiDataObjectHelper;
use MagentoFrameworkModelAbstractModel;
use MagentoFrameworkModelContext;
use MagentoFrameworkRegistry;

class Facility extends AbstractModel
{
    protected $_eventPrefix = 'TP_vehicle_facility';

    private DataObjectHelper $dataObjectHelper;

    private FacilityInterfaceFactory $facilityDataFactory;

    /**
     * @param array<mixed> $data
     */
    public function __construct(
        Context $context,
        Registry $registry,
        FacilityInterfaceFactory $facilityDataFactory,
        DataObjectHelper $dataObjectHelper,
        ResourceModelFacility $resource,
        Collection $resourceCollection,
        array $data = []
    ) {
        parent::__construct(
            $context,
            $registry,
            $resource,
            $resourceCollection,
            $data
        );
        $this->facilityDataFactory = $facilityDataFactory;
        $this->dataObjectHelper = $dataObjectHelper;
    }

    public function getDataModel(): FacilityInterface
    {
        $facilityData = $this->getData();

        $facilityDataObject = $this->facilityDataFactory->create();
        $this->dataObjectHelper->populateWithArray(
            $facilityDataObject,
            $facilityData,
            FacilityInterface::class
        );

        return $facilityDataObject;
    }
}

Can you suggest how I can eliminate the use of the registry class?