Representation of the leading representation of the model, how to transfer objects between representations?

Edit: Accepted the answer by Chris Holmes, but is always ready for refactoring if someone comes up with a better way! Thanks!

Running some winforms with MVP is the best way to pass an object to another view.

Say I have a CustomerSearchView/Presenter , in doubleClick I want to show CustomerEditView/Presenter . I do not want my view to know about the model, so I can not create a ctor that accepts ICustomer parameters.

my reflex will be

CustomerSearchView create a new CustomerEditView creating its own presenter. Then my CustomerSearchView will do something like:

 var customerEditView = new CustomerEditView(); customerEditView.Presenter.Customer = this.Presenter.SelectedCustomer; 

Another possible approach would be the CustomerDTO class and make a CustomerEditView that accepts one of these CustomerDTO , but I think a lot of the work is simple.

Sorry for the main question, but the whole example that I can find never reaches this point, and this is an abandoned background project, and the approach used so far gives me a headache ...

+7
c # mvp winforms
source share
5 answers

I don’t know exactly how you show your views, so it’s a little difficult to give you specific advice. Here's how I did it before:

What we have done is that CustomerSearchViewPresenter fires an event like OpenCustomer (customerId). (It is assumed that in your search view there will be only a few pieces of Client data, and customerId will be one of them. If all of the Client’s objects are listed in your search view, you can call OpenCustomer (client). But I wouldn’t create a search and the ability to fill it whole objects ... We keep our searches easy in terms of data.)

Elsewhere in the application, an event handler is used that listens for the OpenCustomer () event and performs the task of creating a new CustomerEditView w / Presenter (and I'm going to postpone it to my IoC container to do this for me, so I don't need to use the "new" keyword wherever). After creating the view, we can pass the identifier (or customer object) to the new CustomerEditView, and then show it.

This class, which is responsible for enumerating the OpenCustomer () event and performing the creation of the CustomerEditView, is usually a kind of Controller class in our application.

To simplify this situation, I did it differently: I create both the CustomerSearchView (& presenter) application and the CustomerEditView (& presenter) when the application or module starts. When CustomerSearchView needs to open the Client for editing, CustomerEditView becomes a responder to the OpenCustomer event and loads the data into itself and knows how to show itself in any container that it should do.

So there are several ways to do this.

+4
source share

What about:

 //In CustomerSearchPresenter var presenter = new CustomerEditPresenter(); var customerEditView = new CustomerEditView(presenter); presenter.SetCustomer(customer); //In CustomerEditPresenter public void SetCustomer(customer) { View.Name = customer.Name; View.Id = customer.Id; ... } 

Think that your request to find a client should simply delegate it to the host, you need to complete the action.

+1
source share

There are several important ideas for getting a natural flow in any MVP code:

  • This is the host who controls the presentation, not the other way around.
  • Because of 1. opinion should not know about the existence of a leader. Smaller dependencies usually mean easier maintenance.

In C #, I believe that events are a great advantage when decoupling presenters from views. More in the previous answer: Model-View-Presenter in WinForms

0
source share
  • I would look at MS Prism 4 and their beautiful navigation interface. Also check out Silverlight and WCF Navigation. They are well-made and handle things like user request for confirmation from dirty forms with cancellation.

  • I would look at the WCF PageFunction () documentation on how to “call” a page from another and get the information.

Here's how it works (javascript, sorry):

The user double-clicks the client on the list of clients:

 CustomerList.onDblClick(customerId){ app.fireEvent('customerEditRequest', id) } 

...

 app.onCustomerEditRequest(id){ this.mainRegion.requestNavigate('customers/edit', id); } 

If viewing the navigation for editing was successful ...

 CustomerEditView.onNavigatedTo(context){ this.model.load(context.parameters.id)); } CustomerEditView.onSaveButtonClick(){ this.model.save(); app.fireEvent('customerEdited', id); } 

...

 app.onCustomerEdited(id){ app.mainRegion.requestNavigate('customerlist', id); } 

There are several ways you could do this:

  • send a callback function to the edit form from the list of clients. the editable form will call it, and you will do what you want when it is called.

  • You have the option to edit the form at the customerEdited event that you listen to and respond to (no broadband)

  • Use a common event bus to manage events located centrally.

0
source share

I used to have connections with my presenters, but they moved away from this. It does not correspond to the initial definition of the template (not in itself the reason for the deviation of only a factor contributing to the determination of these advantages). Ideas should ideally be as stupid and with as few dependencies as possible. View should report w / Presenter (any "observers") through delegates / events / some mechanism of "fire and swelling". In fact, I introduced the controller in MVP specifically for intercepting Event viewing and restarting for the presenter (rarely) to communicate with the presenter or to communicate with the system or a special event bus, allowing me to change the mechanisms for preventing user actions without touching the view. However, be careful with the event bus; pretty soon, you start throwing all the events there, the application becomes chatty / bogged down in event processing, and events are not the fastest things in .Net. Sunchronization is an additional problem, esp, if ur application should have more “interactive” interaction with your user.

Keep in mind that although Presenter is a view of the view / process, view (and view models) can be reused; having a representation in a containment / delegation relationship with a presenter is a strong pair. View / restrict its reuse. This can be reduced with some DI, but in most cases I find that DI containers are not unnecessarily complex (since I need to know how to create objects anyway and how often you change an object for another semantically similar after creating / testing it ?). A specific dependency goes nowhere except at a different level / adds more ambiguity / makes debugging / tracing more difficult. Recently, "simplicity" has been used, and in most cases I prefer to do my Factory work / object creation / ORM mappings for most applications, since there is a 1-to-1 btw db table / entity and n is needed for added complexity of a third-party ORM universal tool, which due to the universal context / service needs of various applications should make everything more difficult than they need, even if you understand how they work (not so).

In addition, it is still possible that View will observe the model in MVP (as in MVC), so I would not do it so quickly. I do not prefer to do this myself, but it does not “break” the template. In fact, I developed something similar to MVP about ten years ago, because I did not like the “circular loop” between the components of MVC (View know about Model); I preferred to have a cleaner separation of btw View and Model that all of these templates claimed (including MVC), as well as the desire to keep the View as stupid as possible (watching Model would mean that the View would need more intelligence to handle model changes ) What I ended up with was something like MVVM and the patter strategy, where I used the “substructures” of the model to go to the view, acting as “change notifications”. It all came down to specific goals and flexible / reusable (hard combos).

0
source share

All Articles