tags:

views:

60

answers:

1

Just trying to grasp the MVC by doing ... and still not sure got it 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 
            {
                get { return _Prop1Gui; }
                set { _Prop1Gui = value; } 
            }

            private string _Prop2Gui;
            public string Prop2Gui
            {
                get { 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 the new props separated by 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 the data from 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 
+1  A: 

In short, the pattern of MVC should follow:

  • view receives user input and passes it to the controller
  • controller updates the model in response to user input (can also modify view directly)
  • model notifies the view of the change
  • view updates itself based on changes to model.

In otherwords your View should be observing your Model. At the moment your View does not know when the Model changes hence it cannot update itself accordingly.

You should look at introducing something like the Observer Pattern.

James