如何在Windows窗体应用程序中使用C#.NET正确实现MVC

前端之家收集整理的这篇文章主要介绍了如何在Windows窗体应用程序中使用C#.NET正确实现MVC前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我一直在网上寻找.NET中MVC设置的示例.我发现了许多例子,但它们在某些方面似乎都有所不同.我有一本关于设计模式的书,描述了MVC起源于Smalltalk,因此我读了几个人讨论它在该语言中的实现.以下是我用自己收集的内容编写的一个示例项目是一个正确的实现,但我对一些细节感到困惑.

我遇到的一个问题是对象构造的正确顺序.这是我的Program.cs中的impl

Model mdl = new Model();
Controller ctrl = new Controller(mdl);
Application.Run(new Form1(ctrl,mdl));

风景:
我马上就遇到了一些我不确定的问题.首先,如果视图应该只从模型中读取数据以进行更新,但包含对它的引用,那么是什么阻止我从控制器对视图中的模型进行调用?程序员是否应该忽略它们暴露给模型成员函数的事实?我的另一个想法,也许是通知视图模型更新的事件,将为视图发送某种状态对象以更新自身.

public interface IView
{
    double TopSpeed { get; }
    double ZeroTo60 { get; }

    int VehicleID { get; }
    string VehicleName { get; }
}

/// <summary>
/// Assume the form has the following controls
/// A button with a click event OnSaveClicked
/// A comboBox with a selected index changed event OnSelectedIndexChanged
/// A textBox that displays the vehicles top speed named mTextTopSpeed
/// A textBox that displays the vehicles zero to 60 time named mTextZeroTo60
/// </summary>

public partial class Form1 : Form,IView
{
    private IController mController;
    private IModel mModel;

    public Form1(IController controller,IModel model)
    {
        InitializeComponent();

        mController = controller;
        mController.SetListener(this);
        mModel = model;
        mModel.ModelChanged += new ModelUpdated(mModel_ModelChanged);
    }

    void mModel_ModelChanged(object sender,EventArgs e)
    {
        mTextTopSpeed.Text = mModel.TopSpeed.ToString();
        mTextZeroTo60.Text = mModel.ZeroTo60.ToString();
    }

    public double TopSpeed { get { return Double.Parse(mTextTopSpeed.Text); } }

    public double ZeroTo60 { get { return Double.Parse(mTextZeroTo60.Text); } }

    public int VehicleID { get { return (int)mComboVehicles.SelectedValue; } }

    public string VehicleName { get { return mComboVehicles.SelectedText; } }

    #region Form Events

    private void OnFormLoad(object sender,EventArgs e)
    {
        mComboVehicles.ValueMember = "Key";
        mComboVehicles.DisplayMember = "Value";
        mComboVehicles.DataSource = new BindingSource(mModel.VehicleList,null);
    }

    private void OnSelectedIndexChanged(object sender,EventArgs e)
    {
        mController.OnSelectedVehicleChanged();
    }

    private void OnSaveClicked(object sender,EventArgs e)
    {
        mController.OnUpdateVehicle();
    }

    #endregion
}

控制者:
我实现控制器的方式唯一真正的问题是,对我来说似乎有点奇怪,可以构建控制器而不必明确分配视图.我可以完全忽略视图但这意味着我会将参数传递给控制器​​的函数以更新模型,这似乎完全忽略了这一点.

public interface IController
{
    void OnUpdateVehicle();
    void OnSelectedVehicleChanged();
    void SetListener(IView view);
}

class Controller : IController
{
    private IModel mModel;
    private IView mView = null;

    public Controller(IModel model)
    {
        mModel = model;
    }

    public void OnUpdateVehicle()
    {
        if(mView == null)
            return;

        mModel.UpdateVehicle(mView.VehicleID,mView.TopSpeed,mView.ZeroTo60);
    }

    public void SetListener(IView view)
    {
        mView = view;
    }

    public void OnSelectedVehicleChanged()
    {
        if (mView == null)
            return;
        mModel.SelectVehicle(mView.VehicleID);
    }
}

该模型:
在我的表单中,我有一个组合框,它是我的伪数据库中给出的车辆列表.我觉得我的表单实际上应该实现多个视图/模型.特定于列出具有相应控制器/模型的可能车辆的视图,以及用于使用其自己的控制器/模型显示关于所选车辆的信息的视图.

public delegate void ModelUpdated(object sender,EventArgs e);

public interface IModel
{
    event ModelUpdated ModelChanged;

    void UpdateVehicle(int id,double topSpeed,double zeroTo60);
    void SelectVehicle(int id);

    double TopSpeed { get; }
    double ZeroTo60 { get; }
    IDictionary<int,string> VehicleList { get; }
}

// class for the sake of a pseudo database object
class Vehicle
{
    public int ID { get; set; }
    public string Name { get; set; }
    public double TopSpeed { get; set; }
    public double ZeroTo60 { get; set; }

    public Vehicle(int id,string name,double zeroTo60)
    {
        ID = id;
        Name = name;
        TopSpeed = topSpeed;
        ZeroTo60 = zeroTo60;
    }
}


class Model : IModel
{
    private List<Vehicle> mVehicles = new List<Vehicle>()
    {
        new Vehicle(1,"Civic",120.0,5.0),new Vehicle(2,"Batmobile",9000.0,1.0),new Vehicle(3,"Tricycle",5.0,0.0)
    };

    private Vehicle mCurrentVehicle;

    public Model()
    {
        mCurrentVehicle = mVehicles[0];
    }

    public event ModelUpdated ModelChanged;

    public void OnModelChanged()
    {
        if (ModelChanged != null)
        {
            ModelChanged(this,new EventArgs());
        }
    }

    public double TopSpeed { get { return mCurrentVehicle.TopSpeed; } }

    public double ZeroTo60 { get { return mCurrentVehicle.ZeroTo60; } }

    public IDictionary<int,string> VehicleList
    {
        get 
        {
            Dictionary<int,string> vDict = new Dictionary<int,string>();
            foreach (Vehicle v in mVehicles)
            {
                vDict.Add(v.ID,v.Name);
            }

            return vDict as IDictionary<int,string>;
        }
    }

    #region Pseudo Database Calls

    public void SelectVehicle(int id)
    {
        foreach (Vehicle v in mVehicles)
        {
            if (v.ID == id)
            {
                mCurrentVehicle = v;
                OnModelChanged(); // send notification to registered views
                break;
            }
        }
    }

    public void UpdateVehicle(int id,double zeroTo60)
    {
        foreach (Vehicle v in mVehicles)
        {
            if (v.ID == id)
            {
                mCurrentVehicle.TopSpeed = topSpeed;
                mCurrentVehicle.ZeroTo60 = zeroTo60;
                OnModelChanged(); // send notification to registered views
                break;
            }
        }
    }

    #endregion
}

在这个tl; dr的结论中,我想我正在寻找的是关于我在这里做什么代表一个真正的MVC实现的一些指导,也许是某些人对上述问题有所了解.任何建议将不胜感激.

我们这取决于你想做什么.您目前有一个监督控制器的实现.如果您希望从视图中删除模型(以及任何数据绑定),则可以实现Passive View模式.有关更多差异,请参阅 this文章.

Passive View and Supervising Controller http://i.msdn.microsoft.com/dynimg/IC281772.png

而马丁福勒是国王(GUI Architectures).

猜你在找的Windows相关文章