0

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:

  1. 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.

  1. Upon clicking the DeleteAll button, the view model's DeleteAllCommand 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?

Laurent Michel
  • 1,069
  • 3
  • 14
  • 29
  • Can you share some code from the unit tests for this view model? I'd be interested in a test for the `DeleteAllCommand` in particular. – Haukinger Dec 05 '19 at 17:35
  • That's the thing: they have no test for that view model. – Laurent Michel Dec 05 '19 at 18:38
  • Write that test. When you're done swearing, you turn away from passing view stuff into the view model. Un-interfaced, I should say, as interfaces are fine, see this example https://stackoverflow.com/questions/40257513/mvvm-is-code-behind-evil-or-just-pragmatic/40260229#40260229 – Haukinger Dec 05 '19 at 18:45

0 Answers0