DataObjectFactory ================= * Published: 2019-07-12 * Author: Nickolas Burr .. contents:: Table of Contents :local: Related ------- * :doc:`DateTimeFactory` * :doc:`ExceptionFactory` Description ----------- The ``DataObject`` class plays an integral role in Magento as the data container for CRUD operations. Likewise, there are circumstances where you need to interact with data injected via ``di.xml``, but don't want to introduce tighter coupling caused by extending the ``DataObject`` class. This is where a ``DataObjectFactory`` can come in handy. It provides a compositional approach to interacting with injected data. In the usage example below, we have a ``$container`` property that will store our injected data in a ``DataObject``. By using this approach, you not only have looser coupling, but you can control access to the data with the appropriate visibility settings for ``$container`` and ``getContainer``. Usage ----- .. code-block:: php container = $dataObjectFactory->create($data); } /** * @return DataObject|null */ protected function getContainer(): ?DataObject { return $this->container; } /** * @return array */ public function getCountries(): array { return $this->getContainer()->getData('countries') ?? []; } /** * @return array */ public function getCities(): array { return $this->getContainer()->getData('cities') ?? []; } } .. code-block:: xml US England France Italy New York London Paris Rome