views:

44

answers:

3

Im designing a MVC framework but I dont know where should I put my utility classes. I read somewhere where it said utility classes are Model Classes. Is this line of thinking correct or are they some separate type of class?

A: 

If it isn't a representation of stored data, then it's probably not a Model class. Even then, if it still represents stored data of some sort, but behaves radically different from the rest of your Models, it may not belong with the rest of the Models, anyway.

Utility functions can be part of the whichever class they are utilities to, either Model, View or Controller. If they're a bit more generic, then they kind of live "outside" the MVC design.

staticsan
The Model is not just the database!
Gordon
You know, when I first started on SO, I did encounter quite a lot of arguments and confusion about what the Model layer did in MVC. After a while, I came to the realisation that there were a whole lot of wrong ways to do MVC. This was a little after I started maintaining an old codebase that did MVC in at least three different, bad ways. However, the one thing that was the most coherent was that The Model **modelled stored data objects**. This didn't mean it was just an ORM layer, but they should know how they interact with other and how to store and retrieve their data.
staticsan
A: 

Model class can contain your data access code, application logic. so it is okay to place in Model.

The best approach is to create a folder Utility in Model folder and place utility class there.

Adeel
+1  A: 

It depends on what these Utility classes are doing. The main idea of MVC is to separate the presentation layer (V and C) from the remaining application (M). If your Utility classes handle things related to the presentational layer, place them in an appropriate package, for instance ViewHelpers or InputHandlers. If the Utility functions relate to anything else, see to which layer in your model they best fit into, for instance DataAccess Utilities go into the DAO package while a a MailHelper might go into the the Services package, and so on.

Gordon
I read a book where it said that Models are the classes from a business layer and/or utilities. The idea of having a ViewHelper seems like making a hybrid of Controllers and Views. What do you think?
rix501
@rix501 A Controller handles user input from the UI and delegates requests to the model. A [ViewHelper](http://java.sun.com/blueprints/corej2eepatterns/Patterns/ViewHelper.html) should not handle user input. The main idea is to separate any logic needed by the View to complete rendering away from your view template. The distinction you read in the book is correct. The term Utilities is just vague though.
Gordon