Page 1 of 1

One way.. or another..

Posted: Wed Jul 26, 2006 5:03 pm
by Jenk
(Apologies for vague title.. it just so happened I was listening to Blondie at the time..)

I'm unsure which way my application should operate when it comes to View <-> Model.

Should my View Controller/Class ask for the page variables from the Model, or should the Model feed them to the View?

E.g.:

Code: Select all

$model = new Model(new View);
vs:

Code: Select all

$view = new View(new Model);
should it be the former and my View class implement:

Code: Select all

interface iView
{
    public function assignVariable ($var, $val);
    public function setTemplate ($template);
}
or, the latter, all my model classes will implement:

Code: Select all

interface iModel
{
    public function getTemplate ();
    public function getVariables ();
}
I'm at a bit of a cross roads with it, because in all other scenarios, it's always take, not give, but with my view <-> model interaction, it's give..

It works, but it's bugging me.. I like uniformity but also I like to meld with 'the norm', and as I need to revamp one of my projects this would be an ideal time to make this change, however being as indecisive as I am.. what do you guys think?

Posted: Wed Jul 26, 2006 5:20 pm
by John Cartwright
My personal preference is to pass the model to the view, so I end up with something like

Code: Select all

class IndexView extends ApplicationView
{
   function __construct($model = false)   
   {
      if ($model)
      {
         $this->model = $model; 
      }
   }

   function setNewBlock($max)
   {
      $this->set('latestnews', $this->model->getNewsTitles($max));
   }
}
I know a lot of people prefer to have the controller directly interacting with the model, but this way I can throw in some hidden logic that doesn't belong into the controller.

Posted: Wed Jul 26, 2006 5:27 pm
by Christopher
Definitely pass the Model to the VIew. This is the most important dependency to get right in programming. The View is in the Presentation Layer and the Model is in the Domain Layer. You want to fight to eliminate/minimize the Domain Layer having any dependencies on the Presentation Layer. Likewise the Data/Service Layer should not have any dependencies on the Domain Layer.

There are rare exceptions, and I would recommend implementing a Helper object to do that if necessary to document what is going on.

Posted: Wed Jul 26, 2006 5:38 pm
by Ambush Commander
I tend to think the View depends on the Model: if the View changes, the Model shouldn't have to change. ;-)

Posted: Wed Jul 26, 2006 7:17 pm
by Jenk
Thanks guys, and JCart's example has totally opened my eyes in realising that the View doesn't have to be the same class for every page/action.

Posted: Thu Jul 27, 2006 3:59 am
by Jenk
right.. so now I'm using classes as per similar to JCart's example..

I have a base View class with the template engine assignment, and Model assignment, variable assignment, template assignment yada yada...

I then extend with a sub-class that is page/action specific for generating the content and loading into the template engine..

For the model, I have a base class for DB object assignment and User object assignment, and again extend with sub-classes specific for each page/action.

It's all so clear all of a sudden.