Search code examples
c#.netwinformspropertiesmvp

Using two get and set properties for the same object


I have a DataGridView in UI and my presentation class needs to set and get data to and from the gird. So that I could use a public property in this case as follows.

public DataGridView DeductionDetailsInGrid
{
    get { return dgEmployeeDeductions; }
}


public List <Deduction > DeductionDetails
{
    set { dgEmployeeDeductions.DataSource = value; }
}

I'm using two properties here as the set property should be able to show the list of objects passed in by the presenter, on the grid and also the presenter should be able to somehow get the data from the grid to supply them for the upper layers.

Is using two get and set properties for the same DataGridView an acceptable solution?

I think that I should change the get property's data type (DataGridView) as exposing the DataGridView breaks encapsulation! How to do that?

EXTRA:

If using ListBoxes we could do something like this...

    public List<string>  GivenPermission
    {
        get { return lstGivenPermissions.Items.Cast<string>().ToList(); }
        set { lstGivenPermissions.DataSource = value; }
    }

Solution

  • public List <Deduction > DeductionDetails
    {
        get { return (List<Deduction>)dgEmployeeDeductions.DataSource; }
        set { dgEmployeeDeductions.DataSource = value; }
    }