代码之家  ›  专栏  ›  技术社区  ›  Maslow

Microsoft for Windows窗体是否有MVC或其他良好的框架?

  •  1
  • Maslow  · 技术社区  · 15 年前

    在厚客户端或智能客户端应用程序(如ASP.NET MVC for Web)中,是否有任何东西可以帮助提供良好的关注分离结构?

    5 回复  |  直到 15 年前
        1
  •  3
  •   Thomas Levesque    15 年前

    对于WinForms,我不知道任何这样的框架(尽管它可能存在)。但是,如果您愿意切换到WPF,MVVM模式正是您所需要的,并且有许多框架可以让您的生活更轻松地使用此模式。

    你可以找到一个 list of MVVM frameworks 在Jeremy Alles的博客上,甚至 comparison matrix

        2
  •  2
  •   johnny g    15 年前

    MVC只是一个模式,可以很容易地在没有任何框架的情况下实现。它主要将关注点[soc]分离为关注点GUI和业务功能。我还建议您查找模型视图演示者[MVP]和模型视图视图模型[MVVM]——MVC的细微变化。MVVM比严格的MVC更普遍。

    嗯,撇开这一点不谈,MVC有几种实现方法,例如控制反转(IOC)和依赖注入(DI)。这些模式有助于创建和自动化MVC组件的“布线”,以便它们可以相互通信。在这方面,您可能希望看看微软的ObjectBuilder[如果目标是.NET 2.0],或者如果您想要更新的东西,可以使用更轻的Unity容器滚动。我个人更喜欢城堡温莎容器为我所有的国际奥委会工作,但团结是成熟的,我可能会很快转换。

    当然,您也可以查看这些组件所属的框架,分别是复合应用程序块[cab]或prism,但我不建议这样做。他们提供了一些漂亮的解决方案(你可以用cab的eventbroker在winforms中模拟“命令”),但是除非你有特定的要求,否则在imo中会有很多额外的负担。

    如果您了解模式[MVC],那么模板在很大程度上是不必要的。


    如果您想知道这些位是如何组合在一起的,一个快速的MVC崩溃课程:

    // this is the controller. manages business logic, handles
    // Gui inputs
    public class Controller
    {
    
        // constructor
        public Controller (View view, Model model) 
        {
    
            // nothing magical about wiring things up
            // obtain reference through ctor here, and
            // hook up events, or provide bindings
    
            // attach event handler - obviously make sure
            // signatures match etc, i'm winging this whole thing
            view.ButtonSubmit.Click += Submit;
    
            // understand this will set the value of view's
            // text once, here on instantiation. the problem
            // with WinForms is databinding, you will have to
            // roll your own. to provide proper databinding
            // Model class must expose events when they change
            // and then Controller or View must listen to event
            // and bind new value to View
            view.TextBox.Text = model.Count;
    
        }
    
        // Submit button clicked!
        public void Submit () { ... }
    
    }
    
    // this is the model. contains all data relevant
    // to Gui component
    public class Model
    {
        // a count of stuff
        public int Count { get; set; }
    }
    
    // this is the view. simple WinForms object,
    // contains buttons, white fluffy clouds, and other
    // good stuff
    public class View : Control
    {
        public TextBox TextBoxCount;
        public Button ButtonSubmit;
    }
    
    public static class Program
    {
        static void Main ()
        {
            // you can do this explicitly like below, or implicitly
            // if you configure and use an IoC container.
            View view = new View ();
            Model model = new Model ();
            Controller controller = new Controller (view, model);
        }
    }
    

    MVC+WinForms的主要缺点在控制器中。控制器直接引用winforms对象,这使得大多数场景难以进行单元测试。您想要的是从GUI元素和它们抛出的事件中抽象出来。另外,您不希望执行我在内联注释中提到的重复数据绑定任务。wpf通过引入命令和数据绑定来添加这些内容。

        3
  •  1
  •   James Bloomer    15 年前

    也许值得一看 Smart Client Software Factory . 主要关注的是复合应用程序,但确实如此 use views 我认为MVP模式。

    对于WPF或Silverlight,请查看 Prism .

        4
  •  1
  •   ChrisF    15 年前

    虽然不是Winforms WPF Toolkit From CodePlex具有用于Visual Studio的模型视图应用程序模板。

    This MSDN article 在模式上也有一些可能有用的可下载内容。

        5
  •  1
  •   Community CDub    8 年前

    这个 SO link 讨论了一些有用的WPF框架,有些来自MS。