The ProductInterface has a DI preference leading it the ProductModel. While ProductModel is marked as falling under the Backward Compatibility Standard (it is marked with @api), it is still belonging to the Persistence Layer and not the Domain Layer, so hence it should not be used directly. At least, that's my understanding of the Persistence Layer. In other words, injecting ProductInterface in your class constructor is to be discouraged (and perhaps even prevented): Instead, you should be injecting ProductRepositoryInterface in your class constructor. And while using the repository, you might get your hands on ProductInterface anyway ($product = $productRepository->get(...)) - something that I would call "indirect injection" (I'm making up things as I write).

Here's the question: If the only usage of ProductInterface is triggered via other classes (a repository or perhaps a factory) and if injecting ProductInterface in the constructor should not be done, why is there a DI preference for it?

  • Magento 2 is following SOLID principle , where last dependency inversion principle to remove concretion of class , so in my opinion you can elaborate your question taking care of that as well , it will be good – Shashank Gupta Jan 20 '18 at 21:37
  • I don't know what you exactly mean here. But maybe you are just commenting to support the question. So to extend upon it: It is good to use SOLID and dependency inversion is indeed good: So using the ProductInterface when somebody is actually receiving a ProductModel is complying to that. But you don't need a DI preference there. The DI preference is only needed if you inject ProductInterface and want the Object Manager to resolve this to become ProductModel. But you're not supposed to do that (?). So hence, the preference would actually not be needed. – Jisse Reitsma Jan 22 '18 at 7:00

Your Answer

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

Browse other questions tagged or ask your own question.