I'm looking at CAB/SCSF as the framework for a data centric application. One thing I'd like to do is to make views (or rather their presenters) responsible for saving their own data, without the user having to intervene. I'm looking for an acceptable strategy
for accomplishing this. An 'unload' event would be ideal but UserControl doesn't support that. I can't figure out if/how cab notifies the current view that its about to be replaced with another view (or that the application is exiting).
I'd appreciate any thoughts.
You can expose a service that chains the ApplicationClosing event. This is what we do with our application.
Example: Our service exposes this event:
public virtual event ApplicationClosingEventHandler ApplicationClosing;
In the AfterShellCreated method we wire up to the Shell's FormClosingEventHandler:
Shell.FormClosing += new FormClosingEventHandler(Shell_FormClosing);
The Handler code then calls a method on the service which fires the service's event. Something like this:
IAutoSaveService service = RootWorkItem.Services.Get<IAutoSaveService>();
And in the service you would just fire the event:
public void ApplicationIsClosing()
if(ApplicationClosing != null)
ApplicationClosing(this, new FormClosingEventArgs());
Your presenters then need a reference to this service (via dependency injection) and they can hook up to the event upon construction.
private IAutoSaveService _saveService;
private IEmployeeRepository _employeeRepository;
[ServiceDependency] IAutoSaveService saveService,
_saveService = saveService;
_employeeRepository = employeeRepository;
_saveService.ApplicationClosing += new ApplicationClosingEventHandler(SomeEventHandler);
Inside that Presenter's event handler you can save to whatever repository/service you need to save to.