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 ?

2

1 Answer 1

16

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.

2
  • 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. Commented Jul 21, 2015 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
    Commented Jul 21, 2015 at 22:23

Your Answer

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

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