MVP Passive View - Save view data and model data separately

I implemented the MVP triad using a passive presentation template, i.e. the view contains only simple getters and setters. However, I am having problems separating view data from model data. In particular, when processing a change in state of a view.

The triad is used so that the user can select a part from the list. A list of parts is provided by the model, each of which is uniquely identified by a unique identifier.

Suppose the parts look like this:

class Part
{
    int ID; // this code uniquely identifies the part within the model
    String partCode;
    String description;
    double voltage;
}

The view displays a list for the user and allows them to select a part

The list is displayed in the DataGridView, and part is selected by clicking on the row in the dataGridView.

, , DataTable, . DataTable , DataSource DataGridView.

class Presenter
{
    IView _view;
    IModel _model;

    //...///

    _view.Data = _model.GetFilteredData();
}

class Model
{
    public DataTable GetFilteredData()
    {
        // create a DataTable with the partCode and Description columns only
        // return DataTable
    } 
}

class View  //winform
{
      public DataTable Data
      {
          set 
          {
              this.dataGridView.Source = value;
          }
      }
}

. View DataGridView.

, , - , .

, , - .

, ( ) ( ) .

:

1) DataTable, , , . . , , ( ).

2) , ​​ . , , , , , ( ) . ( ).

    public int RowIndexSelected { get; private set; }

    //...//

    private void gridParts_CellEnter(object sender, DataGridViewCellEventArgs e)
    {
        if (SelectedPartChangedEvent != null)
        {
            RowIndexSelected = e.RowIndex;

            SelectedPartChangedEvent();            
        }
    }

3) (2). , . ID . dataGridAdapters.

    public PartSelectDataGridAdapter(IPartSelectView view, PartCollection data)
    {
        _view = view;
        _data = data;

        _view.SelectedPanelChangedEvent += HandleSelectedPartChanged;
    }

    void HandleSelectedPartChanged()
    {
        int id = _data[_view.RowIndexSelected].ID;

        if (SelectedPartChanged != null)
        {
            SelectedPartChanged(id);
        }
    }

3, , .

- ?

+3
3

, , DataTable, partCode .

: datatable datagrid.

+1

;

, List<Part> ?

, id . . , SelectionChanged(Part selected) .

, , , .

, , DataTable. ( , .)

, , . : , . , , .

/ DataTable .

: , :

, :

public class PartViewModel
{
  object PartModel { get; set; }
  string Name { get; set; }
  string Description { get; set; }
}

a List<PartViewModel> DataGridView. PartViewModel ( ). , PartModel Part. , , . , "" .

:

interface IPartListPresenter
{
  // other methods
  void SelectedPartChanged(PartViewModel nowSelected);
}

, partBindingSource , gridview, CurrentChanged partBindingSource :

private void partBindingSource_CurrentChanged(object sender, EventArgs e)
{
  _presenter.SelectedPartChanged(partBindingSource.Current as PartViewModel);
}

:

public void SelectedPartChanged(PartViewModel nowSelected)
{
  if(nowSelected == null)
  {
    return;
  }
  part myPart = (Part) nowSelected.Part;
  // dos stuff
}

, .

+2

, !

, , . , , !

, , , !

MVP.

0

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


All Articles