All entries for September 2005
September 23, 2005
MVC Views; how stupid should they be
It seems to me that a lot of people are not utilising the full power of the "view" concept of the MVC paradigm.
For example, imagine a scenario where you want to render one page for logged in users or another for logged out users. Should this logic go in the controller, or the view? It seems the gut reaction is to put it into the controller, which I think is wrong :)
Controllers should be doing business logic, with complete disregard for how it is rendered. Controllers should not know whether they are returning html, rss etc. Of course this is naieve and it may well be that different models are required, in which case the controller has to know, but in the main, keep those MVC boundaries!
In the readonly/readwrite view example, I typically have a delegating view:
public final class PagePermissionsView implements View {
private View readView;
private View editView;
public void render(
final Map model,
final HttpServletRequest request,
final HttpServletResponse response) throws Exception {
ServletRequestAttributeAccessor accessor = new
boolean userLoggedIn = ….
if (userLoggedIn) {
editView.render(model, request, response);
} else {
readView.render(model, request, response);
}
}
public void setEditView(final View detailsView) {
this.editView = detailsView;
}
public void setReadView(final View simpleView) {
this.readView = simpleView;
}
}
This keeps view related logic firmly in the view.
Same with complicated reports. If you need to group data, a lot of developers will perform that grouping in the controller. I would rather do that in the jsp and utilise tag libraries to perform the data, for example, I very often have need of a tag which will take in a collection and return a map, where the key is the value of a property, and the value in the map is a collection of all the objects that matched.
I know exactly why developers are "afraid" to put logic into views, and that is because of the horror that was pre MVC :) But there is a difference between business logic and view logic.
A useful yardstick for any "where should this go" question is "if I need to change how this works, which layers would I need to change". If I decide to change how the view is rendered, I want to change the view, not the controller :)
Rant over; let the flames being…..
September 21, 2005
Spring web flow progresses :)
Writing about web page http://jroller.com/page/kdonald?entry=web_flows_as_application_modules
About pigging time Keith :)
Now hurry up and release it :)
September 20, 2005
Another interesting blog
Writing about web page http://dynamicsemantics.blog-city.com/top5principles.htm
September 18, 2005
Post about my new 4ft marine tank
Writing about web page http://www.reefsuk.org/forum/viewtopic.php?p=1232#1232
enjoy.September 16, 2005
Interesting blog
Writing about web page http://www.jroller.com/page/sdevijver?entry=processing_e_mail_messages_with
Very funny ;)
Writing about web page http://reefsuk.org/forum/viewtopic.php?t=168&highlight=
(nicked of another web site)
WHY I FIRED MY SECRETARY…
LAST WEEK WAS MY BIRTHDAY AND I DIDN'T FEEL VERY WELL
WAKING UP THAT MORNING. I WENT DOWNSTAIRS FOR BREAKFAST HOPING MY WIFE WOULD BE PLEASANT AND SAY, "HAPPY BIRTHDAY!", AND POSSIBLY HAVE A PRESENT FOR ME. AS IT TURNED OUT, SHE BARELY SAID GOOD MORNING, LET ALONE "HAPPY BIRTHDAY."
I THOUGHT… WELL, THAT'S MARRIAGE FOR YOU, BUT THE KIDS WILL REMEMBER. MY KIDS CAME INTO BREAKFAST AND DIDN'T SAY A
WORD. SO WHEN I LEFT FOR THE OFFICE, I WAS FEELING PRETTY LOW AND SOMEWHAT DESPONDENT.
AS I WALKED INTO MY OFFICE, MY SECRETARY JANE SAID,
"GOOD MORNING, BOSS, HAPPY BIRTHDAY!" IT FELT A LITTLE BETTER THAT AT LEAST SOMEONE HAD REMEMBERED.
I WORKED UNTIL ONE O'CLOCK AND THEN JANE KNOCKED ON MY DOOR AND SAID, "YOU KNOW, IT'S SUCH A BEAUTIFUL DAY OUTSIDE, AND IT'S YOUR BIRTHDAY, LET'S GO OUT TO LUNCH, JUST YOU AND ME."
I SAID, "THANKS JANE, THAT'S THE GREATEST THING I'VE HEARD ALL DAY. LET'S GO!"
WE WENT TO LUNCH. BUT WE DIDN'T GO WHERE WE NORMALLY WOULD GO. WE DINED INSTEAD AT A LITTLE PLACE WITH A PRIVATE
TABLE. WE HAD TWO MARTINIS EACH AND I ENJOYED THE MEAL TREMENDOUSLY.
ON THE WAY BACK TO THE OFFICE, JANE SAID, "YOU KNOW, IT'S SUCH A BEAUTIFUL DAY… WE DON'T NEED TO GO BACK TO THE OFFICE, DO WE?"
I RESPONDED, "I GUESS NOT. WHAT DO YOU HAVE IN MIND?" SHE SAID, "LET'S GO TO MY APARTMENT."
AFTER ARRIVING AT HER APARTMENT JANE TURNED TO ME AND SAID, "BOSS, IF YOU DON'T MIND, I'M GOING TO STEP INTO THE BEDROOM
FOR A MOMENT. I'LL BE RIGHT BACK."
"OK." I NERVOUSLY REPLIED.
SHE WENT INTO THE BEDROOM AND, AFTER A COUPLE OF
MINUTES, SHE CAME OUT CARRYING A HUGE BIRTHDAY CAKE… FOLLOWED BY MY WIFE, KIDS, AND DOZENS OF MY FRIENDS AND CO-WORKERS, ALL SINGING "HAPPY BIRTHDAY".
AND I JUST SAT THERE…
ON THE COUCH…
NAKED.
September 14, 2005
package structure
Writing about web page http://forum.springframework.org/viewtopic.php?p=34496#34496
Excellent new article on the spring forum about package structure. This is an area that I think developers really don't pay enough attention to.
I will be interested to see how this plays out….