Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using MVC + Repository Pattern, where Business Logic should be?

I want to know the right concept about it. If I have a MVC application with Repository Pattern, where the BL should be?

  • Should it be inside the Model? Model should have all the business logic before call the unitofwork to insert or not the data into database?

  • Should it be in the controller? Before call the model?

  • Should I have a service layer to do the business logic and decide if I should call the Model to call the UnitOfWork to save the data?

A good explanation will help a lot too.

like image 265
Leandro De Mello Fagundes Avatar asked Dec 19 '22 07:12

Leandro De Mello Fagundes


1 Answers

The short answer - it depends. If it's a fairly complex or sizable application, I like to create a service layer project with the repositories as dependencies. If it's a small application, I'll put the logic in the controller. In my opinion, if it takes more time and effort to create the service layer than it would be to create the application (i.e. one or two controllers), then it doesn't make sense to me to go that route. You also need to consider the likelihood that the application will grow. What might start small could grow into something much bigger and in that case, again, it might be more beneficial to create the separate service layer.

like image 57
Matt M Avatar answered Dec 28 '22 23:12

Matt M