This project is read-only.

When to persist application data

Mar 9, 2008 at 4:33 AM
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.

Thanks!

-k
Mar 10, 2008 at 3:39 PM
Edited Mar 10, 2008 at 3:40 PM
I do not know if this is the best approach, but what about persisting the data within the Dispose(bool disposing) method of your view?
Mar 10, 2008 at 3:39 PM
I do not know if this is the best approach, but what about persisting the data in within the Dispose(bool disposing) method of your view?
Mar 10, 2008 at 4:21 PM

sp00ky wrote:
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>();
service.ApplicationIsClosing();

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;
 
public SomePresenter(
[ServiceDependency] IAutoSaveService saveService,
[ServiceDependency] IEmployeeRepository)
{
   _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.