Reputation: 101
I'm new to java based web applications. I have to create a dynamic web project using MVC structure. So far I created three packages and some java classes like below. I have some doubts about these things
Can someone explain the difference between model classes and DAO classes
Since I'm using MVC structure do I really need to create a separate package for DAOs or is it okay to put all the DAO class implementations inside the Model class ? Because MVC means Model-View-Controller
Upvotes: 7
Views: 7002
Reputation: 2266
DAO classes talk to both your persistence system (generally a database) and your controller, and move instances of your model classes between them. Model classes are a representation of the real world stuff that you're working with (patients, doctors and appointments for a hospital management app, for example, or clients, accounts and so on for a bank app). Ideally, your model classes shouldn't even know that there's a DAO. So yes, it makes sense to put them in different packages since they are different things. Do note that even if your app follows the MVC pattern, that doesn't mean your app only concerns itself with the view, the model and the controller. You can and usually will have other concerns, like persistence, that belong in their own layer.
One other thing you probably want to do, while we're at it, is have DTOs. They are an additional representation of your model entities (but they should go in their own package, not the model package), but DTOs needn't have the same properties as your model classes. DTOs are what the controller should feed to the view (and viceversa). That way, your view doesn't know about your model, and you don't expose to the view layer any properties of your model classes that you don't want to expose. For example, if you have a model class for customers with information about their credit card number, but you have a view that doesn't need to show that credit card number, you can create a DTO that has the other customer data and use it for that view without sending the unneeded data to your view.
Upvotes: 7
Reputation: 131486
Can someone explain the difference between model classes and DAO classes
These are very different things.
DAO is a CRUD oriented data service (read/create/update/delete data) and model are objects representing data. DAO uses Model but not the reverse.
Since I'm using MVC structure do I really need to create a separate package for DAOs or is it okay to put all the DAO class implementations inside the Model class ?
Because DAO and models are two different concepts (while related), it appears clearer to separate their classes in two distinct packages.
Upvotes: 7