Pattern for sharing data between views (MVP or MVVM)
What is a good pattern for sharing data between related views?.
I have an 开发者_StackOverflow中文版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.
Josh Smith and Marlon Grech use the mediator pattern with an implementation that uses weak references to prevent memory leaks. Check this out:
http://marlongrech.wordpress.com/2009/04/16/mediator-v2-for-mvvm-wpf-and-silverlight-applications/
Seems like the shared data should belong in the underlying Model, not the View.
As far as how this sharing takes place, I'd assume that something is creating the individual Views, and giving them a reference to their Model? That seems like the appropriate mechanism for the sharing to use.
精彩评论