Click here to Skip to main content
15,885,366 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Iam new to programming in MVC arch and little confused as to where do we write code to insert or update database. In Modal or controller.
Thank you in advance
Posted
Comments
[no name] 23-Jul-14 8:48am    
Well I would say that most people would do it the model.
Christian Amado 23-Jul-14 8:56am    
My wrong comment here: Controller. In the model you have your classes. In the controller you have "the control" of your model and views.

:(
CHill60 23-Jul-14 9:09am    
I thought the Controller was the part that handled User interaction and that the Model was completely decoupled and provided the business logic, including knowledge of the database??
Richard MacCutchan 23-Jul-14 9:16am    
Nice to know I'm not the only one who's confused about this. :)
CHill60 23-Jul-14 9:19am    
And I thought "patterns" were meant to make life easier :-) Strikes me that MVC (and others) is a little like a solution waiting for a problem to present itself.

I have to agree with Wes Aday on this one and did some research to back that up ...

http://www.asp.net/mvc/tutorials/mvc-music-store/mvc-music-store-part-4[^]

http://www.w3schools.com/aspnet/mvc_intro.asp[^]

To quote the latter site
Quote:
The Model is the part of the application that handles the logic for the application data.
Often model objects retrieve data (and store data) from a database.

The View is the parts of the application that handles the display of the data.
Most often the views are created from the model data.

The Controller is the part of the application that handles user interaction.
Typically controllers read data from a view, control user input, and send input data to the model.

Edit - another source Learning MVC - Part 1: Introduction to MVC Architecture and Separation of Concerns[^]
Quote:
MVC does not specifically mention the data access layer because it is understood to be encapsulated by the Model.
 
Share this answer
 
v2
Comments
Christian Amado 23-Jul-14 9:49am    
You're right. Everyday we learn something new ;) Thanks
You have raised a very good question and an ambigous one. The main focus is to make our application loosely coupled, else if it is tightly coupled then we have to compromise on many things. SO, better follow the
"REPOSITORY PATTERN"

What is this??
Now We have models, that is our classes for the project. We have controllers, but remember never use DB operations in the Controllers, its bad. It makes tight coupling. So have "Service Methods" & "Repositories" they will handle all the DB operations i.e. in coding language The Context will play its role in the Services/Repositories.
Use them creating the constructors for the services interfaces in the controller and access them.
Remember a main thing here to understand is Dependency injection that handles the Parameterless Constructor issue. Please read about Dependency Injection. This will surely help you do good coding.

Thanks.
 
Share this answer
 
You write the code inside Model. Model contains classes, validations and operations. Controller interacts with the View-Model (in this case calls to your Database operation).

For example, you can see this link[^].

I wrote a wrong comment :(

Thanks to @CHill60 and @Wes Aday for their comments.
 
Share this answer
 
v2
It depends,

I follow different strategy, In model I only writes static classes, which I generally use for dynamic partial views and direct calling the methods without intimating the controller. and In controller I write the methods to do all the other functionality like inserting the data in the database getting the value from the database (in sort CRUD).
 
Share this answer
 
Comments
CHill60 23-Jul-14 10:36am    
There are many ways to achieve things, but I wouldn't say this one was true to the MVC pattern/concept - you've now tightly coupled the controller to the database design - but the model is also tightly coupled to the database design - two things have to change if there are any database changes. And you are also implying that you are bypassing the controller and having Views call DAL methods directly - again, not truly MVC. I'll accept that the DAL stuff ought to be separated out but imho the DAL "calls" really belong in the Model (and never in the View)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900