I am creating an application which will have a very large database to draw it's information from (a student registration system to be exact). The database is created with Microsoft SQL Server if that matters. I plan to implement the MVC pattern in this, but I am unsure of how to handle accessing the database.
My current understanding of how it should work is this (please correct me if there is a better way to do it):
- I have a model (for example User) that contains a few attributes and some business logic.
- When I need to view or edit the data in the table, I get the relevant data and put it in a model, change it and then 'save' the changes to the database. So for example if I need to change a student's name I will search the database for that student (by ID number) and instantiate a new User object. I would then change the Name property of the User object and run a 'save' method that uploads the data to the database.
I guess I have two questions: firstly am I understanding how the MVC pattern works with data driven applications and secondly where does the code for getting the data from the database to instantiate a model go? Does it go in the controller for said model? Does it go in some other static class? Is it a constructor for the model?
-
3In short, the question of how to getting data to and from the database is completely outside the scope of the MVC model. The MVC pattern is about how to structure the user interface layer, how to get data from the model to the view and vice-versa. But how the model is populated and persisted is outside the pattern.JacquesB– JacquesB04/13/2016 12:32:28Commented Apr 13, 2016 at 12:32
-
You should look into Patterns of Enterprise Application Architecture (PoEAA, as it's known) by Martin Fowler. Particularly, things like the MVC pattern, Data Transfer Objects (DTOs) and the Data Mapper, etc.Tersosauros– Tersosauros04/15/2016 13:10:43Commented Apr 15, 2016 at 13:10
1 Answer 1
You actually need to create a Data Access Layer
:
1- Create a New Class Library [ProjectName].DataAccess
2- Create a Class Named UserManager
.
3- Create a Method called AddUser
that takes the User Model as an argument.
and inside that method, you write the logic to insert the user to the database.
Depending on the Scale of your system, you might want to Create a Bussiness Layer
(Class Library)
that you use in your controller in order to Call one of the data Access Layers (SQL or Oracle or etc).
If you reach that complexity, you might need to create interfaces with (AddUser, GetUser, DeleteUser) and provide
both SQL and Oracle Class Implementation of that interface and choose one from your configuration.
If you need more details, please let me know.
Edit After your Comment:
You may have a GetUsers()
and GetUserByID(int id)
or by Name
Whatever. That all depends on what you need in your UI and what functionalities you're giving to the user.
However, you should not Query all data from database and then filter what the user requested via code, you should only query the data you need from database. Unless you're showing the complete list in UI and then providing Search functionality on the screen.
Whether to pull a complete table from the database is something to think about before doing.
In real applications, you have to consider the size of the table, if you have a huge one you may need to do some
pre-filtering (where the end-user selects a department for example and then you Call GetUsersByDepartment(int depId)
).
Anyway, you can have a option to query all the users if the end-user requests that through your app screen, but he might need to wait for the data to be pulled. All these methods should be in you Data Access Layer
for now considering your case of not having a Business Layer
.
-
Ok, that's just what I need. This is a second year university project, so implementing an interface so that the database can be accessed with Oracle as well isn't really necessary. I have one more question though. Should I wish to get a list of users rather than just a single one, this code would be GetUserList(), etc. and would go in the DataAccess library, right? Is there a better way to do it?Omegastick– Omegastick04/13/2016 19:52:26Commented Apr 13, 2016 at 19:52
-