简体   繁体   中英

How connect adapter from view to model in MVP

Am not shure how to do this. Am litle confused, in my project c# winforms am using MVP pattern for deskotp application.

I have datagridview where i show products gorup . Database query for selecting products group is stored in model and that method for selecting return DataTable . Next, presenter call that model function for selecting and pass it to view. In view i put that DataTable in BindingSource and in gridview as DataSource i pass binding source.

This work good but what when i try to add new row in grid and try to save that new row?

What if i want to edit existing row and update that record to database.

If i want to create/update record from database i need to access to adapter and call update . Problem is becouse my adapter is in model not in view .

Am not shure how to from view access to adapter and dataset to model.

Check my code for selecting:

Model:

public DataTable SelectAll()

{
            using (MySqlConnection conn = new MySqlConnection(Properties.Settings.Default.ConnectionString))
            {
                try
                {
                    conn.Open();
                    string query = "SELECT Naziv, Opis FROM grupe";

                    using (MySqlDataAdapter adapter = new MySqlDataAdapter(query, conn))
                    {
                        dt = new DataTable();
                        ds = new DataSet();

                        adapter.Fill(ds, "grupe");

                        dt = ds.Tables["grupe"];
                    }

                    conn.Close();
                }
                catch(MySqlException ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }

           return dt;
        }

Presenter:

public DataTable SelectAll()
{
    return _model.SelectAll();
}

View:

private void GrupeArtikala_Load(object sender, EventArgs e)
{
    bs = new BindingSource();
    bs.DataSource = _presenter.SelectAll();
    groupDataGridView.DataSource = bs;     
}

So now when i want to save changes to database i need to access to model database adapter but i dont know how.

private void saveToolStripButton_Click(object sender, EventArgs e)
{
    changes = ds.GetChanges(); // DatSet changes is in model but i must access here but dont know how

    if (changes != null)
    {
        int updatedRows = dataAdapter.Update(changes); // Adapter is in model and i need him here 
        ds.AcceptChanges();
    }
}

Am not shure how to connect this to model to avoid mixing database queries in view forms.

Anyone can give me example to do this.

If I understand correctly, you need to call functions on objects belonging to your model, but you don't want your view to have a reference to your model. Is that right? If so, you could follow the pattern you used with the SelectAll function:

bs.DataSource = _presenter.SelectAll();

You are calling _presenter.SelectAll in the view, but ultimately getting the returned value from the model. So to call ds.GetChanges in your view, you could add a GetDataSetChanges function to your presenter:

public DataSet GetDataSetChanges()
{
  return _model.GetDataSetChanges();
}

Same thing for calling update on your adapter. Add a function to the presenter:

public int UpdateAdapter(DataSet dataSet)
{
  return _model.UpdateAdapter(dataSet);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM