Difference Between Session and SessionFactory in Magento 2

customercustomer-sessionfactoryfull-page-cachemagento2

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.

Best Answer

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

Related Topic