3

There are many interfaces in the Magento 2 core that not configured as di.xml preferences. Examples are

  • \Magento\Checkout\Model\Cart\CartInterface
  • \Magento\Catalog\Model\Layer\FilterableAttributeListInterface
  • \Magento\Catalog\Model\Layer\ContextInterface
  • ...

All of these interfaces have implementations, there just is no preference.

Because no preferences are configured for these interfaces they need to be explicitly supplied to the object manager when it is creating the object graph for integration tests.

I understand that in many cases there can be no preference that fits all cases, but just choosing one of the available implementations more or less randomly when writing the tests seem to work.

If possible, I would like to know the reasoning behind choosing not to configure a preference for an interface. It would be great if someone from the core team maybe could elaborate. Thanks!

2

Only reason not to configure the preference is if you have multiple equal implementations and for every client you have to decide which implementation will be provided. So putting a preference does not make much sense and in some conditions might lead to developer mistake.

\Magento\Catalog\Model\Layer\FilterableAttributeListInterface and \Magento\Catalog\Model\Layer\ContextInterface are examples of such interfaces.

\Magento\Checkout\Model\Cart\CartInterface does not have preference by mistake: it was created after its implementations and all client code was not modified to use it.

1
  • Thank you for the information! I found the Layer implementations and found workable solutions. Good to know regarding the CartInterface, too.
    – Vinai
    Jan 14 '16 at 16:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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