Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC-like design for console applications?

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?

like image 934
Robert MacLean Avatar asked Jul 10 '09 07:07

Robert MacLean


2 Answers

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:

  • MSDN on MVP - http://msdn.microsoft.com/en-us/library/cc304760.aspx
  • Martin Fowler on Supervising Controller - http://martinfowler.com/eaaDev/SupervisingPresenter.html
  • Martin Fowler on Passive View - http://martinfowler.com/eaaDev/PassiveScreen.html
  • Jeremy Miller on MVP for ASP.Net - http://codebetter.com/blogs/jeremy.miller/archive/2006/02/01/137457.aspx. You might also check out his Build Your Own CAB series. It teaches MVP as it applies to WinForms.
  • Polymorphic Podcast screencast on Supervising Controller - http://polymorphicpodcast.com/shows/mv-patterns/ (Highly recommended)

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.

like image 156
Brad Gignac Avatar answered Nov 10 '22 09:11

Brad Gignac


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();
        }
    }
like image 45
Gaurav Jain Avatar answered Nov 10 '22 09:11

Gaurav Jain