Pattern for sharing data between views (MVP or MVVM)

Posted by Dovix on Stack Overflow See other posts from Stack Overflow or by Dovix
Published on 2010-05-10T03:59:27Z Indexed on 2010/05/10 4:08 UTC
Read the original article Hit count: 454

Filed under:
|
|

What is a good pattern for sharing data between related views?.

I have an application where 1 form contains many small views, each views behaves independently from each other more or less (they communicate/interact via an event bus). Every so often I need to pass the same objects to the child views. Sometimes I need this same object to be passed to a child view and then the child passes it onto another child itself contains.

What is a good approach to sharing this data between all the views contained within the parent form (view) ?

I have looked into CAB and their approach and every "view" has a "root work item" this work item has dictionary that contains a shared "state" between the views that are contained.

Is this the best approach? just a shared dictionary all the views under a root view can access?

My current approach right now is to have a function on the view that allows one to set the object for that view. Something like

view.SetCustomer(Customer c); 

then if the view contains a child view it knows to set it on the child view ala:

this.childview1.SetCustomer(c);

The application is written in C# 3.5, for winforms using MVP with structure map as a IoC/DI provider.

© Stack Overflow or respective owner

Related posts about c#

Related posts about mvp