如何在使用旧的WinForms项目期间最大程度地减少工作时间成本?

时间:2019-03-01 08:11:02

标签: c# winforms model-view-controller

我的主要目标是在使用旧WinForms项目期间最大程度地减少工作时间成本。我以实现MCV + WinForms为例。有效。我有一个清晰的模型,但是Controller.cs中有两个方法(OnSuper()和WorkMethod())-我不喜欢它们。它们不具有功能性,但是当有人将黄油涂在没有面包的黄油上时,它们具有外观。没有MPV界面和WPF界面时,该如何升级?还是不是一个好方法,我必须创建一个新的WPF项目?

Class Model.cs

class Model
{    
    public int X { get; set; }
    public int Y { get; set; }
    public int R { get; set; }
    public Model() { }
    public Model(int x, int y)
    {
        X = x;
        Y = y;
    }
    public int GetRes()
    {
        return X + Y;            
    }
}

Form1.cs

public partial class Form1 : Form
{        
    public Form1()
    {
        InitializeComponent();            
        button1.Click += delegate
        {
            Controller controller = new Controller
            (
                this, 
                Int32.Parse(this.textBox1.Text),
                Int32.Parse(this.textBox2.Text)
            );
            controller.SuperEvent += Controller_SuperEvent;
            controller.OnSuper();
        };
    }
    private void Controller_SuperEvent(object sender, EventArgs e)
    {
        Controller controller = sender as Controller;
        textBox3.Text = controller.WorkMethod();
        controller.WorkMethod();
    }
}

和类Controller.cs

class Controller
{        
    public event EventHandler SuperEvent;
    Model model;
    Form1 form1;
    public int Result { get; set; }
    public int CX { get; set; }
    public int CY { get; set; }
    public Controller(Form1 form1, int cX, int cY)
    {
        this.form1 = form1;
        this.model = new Model();            
        CX = cX;
        CY = cY;
    }
    public void OnSuper()
    {
        SuperEvent?.Invoke(this, EventArgs.Empty);
    }
    public string WorkMethod()
    {            
        model.X = CX;
        model.Y = CY;
        return model.GetRes().ToString();
    }
}

1 个答案:

答案 0 :(得分:0)

为简化可维护性,在层级职责之间划清界限。

负责业务逻辑的模型

class Model
{    
    public int X { get; set; }
    public int Y { get; set; }

    public Model() {}

    public int GetRes() => X + Y
}

ViewModel负责准备要在视图中显示的数据(如果需要),并调用从视图中请求的模型的业务逻辑操作。
INotifyPropertyChanged将通知视图有关可能的更改,而视图模型不依赖于具体的视图类型。

class ViewModel : INotifyPropertyChanged
{    
    private readonly Model _model;

    public int X { get; set; }

    public int Y { get; set; }

    private string _result = "";
    public string Result 
    { 
        get
        {
            return _result;
        } 

        private set
        {
            if (_result.Equals(value) == false)
            {
                _result = value;
                RaisePropertyChanged();
            }

        }
    }

    public ViewModel(Model model) => _model = model;

    public void CalculateResult()
    {
         Result = _model.GetRes().ToString();
    }

    // Implementation of INotifyPropertyChanged
    public event PropertyChangedEventHandler PropertyChanged;

    private void RaisePropertyChanged([CallerMemberName] string propertyName = "")
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}

视图,其职责是显示来自视图模型的值或在用户请求时调用视图模型操作。

public partial class View : Form
{    
    private ViewModel _viewmodel;

    public View()
    {
        InitializeComponent();   
        var model = new Model { X = 10, Y = 20 }  
        _viewmodel = new ViewModel(model);

        // Bind controls to the viewmodel
        textboxForX.DataBindings.Add("Text", _viewmodel, "X", true);
        textboxForY.DataBindings.Add("Text", _viewmodel, "Y", true);
        labelForResult.DataBindings.Add("Text", _viewmodel, "Result", true);            
    } 

    private void CalculateButton_Click(object sender, EvetnArgs e)
    {
        _viewmodel.Calculate();
    }
}    

请注意,在简单情况下(如上所述),您可以跳过viewmodel的“图层”并将视图直接绑定到模型。但是,在项目开始成长时,在视图和业务逻辑之间留有“缓冲”将使您的生活更加轻松。