c# - Changing the model -
imagine simple usercontrol 1 textbox (customername), , 2 buttons (save/cancel).
this usercontrol sits within parent control 2 more buttons (previouscustomer/nextcustomer)
the problem don't know pass in model presenter. go through view constructor? if model changes (previouscustomer/nextcustomer clicked)? create new view every time? seems wasteful if usercontrol complex many children. implement updatemodel method in view , presenter, or incorrect?
here's code:
public interface imodel { string customername { get; set; } } public class presenter { private iview _view; private imodel _model; public presenter(iview view, imodel model) { _view = view; _model = model; _view.customername = _model.customername; } public void save() { _model.customername = _view.customername; } public void cancel() { _view.customername = _model.customername; } public void updatemodel(imodel newmodel) { _model = newmodel; _view.customername = _model.customername; } } public interface iview { string customername { get; set; } } public class view : usercontrol, iview { private presenter _presenter; public string customername { { return customernameeditbox.text; } set { customernameeditbox.text = value; } } //is right place pass in model? public view(imodel model) { _presenter = new presenter(this, model); } private void savebutton_click(object sender, eventargs e) { _presenter.save(); } private void cancelbutton_click(object sender, eventargs e) { _presenter.cancel(); } public void updatemodel(imodel newmodel) { _presenter.updatemodel(newmodel); } } edit: stated in comments below, think best have view dumb , know nothing of presenter or model. here revised code.
model:
public interface imodel { string customername { get; set; } } public class model : imodel { public string customername { get; set; } public model(string name) { customername = name; } } view:
public interface iview { event eventhandler saveclick; event eventhandler cancelclick; string customername { get; set; } } public class view : usercontrol, iview { public event eventhandler saveclick { add { savebutton.click += value; } remove { savebutton.click -= value; } } public event eventhandler cancelclick { add { cancelbutton.click += value; } remove { cancelbutton.click -= value; } } public string customername { { return customernameeditbox.text; } set { customernameeditbox.text = value; } } } presenter:
public class presenter { private iview _view; private imodel _model; public control view { { return (control)_view; } } public presenter(iview view, imodel model) { _view = view; _view.saveclick += new eventhandler(_view_saveclick); _view.cancelclick += new eventhandler(_view_cancelclick); _model = model; _view.customername = _model.customername; } void _view_saveclick(object sender, eventargs e) { _model.customername = _view.customername; } void _view_cancelclick(object sender, eventargs e) { _view.customername = _model.customername; } public void changemodel(imodel newmodel) { _model = newmodel; _view.customername = _model.customername; } } usage:
public class mainprogram : form { public void arbitrarymethod() { //create new model object imodel model1 = new model("test1"); //create presenter model , view implementing iview presenter presenter = new presenter(new view(), model1); //add view panel control in form mainpanel.controls.add(presenter.view); //change model imodel model2 = new model("test2"); presenter.changemodel(model2); } }
if design mvp (model view presenter) should create model inside presenter. that's idea behind pattern. view communicates presenter , presenter offers him methods, properties, events needed. in example decided create model well. can that, please hide view. related example, remove imodel parameter in constructor , create new 1 in code instead.
consider setting view changes via methods (_view.setname("me"). make life easier later.
there's no need create new winforms form. perhaps, don't understand point here..
update:
take @ example below. have interfaces view, presenter , model ( based code). renamed imodel icustomer make point clear. presenter creates model doing i/o stuff , gets view interface updating windows or whatever behind interface.
public interface iview { public string customername { get; set; } } public interface icustomer { string customername { get; set; } } public interface imodel { void save(icustomer entity); void update(icustomer entity); icustomer create(); } public class customer : icustomer { public string customername {get;set;} } public class mymodel : imodel { public void save(icustomer entity) { //do here.. } public void update(icustomer entity) { //do here.. } public icustomer create() { return new customer(); } } public class presenter { private iview _view; private icustomer _entity; private imodel _model = new mymodel(); public presenter(iview view) { _view = view; _model = new mymodel(); _entity = _model.create(); _view.customername = _entity.customername; } public void save() { _model.save(_entity); } public void cancel() { _entity = _model.create(); } public void updatemodel(icustomer customer) { _model.update(customer); _view.customername = customer.customername; } }
Comments
Post a Comment