Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should the business logic be separate from the model?

We're working on a fairly code-heavy PHP5 project, and in the last week I worked on a PoC of a RESTful API. We are separating Model Classes from Business Classes.

Trying to implement CRUD functionality, I discovered, that implementing CRUD against the Models directly would be pretty straight-forward while implementing it against the Business Logic isn't, since its functionality is specific to the currently existing Views and its interface doesn't provide the general data access model I need for implementing the API.

Thinking about this, the following questions came to my mind:

  • What is the best way to interact with the data, keeping the flexibility of the model and keeping the functionality the model doesn't care about at the moment (like sending a mail with an activation link when changing the email address)?

  • Working a lot with django previously, where most of the business logic is implemented in the model, why should you keep the business logic separate anyway? Do you have any real-life examples? What problems does this solve, if any?

Some possible solutions came to my mind, too:

  • Putting the whole business logic into the model. Checking which fields changed after / before calling the save method.
  • Using the observer pattern to notify the business objects of changes in the model and interact with the models directly.

What are the pros and cons in your opinion, what would you do?

like image 691
stefreak Avatar asked Jul 20 '12 23:07

stefreak


People also ask

Should business logic be in the model?

For non trivial applications, business logic/business rules/data access should not be placed directly into Models, Views, or Controllers. To do so would be placing business logic in your presentation layer and thus reducing reuse and maintainability of your code.

Is it important to separate business logic from system logic?

Ensuring UI and business logic separation accelerates development as layers can be developed in parallel, and it reduces the "brain burden" for a developer working on one part of the stack.

Where do you put your business logic typically?

Business logic should live in the data model. And, what's more, it should live in the graph data model because that's the right abstraction for the next twenty years. If you've been paying attention to this blog or to Stardog generally, then you must have known this is where we were going to end up.

Should you put business logic in controller?

Let me tell you this, controllers shouldn't do anything remotely related to business logic, and directly access data stores. The controller's only purpose is to receive a request and return a response. Everything that goes in between is not its responsibility.


1 Answers

If you separate out your storage in data a data access layer you get the separation you want and the functionality in the model:

DAL (doing queries etc)
  |
Model (doing business)
  |
Controller
  |
View

So in the view you have an action: Mark as Paid. So you controller gets the request (POST) /invoice/1/markaspaid (or any other url structure you use). Then the controller calls the model:

$Invoice=new Invoice();
$Invoice->markAsPaid(1);

Then your model calls the DAL to actually store this change. There is no real need to separate this from the models. If it is very complex or very transactional you might think about a separate service for a complex task. That way your model gets thinner and the complex part gets separated.

What is the best way to interact with the data, keeping the flexibility of the model and keeping the functionality the model doesn't care about at the moment (like sending a mail with an activation link when changing the email address)?

I don't understand this totally. As far as I see it you should separate the send e-mail process anyway from your normal code run. So put it in a queue and find it out there. It isn't part of your normal code path. You might initiate it from your model but that's about it.

See this question which has relevant info on that topic: Cakephp cron job to call a controller's action

like image 60
Luc Franken Avatar answered Oct 08 '22 01:10

Luc Franken