views:

82

answers:

4

I'm developing basic GUI application. I have model class which counts time, I need to display this time to label in specific format. What is the right way to do it in according to MVC paradigm ? Logically I think it should be formatted in the view, but view is standard label control and implementing sub-label class seems a little bit overwhelming task for this case. Would it be right if I format it in the controller ? Or maybe I should format it in model?

+5  A: 

I see presentation as the View's job. Formatting according to the user's locale and preferences is clearly a presentation issue. It may seem like a bit of an overhead, but surely you end up with a reusable widget, that can be used in many Views? Many existing View frameworks would provide such functionality out of the box.

Just to add more thing ... in my world the Controller doesn't even know what data is displayed by the view. A model might make various pieces of information available, a view might choose only some of them. A date might not be displayed at all, or be represented by a nice little Calendar widget. In either case the Controller does not need to format the date. Let the View do his job, and get the data from the Model or DTO returned by the Model and passed on without interpretation by the Controller.

djna
I agree with this because if you're going to use an MVC-based framework, stick to the framework's methodologies. However, if you're trying to wrap MVC around something from scratch, this becomes more difficult.
jathanism
A: 

If you want to test the formatted time, then include the formatting logic in the controller.

Otherwise, it's fine to leave it in the view.

Jim G.
If my view can use a formatting library (consider JSPs and taglibs for example) those libraries can readily be tested, View logic may not actually be tied to hard-to-test visuals.
djna
@djna: I never suggested that the formatting logic would be difficult to test. But I *was* opining that unit tests should be directed at the controllers.
Jim G.
A: 

I would put internationalization formatting in the controller, as I believe that views are intended to simply show what they're given. This also gives the benefit of the controller being able to choose completely different views for different locales, if your application ever needs that.

Kaleb Brasee
So if this is an entry field rather than a display field, what knowledge does the View have? Can he pop up a suitable selection widget? When that selection widget returns a value and the field is to display it do we need to trip to the Controller?
djna
A: 

As per MVC pattern, here is what you might consider doing:

1: You run your model from the controller and get the count time 2: Controller gets the result from model and sends to view 3: You output the same variable on your view page.

That's it...

Sarfraz