4

Magento 2 uses Object manager to create all objects.

Injectable

Object (typically a singleton) that can be instantiated by the object manager.

Non-injectable

Object that cannot be instantiated by the object manager. 

these statements are very confusing.can you any one explain ?

10

Doc should say "Object that should not be instantiated by object manager".

Injectables are usually stateless services without identity (EventManager, CustomerAccountManagementService). They can be asked for in constructor.

Non-injectables are usually entities (e.g. product, category) having identities and state. Because they have identities, so it is important with which exactly instance of that entity you want to load. When you ask for such entity in constructor OM will not be able to figure out which exactly instance of entity to provide you (Product 1 or Product2 or ProductX), so it will just provide empty product.

That is why entities are non-injectables and should not be asked for in constructor.

  • thank you for your response.i think if you pass factory of entity also incomplete.i mean still we don't know which product it is.may be its meaningful object(responsibility is create an object).so that's why its a injectable. – Sivakumar K Jul 21 '15 at 12:57
  • 2
    When you create object through factory it's in in your control, which exactly object you want to create. Best example is collection. When you want to load 20 products from database. Requesting 20 Product objects in constructor is wrong. You ask for factory and then crate 20 products and populate them with data from databes. – Anton Kril Jul 21 '15 at 22:23

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.