MVC-like design for console applications?

15,252

Solution 1

I think you'll find that the a popular alternative to Model View Controller is Model-View-Presenter. The model is basically the same between the two. The role of the controller and view are very similar, but they may get a little more responsibility depending upon your implementation. Within MVP, there are two implementation methods: Supervising Controller and Passive View. MVP is usually considered the standard architecture for WinForms clients and can be applied to WebForms as well. Here are some relevant links for more information:

Finally, if you want to pick up a book, Agile Principles, Patterns, and Practices in C# contains an excellent walkthrough for building a console-based payroll application. Once compeleted, they build to WinForms UI to show how their application architecture allowed them to add a new view with minimal fuss.

Solution 2

MVC for console application example:

public interface IController
{
 void RequestView(IView view);

        IView ResponseView();
}

public interface IView
{
        int GetID { set; get; }
        void DisplayId();

}
public interface IModel
{
        int GenrateID(int id);
}

//Business logic in Model
public class Model : IModel
{
        public int GenrateID(int id)
        {
            id = id * 10;
            return id;
        }
}

//Event Logic in Controller
public class Controller : IController
{
        private IModel model;
        private IView responseView;
        public Controller()
        {
            responseView = new View();
        }

        public void RequestView(IView view)
        {

            model = new Model();
            responseView.GetID = model.GenrateID(view.GetID);
        }

        public IView ResponseView()
        {
            return responseView;
        }
}
//Display Logic in View
public class View : IView
{
        public int GetID
        {
            get;set;
        }

        public void DisplayId()
        {
            Console.Write(GetID);
        }
} 

class Program
    {
        static void Main(string[] args)
        {
            IController ctr = new Controller();
            int input =int.Parse(args[0]);
            IView view=new View()
            {
                GetID = input
            };

            ctr.RequestView(view);
            view =ctr.ResponseView();
            view.DisplayId();
        }
    }
Share:
15,252
Robert MacLean
Author by

Robert MacLean

Robert is a senior software developer. What does that mean? Senior, because he feels tired all the time and standing for too long hurts his back. Software, because as a child (and an adult) he played with Lego and not Meccano. Developer, because he loves to drink coffee and create things. Robert believes his best skill is the ability to quickly learn and understand technology but in reality it is his ability to hide bodies of those who have seen him make mistakes (pro tip: 2 parts lime, 1 part bleach). He will tell you his biggest weakness is his emotions, but actually he can't snap his fingers and that has cost him more than he will ever know. Robert attempted cloning once and is very proud of the result which he has nicknamed "son". Robert maintains a blog at www.sadev.co.za, because he forgets often and this means he can remind himself of what he used to know (it is a save point for the brain). Robert hangs out on Twitter, which makes him feel normal (he think you are all very weird).

Updated on June 04, 2022

Comments

  • Robert MacLean
    Robert MacLean almost 2 years

    I find that writing web apps and WinForm apps generally come out a lot cleaner than when I write a console application.

    What do I mean by cleaner? Well the fact that the fact the UI (i.e. readline/writeline) is so intertwined with the logic code becomes horrible and the fact it is not event driven means that it is harder to get good abstraction.

    I was thinking about this and MVC does try to solve similar issues for web apps, so my question is there anything like that for console apps? or any guides to get better design into console apps?

  • Brad Gignac
    Brad Gignac almost 15 years
    I apologize for the lack of actual hyperlinks, but I'm a new user and am capped on the number that I can post.
  • CoderDennis
    CoderDennis almost 15 years
    @Brad, welcome to StackOverflow! I added the hyperlinks to your post.