I am implementing a little extension for an existing application. Now I am creating a ‘wpf-library’ with mvvm and everything seems quite nice at the moment.
Now let’s say I have an event to delete some datasets from the underlying Database. I don’t want to do this in my extension-app but in the calling-application.
So what I have achieved (and whats working) is that the user clicks on my ‘remove’ button, the view-model implemented the command and here I am able to fire an event. What I wanted is to send the event out to the calling-application.
My startup-class that the calling-app is able to see now like this:
public UserControl ViewToShowInContainer { get; private set; }
public StartMyExtensionApplication(Model.TransportClass dataToWorkWith)
{
ViewToShowInContainer = new View.MainView();
(ViewToShowInContainer.DataContext as VehicleSearchWPF.ViewModel.MyMainViewModel).RemoveSelectStatementFromDB += new EventHandler<SelectStatementRemovedEventArgs>(StartVehicleSearch_RemoveSelectStatementFromDB);
LocalDataToWorkWith.MapTransportClass(dataToWorkWith);
}
void StartVehicleSearch_RemoveSelectStatementFromDB(object sender, SelectStatementRemovedEventArgs e)
{
throw new NotImplementedException();
}
But in my opinien there must be some nicer / cleaner / better method to implement this?
Thanks in advance! 🙂
This indeed doesn’t seem like the right place to use normal events; a couple of other options:
-do not use events but use ‘services’, imo more clear and more direct while still decoupling. Has the benefit that you can easily test your viewmodel (eg test that executing the Remove command effectively calls Remove on the database) by mocking the database service.
-use something like Prism’s IEventAggregator