Question

I found this sample but its for web. Can any one check this proj. and add a simple sample using winforms(no wpf).

Source Code

Thx

Was it helpful?

Solution

What kind of problem were you facing? Anyways I have made sample (yet basic) structure of how you can achieve this in WinForm. I have done using sort of Model View Presenter pattern.

First of all we have a presenter, which would deal with unit of work almost similarly the way controller does

internal class EmployeePresenter
{
    private readonly IEmployeeFormView _employeeFormView;
    private readonly IUnitOfWork _unitOfWork;

    public EmployeePresenter(IEmployeeFormView view)
    {
        _employeeFormView = view;
        _unitOfWork = new SqlUnitOfWork();
    }

    internal void GetData()
    {
        var id = 1; //parameter
        var employee = _unitOfWork.Employees.Single(e => e.Id == id);
        _employeeFormView.PopulateData(employee.Name);
    }
}

Then we have an interface and a form implementing that interface

public interface IEmployeeFormView
{
    void PopulateData(string data);
}

public partial class EmployeeForm : Form, IEmployeeFormView
{
    private readonly EmployeePresenter _presenter;

    public EmployeeForm()
    {
        InitializeComponent();
        _presenter = new EmployeePresenter(this);
    }

    #region IEmployeeFormView Members

    public void PopulateData(string data)
    {
        txtName.Text = data; //txtName is a textbox on form
    }

    #endregion

    private void btnGet_Click(object sender, EventArgs e)
    {
        _presenter.GetData();
    }
}

Add the required reference and you are done. This might not be the best way but it's certainly a way to achieve this.

Solution is upload here.

Hope this helps. Please feel free to discuss, if required.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top