Could this be C# Model View Controler console app in and if not why? -
just trying grasp mvc doing ... , still not sure got right ..
using system; using system.text; namespace mvpconsoleapp { class program { static void main(string[] args) { view objview = new view(); controller objcontroller = new controller(objview); objcontroller.buildui(); objview.waitandread(); } } //eof program class view { private string _prop1gui ; public string prop1gui { { return _prop1gui; } set { _prop1gui = value; } } private string _prop2gui; public string prop2gui { { return _prop2gui; } set { _prop2gui = value; } } public void waitandread() { string bothprops = console.readline(); string [] bothstrings = bothprops.split(new char [] { ' ' } ); this.prop1gui= bothstrings[0]; this.prop2gui = bothstrings[1] ; controller objcontroller = new controller(this); objcontroller.storedata(); objcontroller.builduiaftgerinput(); this.waitandread(); } public void showmsg ( model objmodel) { console.writeline("objmodel.modelprop1 " + objmodel.modelprop1 + " objmodel.modelprop2 " + objmodel.modelprop2); console.writeline("write new props separated space !"); } } //eof class class controller { view view { get; set; } model model { get; set; } public controller(view objview) { this.view = objview; this.loaddata(); } public void buildui() { this.view.showmsg(this.model); } public void loaddata() { //get data db model objmodel = new model(); objmodel.modelprop1 = "modelprop1"; objmodel.modelprop2 = "modelprop2"; this.model = objmodel; } //eof loaddata() public void storedata() { model objmodel = new model(); objmodel.modelprop1 = this.view.prop1gui; objmodel.modelprop2 = this.view.prop2gui; this.model = objmodel; } public void builduiaftgerinput() { this.view.showmsg( this.model); } } //eof class class model { public string modelprop1 { get; set; } public string modelprop2 { get; set; } } //eof class } //eof namespace
in short, pattern of mvc should follow:
- view receives user input , passes controller
- controller updates model in response user input (can modify view directly)
- model notifies view of change
- view updates based on changes model.
in otherwords view should observing model. @ moment view not know when model changes hence cannot update accordingly.
you should @ introducing observer pattern.
Comments
Post a Comment