I've been wrestling with the project's layered architecture. The most tangled issue is how to introduce a third-party class library in a project
For example, there's a MSG controller to send mail
class MsgConstroller extends Constroller { public function send() { $email = new email([options....]); $email->send(); }}
This controller implements the ability to send email by introducing an email tool, but if you want to send a text message at the same time.
class MsgConstroller extends Constroller { public function send() { $email = new email([options....]); $sms = new sms([options....]); $email->send(); $sms->send(); } }
It's not so elegant, so I thought about using a service layer to solve
class MsgConstroller extends Constroller { public function send() { $msg = service('Msg'); //实例化一个Msg服务类 $msg->send('message,email,sms'); //通过send接口传的参数 会同时发送站内信、邮件、短信 } }
But this will also transfer the new operation to the service layer feeling is not how ideal so what are the simpler and more elegant solutions?
Reply content:
I've been wrestling with the project's layered architecture. The most tangled issue is how to introduce a third-party class library in a project
For example, there's a MSG controller to send mail
class MsgConstroller extends Constroller { public function send() { $email = new email([options....]); $email->send(); }}
This controller implements the ability to send email by introducing an email tool, but if you want to send a text message at the same time.
class MsgConstroller extends Constroller { public function send() { $email = new email([options....]); $sms = new sms([options....]); $email->send(); $sms->send(); } }
It's not so elegant, so I thought about using a service layer to solve
class MsgConstroller extends Constroller { public function send() { $msg = service('Msg'); //实例化一个Msg服务类 $msg->send('message,email,sms'); //通过send接口传的参数 会同时发送站内信、邮件、短信 } }
But this will also transfer the new operation to the service layer feeling is not how ideal so what are the simpler and more elegant solutions?
You've done your thing. Re-optimization can be wrapped in a message class with dependency injection, e-mail message all kinds of classes into private variables, the operation method of the parameter control is instantiated and assigned to a private variable, the Send method supports concatenating, used similar to
$message = new Message();$result = $message->send(array('mail', 'mail_address'))->send(array('sms', 'tel'));
Re-cut design mode is also original aim, and want to use the cool and want to upgrade the changes when good maintenance is no way out. Package granularity is too coarse and easy to change, too fine to be difficult to use.
One more thing, don't have anything to do with elegance, just two lines of programming, which is so much elegance.
How does SMS and e-mail implement the Send () method interface, and then use the composite design pattern to combine them?
class MsgConstroller extends Constroller { public function send() { $msg = new MsgComposite(); $msg->add(new email()); $msg->add(new sms()); $msg->send(); }}
New operation now the framework seems to like to use a unified servicelocator solution, convenient for unified replacement for unit testing
Similar to this requirement, the service can be used in Symfony2, and only the service can be configured to be called anywhere in the world:
$mailer = $this->get('mailer');
Similarly:
$sms = $this->get('sms');
Reference:
Http://symfony.com/doc/current/book/service_container.html