5

While working i found there are two methods of getting customer data using two session classes
1.) \Magento\Customer\Model\SessionFactory $customerSession
2.) \Magento\Customer\Model\Session $customerSession

Most posts prefers to use the second one but does not work when Full Page Cache is enable.

So what is the best method for practice.

Thanks.

2 Answers 2

4

There are two ways of creating object:

1) Model \Magento\Customer\Model\Session

2) Factory \Magento\Customer\Model\SessionFactory

If you use Model class, it will always create shared objects it will not create a new object, but what if you want to use new object of this class, use factory method

Factory method will be used to instantiate an object. In Magento 2, the Magento 2 Factory Object do the same thing. The Factory class name is the name of Model class and append with the Factory word.

So for example, we will have SessionFactory class. You must not create this class. Magento will create it for you. Whenever Magento’s object manager encounters a class name that ends in the word ‘Factory’, it will automatically generate the Factory class in the var/generation folder if the class does not already exist.

This factory class is also dependent on the ObjectManager class. When you will load the class or run magento2 compilation using CLI:

php bin/magento setup:di:compile

Before Magento 2.2

var/generation/{Vendor}/{Module}/Model/MyModelFactory.php

After Magento 2.2

generated/{Vendor}/{Module}/Model/MyModelFactory.php

You should always use factories for non-injectable objects, All the entities such as Product, Customer are non-injectable and classes such as EventManager and all the Management classes are injectable in magento2.

Injectable objects don’t have identities but in the case of non-injectable objects they have identities, so you don’t know which instance you need at which time, so you must use Factory classes to inject non-injectable objects.

Here is the example factory class that is generated for Magento\Customer\Model\Customer, inside var/generation/Magento/Customer/Model/CustomerFactory :

<?php
namespace Magento\Customer\Model;

/**
 * Factory class for @see \Magento\Customer\Model\Customer
 */
class CustomerFactory
{
    /**
     * Object Manager instance
     *
     * @var \Magento\Framework\ObjectManagerInterface
     */
    protected $_objectManager = null;

    /**
     * Instance name to create
     *
     * @var string
     */
    protected $_instanceName = null;

    /**
     * Factory constructor
     *
     * @param \Magento\Framework\ObjectManagerInterface $objectManager
     * @param string $instanceName
     */
    public function __construct(\Magento\Framework\ObjectManagerInterface $objectManager, $instanceName = '\\Magento\\Customer\\Model\\Customer')
    {
        $this->_objectManager = $objectManager;
        $this->_instanceName = $instanceName;
    }

    /**
     * Create class instance with specified parameters
     *
     * @param array $data
     * @return \Magento\Customer\Model\Customer
     */
    public function create(array $data = array())
    {
        return $this->_objectManager->create($this->_instanceName, $data);
    }
}

Source: webkul, Google

4
  • 1
    thank you for your explanation.Can you elaborate about non-injectable objects and the identities they posses.
    – Knight017
    May 25, 2018 at 6:35
  • @Knight017 Injectable objects are singleton service objects obtained through dependency injection. Injectable objects can depend on other injectable objects. Non-injectable, objects are objects that cannot be injected. They are obtained by creating a new class instance every time they are needed. Check this for more details: devdocs.magento.com/guides/v2.2/extension-dev-guide/… May 25, 2018 at 6:59
  • 1
    nice answer, appreciated.! May 25, 2018 at 7:18
  • But does the session factory form a risk? Since you are avoiding the depersonalization when FPC is enabled May 6, 2019 at 13:43
1

Factory definition:

Factories are service classes that instantiate non-injectable classes, that is, models that represent a database entity. They create a layer of abstraction between the ObjectManager and business code.

For example:

It seems like asking the difference between car manufacturing plant and car. A manufacturing plant is a place where the cars will be produced.

Similarly, SessionFactory is an instance which will create session instances.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.