Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where to write Database and Business logic in MVC?

As I am learning and working on Asp.Net MVC application, I want to know that what is the better place to write Business Logic and Data Access logic in MVC.

Where should I write DataAccess and Business Logic among three layers (Model, View and Controller) ??

Could anybody please tell me the correct way to write the code for this.

Scenario: I want to retrieve all the employees where employee name like 'Mi%' ( I have SQL procedure to execute and retrieve the data.)

PS: Want to know that where I should create instance of Business Logic class and where I should create instance of Data Access layers class?

Thanks in advance.

like image 553
nunu Avatar asked Sep 09 '10 18:09

nunu


People also ask

Where is business logic written in MVC?

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.

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 business logic go in controller?

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.

Should you put business logic in the database?

Two good reasons for putting the business logic in the database are: It secures your logic and data against additional applications that may access the database that don't implement similar logic.


2 Answers

Business logic (BL) and data access (DAO) should be in separate layers. Models should only keep data and contain no logic. Controller should only receive data from view and send it to BL layer (or send from BL to view).
It's not a strict rules, but most recently used approach

like image 161
kilonet Avatar answered Oct 13 '22 00:10

kilonet


Business logic should be in the Model.

Data Access can either be its own later your Controllers call, or automated in an ORM that your Controller will call through repositories.

A walk-through covering this can be found in Nerd Dinner, look for the free download section.

like image 45
blu Avatar answered Oct 13 '22 01:10

blu