How to automatically register classes into DIC

5 years ago by Rick Strafy  

Many of you didn't know the fact that Nette 3 has a built-in extension for automatic registration of your classes to dependency injection container. Such extension can save you time writing hundreds of lines of neon configurations where you register classes with similar names (<Something>Factory, <Something>Repository and so on).

Let's say that you have /app/Model directory where is all business logic of your application, almost every class is service (maybe except entities and some data classes). Most of your services are named like ArticleRepository, UserRepository, ArticleFactory, ArticleFacade, you may have noticed, that your services have Repository, Facade, Factory extensions in their names, and you still register them one by one in neon files.

This simple neon configuration can spare you a lot of time and nerves, then you'll only need to register those services which need to pass some parameters (google/facebook oauth libraries, recaptcha…).

search:
	section:
		in: %appDir%/Model
		classes:
			- *Facade
			- *Factory
			- *Repository

Complete configuration guide with a lot of customized options (excluding classes, registering only classes that extend/implement other classes and so on) can be found in the documentation.