In some cases, you need to... But please only do this once you are sure of the consequences!
## Access the outer container from the inner one
Please note that this can create a dependency of the inner code on a user's code, leading to breaking the reusability principle.
Please stay away from this!
```yml
# model-container.neon
service:
-MyBrokenService(@outerContainer::getByType(WhateverServiceFromTheOuterContainer))# Do NOT do this unless the interface provided by the inner code from the inner container
## Accessing internals of the inner container from the outside
Accessing the internals from outside can be only helpful if you need to get some data for tests or manipulate some data. Otherwise, you can quickly break the app using an internal undocumented API (of this library and your inner container).
Please stay away from this!
```yml
service:
-ServiceRequiringTheServiceFromModelContainer(@model.container.getByName("the.service.name"))# Hey! never do this!
-ServiceRequiringTheServiceFromModelContainer(@model.container).getByType(WhatEverService)# Hey! never do this!
-ServiceRequiringTheModelContainer(@model.container)# Hey! This is not the way!
Split your app dependency injection container into well-defined scopes and enforce relationships between them.
Split your app dependency injection container into well-defined scopes and enforce relationships between them.
## Example
You have *model* where you want to expose access **only to** few facades. So you export them from *model* container into *app* container and the *app* then can **only access** those exported services. This means that *app* can no longer ignore ACL rules or directly access database.
## On the edge between monolith and microservices
## On the edge between monolith and microservices
...
@@ -26,8 +22,108 @@ So if needed you can easily turn already defined modules into microservices late
...
@@ -26,8 +22,108 @@ So if needed you can easily turn already defined modules into microservices late
## Example
You have *model* where you want to expose access **only to** few facades. So you export them from *model* container into *app* container and the *app* then can **only access** those exported services. This means that *app* can no longer ignore ACL rules or directly access database.
### Registering of model into the user DI container
For model container you need to create an DI extension, that will allow you to register all the services exported from the *model* to the application of the user (UI/API/CLI/...) part of the app.
```php
<?php
declare(strict_types=1);
namespaceApp\Infrastructure;
useMangoweb\NetteDIScope\ScopeExtension;
useNette\Configurator;
finalclassModelExtensionextendsScopeExtension
{
publicstaticfunctiongetTagName():string
{
// every service marketed with `exported` tag, will be available in every *user* DI container