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?
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.
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();
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With