I have very little experience with Prism and .NET (< 1 year). Yet, I am working on a project where those technologies are used extensively. Also, I am an experienced developer, but in other frameworks, other programming languages, etc. Therefore, I have sometimes stupid questions to ask like the following one.
So, we are currently trying to build up a client application that shows a grid of data along with buttons allowing for the usual CRUD operations. The team's ansatz is the following:
- We create a xaml view and define the
GridControl
like this:
<dxg:GridControl x:Name="MyGrid"
ItemsSource="{Binding Items}"
SelectedItems="{Binding SelectedItems}">
<dxmvvm:Interaction.Behaviors>
<dxmvvm:EventToCommand EventName="Loaded"
Command="{Binding GridLoadedCommand}"
CommandParameter="{Binding ElementName=MyGrid}" />
</dxmvvm:Interaction.Behaviors>
[...]
This triggers command GridLoadedCommand
upon view loading. GridLoadedCommand
stores the GridControl
object in the view's data context, which is the view model.
- Upon clicking the
DeleteAll
button, the view model'sDeleteAllCommand
calls a method of the following kind:
public static void DeleteAllRows(GridControl gridControl)
{
gridControl?.SelectAll();
DeleteSelectedRowsEx(gridControl);
}
In essence, that is perfectly valid code and is also working like a charm. However, in my opinion, but I might be mistaken, that kind of code goes against MVVM philosophy because in the above snippet we are clearly mixing up "view" with "view model" code. Indeed, the view model manipulates a GridControl
object directly, which, as far as I know, should be avoided as much as possible, as it breaks "separation of concerns" and makes the software design entangled. Also, it might complicate unit testing a bit. Am I seeing that right or am I alien here? I would rather manipulate the ViewModel.Items
and ViewModel.SelectedItems
observable collections directly and trigger the relevant events to update the view instead.
Now, the actual reason why the team came up with that code is because they wanted a generic base view model class with generic CRUD operations, irrespective of what the underlying ItemsSource
abd SelectedItems
(see GridControl
xaml code above) are. Is there a standard way to make that happen in the Prism constellation? I would just add a generic parameter to the base class with the ItemsSource
's data type and pass the specific ItemsSource
and SelectedItems
to the generic class. Is there a better way? Is the way I've described here above with direct GridControl
manipulation from the view model really the way to go?