I am building an WPF Prism MVVM application.
This application will contain a lot of CRUD windows.
I want to optimize the features (and reduce the amount of produ
Build an interface which all your CRUD ViewModels will inherit from, and have your generic ViewModel use the interface to execute CRUD operations
Here's an example of how the interface and classes might look:
// Generic interface
public interface IGenericViewModel
{
bool Add();
bool Save();
bool Delete();
}
// Generic CRUD ViewModel
public class GenericViewModel
{
public IGenericViewModel ObjectViewModel { get; set; }
public RelayCommand AddCommand { get ; }
public RelayCommand SaveCommand { get ; }
public RelayCommand DeleteCommand { get ; }
void Add()
{
ObjectViewModel.Add();
}
void Save()
{
ObjectViewModel.Save();
}
void Delete()
{
ObjectViewModel.Delete();
}
}
// Specific object ViewModel used by generic CRUD ViewModel
public class CustomerViewModel : ViewModelBase, IGenericViewModel
{
bool IGenericViewModel.Add()
{
// Add logic
}
bool IGenericViewModel.Save()
{
// Save logic
}
bool IGenericViewModel.Delete()
{
// Delete object
}
}
Look at this generic control WPFCrudControl, may be useful for you.
A generic WPF CrudControl implemented based on the MVVM pattern. It gives a huge productivity boost for straightforward CRUD operations (Add, Edit, Delete, Validate, Listing with sorting, paging and searching). The control abstracts both the UI and business logic, so it requires relatively minimal coding effort, while keeping it possible to customize its behavior.