How to automatically register classes into DIC
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
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.
- Nette DI 3.1: transition release
- How to pass app directory paths to services
- Services don't need names
- Intro to dependency injection
- Powerful Dependency Injection Container
- DI and property injection
- DI and passing of dependencies
- DI versus Service Locator
- DI versus Lazy loading
- What is Dependency Injection?