【问题标题】:Could this be C# Model View Controler console app in and if not why?这可能是 C# 模型视图控制器控制台应用程序,如果不是,为什么?
【发布时间】:2010-01-13 11:14:50
【问题描述】:

只是试图通过做...来掌握 MVC,但仍然不确定它是否正确..

    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 

【问题讨论】:

    标签: c# console mvp model-view-controller


    【解决方案1】:

    简而言之,MVC的模式应该遵循:

    • 视图接收用户输入并将其传递给控制器​​
    • 控制器根据用户输入更新模型(也可以直接修改视图)
    • 模型通知视图变化
    • 视图会根据模型的更改自行更新。

    换句话说,您的视图应该观察您的模型。目前,您的视图不知道模型何时更改,因此它无法相应地更新自身。

    您应该考虑引入类似Observer Pattern 的东西。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-03
      • 2011-06-09
      • 2017-02-12
      • 1970-01-01
      • 1970-01-01
      • 2012-11-26
      相关资源
      最近更新 更多