One of the most common things you will do as a programmer is query for stuff, such as data in the database. This is what we were doing a lot in Drupal 7 to get our data. A lot. We'd either use the database API or simple query strings and load our data. However, in Drupal 8 the entity API has become much more robust and offers a layer that reduces the need to query the database directly. In a later chapter, we will see how to do that still for when things become more complex. For now, since most of our structured data belongs in entities, we will use the entity query system for retrieving entities.
The service we use for querying entities is the entity.query service (QueryFactory). So, we can inject this into our class or use it statically:
$query = \Drupal::entityQuery('node')
Here, node is the entity type for which we want to query.
However, we also have the option of using the entity_type.manager service (EntityTypeManager) and get the query factory from it:
\Drupal::entityTypeManager()->getStorage('node')->getQuery();
We request the storage handler (remember what that is from when we talked about entity type handlers?) which then can give us the query factory for that entity type.
The reason we might want to do that is that the entity query will always return IDs of the entities found. So, if we want to actually load them, we still need the EntityTypeManager to do so. So, why would we want to always inject both of these services into our class? In the preceding examples, I used static calls but, as always, you should inject the services where you can.