MVC : Fully populated models or Partially Filled Models

mvc

This one has haunted me for so long. When doing MVC programming what do you think is the better programming practice? Should one use fully populated models or the partially filled ones, especially when I know that for this particular task I am going to need only 2 fields from the model object which has 5 others?

Sometimes it just seems criminal to fill a list of 20 model objects with all values from database when you know that you are going to need only a few of them.

Of course partial model means that you will have to write one more method in your DAO apart from the one which fetches everything. Which mean more code to maintain?

On the other hand pulling everything from DB with fully populated models means one method serves all but this is obviously going to give you some performance overhead.

I can see ORM (such as Hibernate or ActiveRecord of Rails) favoring trends in MVC programming and databases like Google’s BigTable full models is accepted trend. But what if you are still using good old JDBC?

Hardware is cheap, development is costly. Is that really true even when the app needs to scale to few hundred thousand requests per hour?

Best Answer

You have two options:

1) Leave some fields in the model unfilled

2) Create an extra "lite" model for your specific situation

Which one to choose depends on the two things again:

a) How many fields of the "full" model are going to be ignored

b) How often this "lite" model will be instantiated

If there are just a couple or more fields that can be unfilled, it is okay to go with 1).

If b) is just an exceptional individual situation, there is perhaps no point in creating an extra model just for one usage case.

Another approach is to define a "lite" model and inherit the "full" model from it.

Related Topic