开发者

Writing dot net desktop application with MVC design pattern

开发者 https://www.devze.com 2023-02-02 09:42 出处:网络
I got an assignment to make a Desktop application using C#. It has to be done using MVC design pattern, but I can\'t find any tutorial which can show how to do it with Desktop based application. All t

I got an assignment to make a Desktop application using C#. It has to be done using MVC design pattern, but I can't find any tutorial which can show how to do it with Desktop based application. All the tutorials which I can find 开发者_StackOverflow社区show how to do it for web (asp.net).

So I was wondering if someone can suggest me a book or online tutorial which is doing this?


I always learned by doing so here's a very basic example. Web or Windows, doesn't matter...

Model

// basic template for what your view will do
public interface IProgram
{
    public string FirstName { get; set; }
}

View

public class Program : IProgram
{
    ProgramController _controller;
    public Program()
    {
        // pass itself to the controller
        _controller = new ProgramController(this);
    }

    public string FirstName 
    {
        get { return firstNameTextBox.Value; }
        set { firstNameTextBox.Value = value; }
    }
}

Controller

public class ProgramController
{
    IProgramView _view;
    public ProgramController(IProgramView view)
    {
        // now the controller has access to _view.FirstName
        // to push data to and get data from
        _view = view;
    }
}

You can bind any members this way, such as events


Since this is homework, I believe that you teacher and, more importantly, yourself, desire to learn the tricks behind MVC. So, while checking MVC Frameworks might help, I recommend you implement basic functionality on your own.

That said, take a look at Wikipedia's article first (which, unfortunately isn't that good), then check Microsoft's take on it.

Once you grasp the concepts, try implement a basic triplet that does "something", nothing really fancy. If you have doubts, come back to SO so that we can solve then. And don't forget the new chat functionality of SO.


There are a few different avenues you might look at.

  • You might consider implementing the pattern yourself. In order to implement the pattern from scratch, you really would need to understand what MVC is trying to solve.
    • Consider reading about Design Patterns. I know that Head First Design Patterns includes an introduction to the MVC pattern.
    • You may consider exploring ASP.NET MVC. Even though it is for the web and not the desktop, understanding what problems ASP.NET MVC is solving by using the MVC pattern will help you to understand how to best implement a solution for the desktop.
  • See if anyone has implemented MVC with WinForms
    • For example, see the question Looking for a MVC Sample for WinForms
  • You also might consider looking for suggestions on implementing the MVC Pattern with WPF or Silverlight.
    • Although the common buzzword pattern surrounding WPF and Silverlight is MVVM (Model-View-ViewModel), some people still prefer to use an MVC implementation with these technologies.
    • In fact, if you consciously create View Models to accompany your views even when using the MVC pattern, you've essentially an "M-V-VM-C" pattern that ensures that your views only know about data from the model that pertains to that particular view.
0

精彩评论

暂无评论...
验证码 换一张
取 消