MVVM, which part of the template is responsible for grouping the datagrid

I was looking for the MVVM pattern, and now I'm trying to implement a small application based on it.

This application has a data set in which, surprisingly, some data is presented. Now I'm trying to add some grouping ability to it. I know how to write this in code (C # and XAML), but I wonder what layer I should put the responsible code on.

One part of me tells me that this should be in the view, because this is code specifically for this particular view. It is not general and serves one object: to group data.

On the other hand, I think I should process it in the ViewModel with the command. However, it feels like I'm polluting my ViewModel with specific view logic.

Any league you can shed on this?

+4
source share
3 answers

In most of my MVVM applications, I try to split responsibilities as follows:

  • The view should simply perform a simple translation of the data in pixel view mode. This usually results mainly in XAML and very little code.
  • The view mode should perform viewing-specific logic, such as grouping, etc. I often even have several view modes for each view. You can have a basic presentation model that provides a list of subvariant models for each group for your view, for example, to implement grouping.
  • If you have any logic that applies to more than one view model, it is likely the domain logic and should go into the domain model.

So, I think the grouping should go in the viewmodel.

+7
source

There is no answer to this. It depends on your scenario:

1) Does the user have any influence on this issue? If they do not, and this is a fixed grouping, I would publish the property with IGrouping and use dataservice or LINQ to do this before it enters the view.

It is generally less effective if you are grouping in a view, but this is not a clear choice. If a user can choose from many different groupings, this could be a fine worth paying for added usability.

0
source

if the user has some influence on the grouping that I bound to the ICollectionView exposed by the ViewModel. The view supports grouping, filtering, sorting, and currency, and the ICollectionView interface is from System.ComponentModel, so you do not need to add a "gui" link to your ViewModel project. WPF DataGrid also supports the ICollectionView interface.

If the user does not affect the grouping (the groups are fixed), I would simply β€œpre” group the data in the model. NTN.

0
source

Source: https://habr.com/ru/post/1312532/


All Articles