There are probably other ways and better ways to do this but this works well for me and it seems not to violate MVC too bad :)
//CustomersListForm.cs
public class CustomersListForm
{
private CustomerController myController;
public CustomersListForm()
{
InitializeComponents();
myController = new CUstomer Controller();
}
private void CustomersListForm_Load(object sender, EventArgs e)
{
PopulateCustomerGrid();
}
public void PopulateCustomerGrid()
{
customersGrid.DataSource = myController.GetCustomers();
}
private void CustomersGrid_SelChange(object sender, EventArgs e)
{
if(CustomersGrid.RowSel != -1)
{
Int32 customerId = Int32.Parse(CustomersGrid[CustomersGrid.RowSel,1].ToString();
myController.GetCustomerById(customerId);
}
}
public void EditCustomer()
{
CustomerForm form = new CustomerForm(myController);
form.ShowDialog();
PopulateCustomerGrid();
}
public void AddCustomer()
{
myController.CreateCustomer();
CustomerForm form = new CustomerForm(myController);
form.ShowDialog();
PopulateCustomerGrid();
}
}
// CustomerController.cs
public class CustomerController
{
private Customer currentCustomer;
private PersistenceManager myPM = PersistenceManager.DefaultManager;
public Customer CurrentCustomer
{
get{
return currentCustomer;
}
set{
currentCustomer = value;
}
}
public void CreateCustomer()
{
currentMember = Customer.Create(myPM);
}
public void GetCustomerById(Int32 customerId)
{
RdbQuery query = new RdbQuery(typeof(Customer));
query.AddClause(Customer.CustomerIdEntityColumn, EntityQueryOp.EQ, customerId);
currentCustomer = myPM.GetEntity<Customer>(query);
}
public void SaveCustomer()
{
try{
myPM.SaveChanges();
}
catch(SaveException ex)
{
}
}
}
// CustomerForm.cs
public class CustomerForm
{
private CustomerController myController;
public CustomerForm(CustomerController controller)
{
InitializeComponents();
myController = controller;
}
private void CustomerForm_Load(object sender, EventArgs e)
{
CustomerBS.DataSource = myController.CurrentCustomer;
}
private void SaveButton_Click(object sender, EventArgs e)
{
myController.SaveCustomer();
this.Close();
}
}
Edited by BillG - 13-Apr-2008 at 11:19am