0

I am trying to understand how to implement MVC in winforms so I have put together the following;

  1. A main form which is divided into 2 panes (left/right) the left pane will display a list of customers.
  2. An order details form which will list all the orders placed for the selected customer on the main form.This order details form will be displayed in the right pane of the main form.

Now, I have defined a view interface for each and a presenter. How should the order details presenter get a hold of the selected customer in the other view?

1
  • @JohnFx. Why the change from MVP to MVC Commented Dec 15, 2009 at 22:12

3 Answers 3

1

I agree with pm100 in that there has to be a publish/subscribe mechanism.

In order to observe the 'separation of concerns' principle, the form (or view if you will), should define a number of events, of which your controller (or presenter in MVP) can hook up to.

If in the left pane you change selection, this will trigger the 'LeftPane_SelectionChanged' event which the controller is hooked up to and acts on, in this case obtaining the details of the selected item (which can be passed as event args), collating the selected customer's orders in a 'model' object and once obtained, the model can then be 'posted' back to the view (form) by means of invoking a 'rightPanel_load' routine and passing that model.

The form/view should remain as lightweight and dumb as possible, leaving all business logic to be defined in the other now decoupled middleware layers and act purely on events.

On mobile at the minute so posting examples isn't easy but will update in due course.

Have a good read on MVC architecture and also on object oriented design principles (SOLID is a good starting point), this should get you in the right mindset for it and once you 'get it', you'll have that lightbulb moment, the fog clears and suddenly you'll be flying!

Sign up to request clarification or add additional context in comments.

Comments

0

You need a publish and subscribe mechanism.

Each view can announce its state changes (selectionchange event for example).

Interested components can subscribe to those events.

This decouples the two views - it means you could have a additional panes that shows the web page of the customer, graph of order history, photo of salesman...

1 Comment

In terms of MVP would it be the order details presenter that would have to subscribe to the main view event, if so how would it achieve this.
0

a) define an event - say selectionchange. If should be high level and expressed in terms of the model data

b) each view with a selection (your left pane for example) exposes this event

c) any view that wants to subscribe (your right pane for example) attaches itself to the event exposed by (b)

You should read up on defining and using events (most good c# books will have this)

Where it gets interesting is then having some form of brokerage system so that consumers can find event publishers, this removes the need for (c) to know the name of (b)

For your simple 2 pane system what I have described is maybe overkill, but it will be interesting to implement and adds more long term flexibility

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.