Sharing state and updating views

Topics: CAB & Smart Client Software Factory
Apr 4, 2006 at 4:10 AM
originally posted by: atome

Hello,

I've some questions. I think my problem is common, but I'm not sure I've found the proper way to solve it. I'll try to explain what I do, and what I want to do.

(I'm using SC BAT as base for my architecture.)

1. I've one module root workitem (WI 1), which manages contacts. This work item launches two other workitems at startup :

- contacts browser workitem (WI 1.1)
- contacts list workitem (WI 1.2)

Each of these sub workitems has one view/presenter couple. The root workitem (WI 1) fills its state, which is a list of contacts, by calling a service. Then, it injects this state in each child workitem state (WI 1.1 & WI 1.2).

2. By using dependency injection, WI 1.1 & W1.2 views fill binding lists from WI1.1 & W1.2 states. Data is well displayed.

3. When someone requests a contact edition from a view, the view call a WI 1 method (through its controller), which launches another contact edition sub workitem (WI 1.3). This workitem displays a view for editing contact. When the user saves contacts changes, the edition view controller calls a CAB service which save changes, and fires and event "ContactSaved".

Well. Now, I want to refresh my views.

What I have to do ?

I think I should subscribe to this "ContactSaved" event in the top level work item (WI 1), update its state (a contacts list) by replace only the edited contact in the list, and then, do something to inform the concerned sub workitems (WI 1.1 & WI 1.2). So the children work items could update theirs view properly.

Is there a smart way to do that ? Using event broker ? Using RaiseStateChanged and StateChanged features ?

Ok, I hope you understood me, my english is not really good. Thank you in advance.

Romain.
Apr 6, 2006 at 11:46 AM
originally posted by: ChrisHolmes

---------------------------------------------------------------------------------
I think I should subscribe to this "ContactSaved" event in the top level work item (WI 1), update its state (a contacts list) by replace only the edited contact in the list, and then, do something to inform the concerned sub workitems (WI 1.1 & WI 1.2). So the children work items could update theirs view properly.

Is there a smart way to do that ? Using event broker ?
---------------------------------------------------------------------------------

Yep, that's basically how I do it.

When the State of the TopLevelWorkItem is changed, fire off a UpdateView event (something you publish using EventBroker). Then the Presenters of your Views can subscribe to that event and update their Views.

The caveat here is how they get their information from the State. If you are subclassing the Controller class they'll have access to their ParentWorkItem's State bag. But how that bag got injected into the WorkItem in the first place will determine if you have to do additional updating logic or not. In your case, you are doing: TopWorkItem --> ParentWorkItem --> Controller/Presenter --> View. If the ParentWorkItem of the Controller/View doesn't get an updated State bag when then TopWorkItem's State changes, then you're going to have problems (and have to figure out how to get that updated).
Apr 7, 2006 at 10:17 AM
originally posted by: atome

Thank you for answering.

I think you well understood my problem. For information, I've done the following:

1. WI 1 fills the state:

wi1.State"ContactList" = MyService.GetAllcontacts();

2. WI1 injects data in children states:

wi11.State"ContactList" = this.State"ContactList";
wi12.State"ContactList" = this.State"ContactList";

3. WI1.1 & WI1.2 views initialize theirs databinding sources by dependency injection:

private BindingList<Contact> contacts;

State("ContactList")
publicBindingList<Contact> Contacts
{
set(contacs = new BindingList<Contact>(value);)
}

So :

When a contact is modified, added or deleted, WI1 needs to subscribe to the corresponding events, to update its state (by modifying, deleting or adding a contact in the list). Children workitems states will be ok, because they just reference their parent state. Then, I have to subscribe to the events in each controller, in order to inform the views. And finally, each view could update its binding list, and reset bindings.

I think the conclusion is that in any case, I have to:

- Update the reference data (top level work item state), which could be used later to initialize some others views.

- Update all concerned views data sources (Binding lists, etc.) which were initialized from the reference state (see 3.), but which are not up to date after a contact was modified/added/deleted from an other place.

Does that seem correct?

Thank you.

Romain.
Apr 7, 2006 at 1:17 PM
originally posted by: ChrisHolmes

------------------------------------
- Update the reference data (top level work item state), which could be used later to initialize some others views.

- Update all concerned views data sources (Binding lists, etc.) which were initialized from the reference state (see 3.), but which are not up to date after a contact was modified/added/deleted from an other place.

Does that seem correct?
------------------------------------

Yes.