Should a controller have business logic?

Should a controller have business logic?

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. It first starts to get exponentially crazier when you’re also inserting data into your database from the controller.

Can I write logic in controller?

The kind of “business logic” that you can place in the controller layer is Application logic. Maybe not all application logic should go there. But you should never place domain logic in the controller layer.

What logic should be in controller?

Generally, the only types of logic you should have in your controller are: Session and cookie handling. This might also include authentication/authorization or any additional cookie processing you need to do. Model selection.

Why is it called business logic?

If you were a professional graphic designer, there certainly would be business involved with using your imaging application – your job is your business! So “business logic” refers to the parts of the code that define how the user conducts his business (in this case, manipulating images).

Where does business logic go in spring boot?

Generally, your business logic goes in the service layer. Though you can put basic validation rules in your pojos with JSR annotations. For a Spring MVC app you have controllers, which handle http requests, and a domain layer, which are pojos representing your business models.

Where do you put business logic?

The business logic should be placed in the model, and we should be aiming for fat models and skinny controllers. As a start point, we should start from the controller logic. For example: on update, your controller should direct your code to the method/service that delivers your changes to the model.

What should controller do?

The duties of a controller include assisting with the preparation of the operating budgets, overseeing financial reporting and performing essential duties relating to payroll. The controller has many tasks which might include preparing budgets and managing important budgeting schedules throughout an organization.

Is API a business logic?

The API is the facade. It should not contain the actual business logic of the task at hand. Instead, the real behavior should remain within the underlying Service class. An API Layer is the best place for cross-cutting concerns.

Where do business logic not exist in controllers?

Business Logic should not be contained in controllers. Controllers should be as skinny as possible, ideally follow the patter: Additionally controllers can contain some application logic. So where do I put my business logic? In Model. What is Model? Now that’s a good question.

How to use business logic in MVC Stack Overflow?

The user clicks the “delete” button next to one of the emails, the controller notifies the model to delete entry N, then notifies the view the model has changed. Perhaps the admin’s email should never be removed from the list. That’s a business rule, that knowledge belongs in the model.

Which is an example of business logic in design?

This is usually logic that differentiates what methods to call from the model and/or what arguments to pass them. Another example of this is a set of utility functions that exist in the controller that may work to format or sanitize data returned from the model, according to a set of business rules.

How much business logic should be in a model?

Ideally, anything that will be modifying, validating, or parsing your model should occur very near to- if not within- the model itself. For example, if you need to ‘clean’ a model object before you store it, consider having a SanitizeInputs () method on the model or as part of the service which handles storing the model.