D
D
dmitriy2016-10-19 13:04:54
PHP
dmitriy, 2016-10-19 13:04:54

What is the best way to use infrastructure code?

There are classes designed depending on the subject area, for example EmailServer, MailBox, Mail, Attachment,
these classes are endowed with the appropriate properties / methods according to the tasks of the business logic. For example, receiving emails looks like $Mails = (new MailServer($host,$login,$password))->getMalBox("INBOX")->getMails();
that is, it is clear that the Server Object contains mail directories, receiving mail is possible through the INBOX (Incoming) directory as an example.
It is clear that the MailServer class is contained in the constructor clearly what this model needs.
There is a situation to introduce code that does not belong to the projected subject area and is more related to the infrastructure, for example, the Logger classes, for logging something, Config which can contain project settings, Profiler which collects information about the operation of the application, etc.
I see options:
1. Clog constructors with these infrastructure code dependencies
2. Inject dependencies through the Container, but again, all classes must have this dependency
3. Use the singleton Container $Logger = Continer::get_instance()->get("Logger");
4. Use a facade class that will work with models and infrastructure code, something like MailService

Answer the question

In order to leave comments, you need to log in

1 answer(s)
@
@smple, 2016-10-19
_

The options you specified have the right to exist, I would also supplement the list
5. use a Proxy object
6. Use di and, based on the di settings, slip the implementation and parameters from the configs.
For example, your MailServer has an interface MailServerInterface from which you will use it everywhere, you have reference implementation of MailServer and you need to do method logging.
```php
MailServerProxy implements MailServerInterface {
public function __construct (LoggerInterface $logger, MailServerInterface $server) {
$this->setLogger($logger);
$this->setMailServer($server);
}
public function __call($name, $args) {
$this->logger->info('called method ' . $name);
return call_user_func_array([$this->mailServer, $name], $args);
}
}
```
Sample code template.
Then, where MailServerInterface is needed, di will create it, and already di, depending on the system settings, will create dependencies, for example, if you need to log, it will create MailServerProxy which needs Logger as arguments (any parameters can be here) and MailServer (and it needs $ arguments host, $login, $password which are in the config and will be read by di)
And if logging is not needed, it is enough to create MailServer immediately in di settings when requesting MailServerInterface, or use some NullLogger that does not write anything.
there is also aspect-oriented programming https://github.com/goaop/framework
but it works slower because you need to read the annotations through reflection but it can also be not bad.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question