L
L
l4m3r2018-04-14 14:13:21
Laravel
l4m3r, 2018-04-14 14:13:21

How to separate business logic?

3 questions

  1. How to get away from fat models? For example, I have a User model that can have a thousand lines. They say you need a service layer. But I don’t understand how to organize it at the level of files and folders and how it should look like. Maybe some code examples?
  2. Let's say I want to implement the PayPal service on the site (it communicates via curl with their server). In Yii, for example, I created a PayPal class file in the components folder and implemented methods there. I added it to the config and then used it as Yii::$app->paypal in the controller and in the model (again, business logic in the controller and model). Pretty primitive. How do I competently create such a module in Laravel. In what folder? Does it need to be divided into contract/implementation if there is only one implementation? (Let's assume that you can create a single contract for all payment systems)
  3. Why is there no Model folder in laravel and models are added to App. Know what you can create yourself? But why is it done? The Model folder is a deprecated structure and is there a better way to organize the models?

Answer the question

In order to leave comments, you need to log in

10 answer(s)
M
Majesko, 2018-04-15
@l4m3r

1. Create a Services folder
2. Separate business logic parts into services
3. Write services and connect via Service Provider to Service Container
4. Write facades (optional)
5. ...
6. PROFIT

D
dmitriy, 2018-04-14
@dmitriylanets

1. I would implement work with PayPal as a framework independent library (at least a minimum of dependencies) to start with in src/paypal after implementation I would connect via composer
2. The service level is not work with PayPal specifically, but work with the payment system, that is, Paypal would walked like a driver

use App\Services;

class Payment {

  protected $payment;

  public function __construct(\App\Contracts\PaymentDriverInterface $payment) {
    $this->payment = $payment;
  }

  public function createPaymentUrl() {
    return $this->payment->getUrl();
  }

}

namespace PayPal;

class PayPalDriver implements \App\Contracts\PaymentDriverInterface {

  public function getUrl() {
    //реализация
  }

}

E
Eugene Pedya, 2018-04-19
@fpinger

Neither Yii nor Laravel follow DDD. Don't bother. Do it either within the framework, or if you know DDD, then within it. Only you will be driven "with a shovel on the back."
Now Peter's publishing house has published a yellow book with a bee. She is good. This is not an advertisement. Enough to stick with it.

K
Konstantin B., 2018-04-14
@Kostik_1993

I think you're overthinking. First, is it true that what you have in the models is business logic? If yes, then we create the Services folder and in it you can have some classes for processing something, let's say a class for unlinking or linking an account to a social network

class UserSocialServices
{
    public function createOrGetUser() {}
    public function associate() {}
}
// или можно
class PayPall {
    public function createPaymentUrl() {}
}

With regards to non-business logic, but I think that you have 50 to 50 there, then it can be taken out into traits, especially those that can be duplicated in different models. In general, there is no clear division into folders, this is your project, the main thing is that the folders are named clearly, and their contents do not contradict them

A
Antonio Solo, 2018-04-15
@solotony

1) business logic for 10,000 lines - I would be worried. scroll for a long time. and 1000 ....
here one customer told me that I have "large files" - I told him - "are we making changes to the TK and doing a reassessment?" he seemed to calm down.
2) paypal has never business logic
3) ask Taylor a question. he answers.

A
ALIAKSANDR ZHALIAZOUSKI, 2018-04-14
@nouomen

In zf2, you can clearly see how services work

S
spaceatmoon, 2018-04-14
@spaceatmoon

1. In general, a question from the category of "Criticizing offer." Who says what he says is already the third. If you yourself feel that the model can be thinned out, something thrown into the API class for this model, then do it.
2. There is no business logic in the PayPal integration, this is nonsense. But query methods can be stored in one class, and configurations in another, data usage in the third.
3. Why is there no folder? Because. This does not mean that you need to blindly follow any paradigm, it's just someone who came up with it.
You must feel for yourself where everything should lie and how. My middle class takes up to 500 lines.

A
Alexander, 2018-04-15
@Sassoft

Implement something like a command bus and add specific commands and handlers

G
gaparchi, 2018-04-19
@gaparchi

Correctly fpinger writes. DDD is designed to address these issues. But you have to read books carefully.

A
Artem Spiridonov, 2018-04-19
@customtema

I did it.
True, I thought that I would manage in a couple of months, but it took a couple of years: nextframework.ru

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question