It's not mandatory to run setup:di:compile command every time but if you have done any code changes related to factory methods, proxy, added plugins or any code compilations then you have to run this command. It will recompile the dependency injections.
magento setup:di:compile in order to generate necessary files. Both options ends up with generating classes in MAGENTO_ROOT/var/generation directory.
What classes are generated?
Factories are used to instantiate objects that can not be injected automatically. For example, a product object has to be loaded from the database, but dependency injection container doesn’t have enough information to create this object. That’s why we use factories.
Magento 2 uses constructor injection in which all dependencies are required. You cannot instantiate an object without passing all dependencies. What if you’d like to have optional dependencies? That’s why proxies exist.
Simply put, plugins are the primary customization mechanisms for Magento 2. No more class rewrites. It allows you to hook in and do something before, after or around any public method of the application.
when you run setup:di:compile command it do below things
Code compilation consists of all of the following in no particular order:
- Application code generation (factories, proxies, and so on)
- Area configuration aggregation (that is, optimized dependency injection configurations per area)
- Interceptor generation (that is, optimized code generation of interceptors)
- Interception cache generation Repositories code generation (that is, generated code for APIs)
- Service data attributes generation (that is, generated extension classes for data objects)