views:

273

answers:

1

Given a View uses a Presenter, and in the presenter you have a model.

How do you inject the model into the presenter? If I was to inject it at the View level, you're back to square one with business logic being in the view - aka the view should not know about its model.

Any advice?

+2  A: 

You must be referring to the Passive View pattern. In the Supervising Controller pattern the view does communicate with model for synchronization.

For Passive View You are correct. You would typically do this either in your Main function or in a configuration class that you could call from Main. Since no language was specified I've written the example in C#.

static void Main(string[] args)
{
    Model model = new Model();
    View view = new View();
    Presenter presenter = new Presenter(view, model);
}

public Presenter(IView view, IModel model)
{
    this.View = view;
    this.View.Presenter = this;
    this.Model = model;
}

Of course this is a gross oversimplification. In a real world application the presenter would depend on domain objects which abstract the model and you'd likely be using an IoC container to handle the configuration. Some IoC containers can even handle circular dependencies in constructor parameters, negating the need for property injection.

codeelegance
Yes, you are right. It is the Passive View I was using.That's excellent though. I can't believe I didn't think about creating everything together - as you've done in your Main method.Cheers
Finglas