这可能是中的C#模型视图控制器控制台应用程序吗?如果不是,原因是什么?
只是想通过做…来掌握MVC。。。但仍然不确定是否正确这可能是中的C#模型视图控制器控制台应用程序吗?如果不是,原因是什么?,c#,console,mvp,model-view-controller,C#,Console,Mvp,Model View Controller,只是想通过做…来掌握MVC。。。但仍然不确定是否正确 using System; using System.Text; namespace MVPConsoleApp { class Program { static void Main(string[] args) { View objView = new View();
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
简言之,MVC模式应遵循:
- 视图接收用户输入并将其传递给控制器
- 控制器根据用户输入更新模型(也可以直接修改视图)
- 模型将更改通知视图
- 视图根据对模型的更改自行更新