views:

433

answers:

6

I have recently programmed a console application and I've experienced a lot of pain in designing it in many aspects, particularly in C#, given its pure OO paradigm. Questions I have faced include anything from how to pass the options to how to return problems to the entry point class, among many, many others.

My question is: would any of you know of good designs of console applications in an OO paradigm so I can learn from them? Code of good implementations is particularly welcome.

EDIT: I'm not after command-line APIs, but after good design principles, and, in particular, good implementations I could learn from.

EDIT 2: There is simple user interaction in the application, but it is not a full-fledged CLI/REPL sort. Think of it as the TeX command, more or less. Interestingly, even though there is good theory floating around (no different than X, use pattern Y, you should know OO principles...[your computer science professor would be so proud!]), there is no real code I can take a look at to see these concepts in action. Again, where should I look (code!) for a good command line application in a pure OO paradigm?

+1  A: 

A console application is no way different from a regular win forms or web application when it comes to key cross cutting concerns like logging, error and exception handling, security,etc.,

Having said that can you please detail our question as to where they key areas of concern are?

You can draw on many patterns from App Architecture Guide from Microsoft.

CodeToGlory
+1 for the pointer to the useful App Architecture Guide!
Alex Martelli
Same here. +1 for the guide, excellent link.
Dervin Thunk
Anyone able to open the App Architecture Guide with Preview on a Mac running 10.5.6?
msiemeri
Hmm..wondering how I got some down votes on this answer..
CodeToGlory
+1  A: 

In Java I often use Apache CLI, I've googled "CLI for .NET" but it doesn't seem to be any implementation yet. Maybe is useful for you to read Java approach for receiving console parameters.

For the architecture of your app, it should be not very different from the architecture you'd use in a Web App, the difference is the console is the interface of your presentation tier. Maybe you can define some 'ConsoleControllers' to receive the model from the business tier to the console and viceversa. Think about every user input as a request ;).

victor hugo
nice pointer - I should pay more attention to the apache commons libraries
msiemeri
A: 

In its most (extreme) general sense, a console application is a type of REPL:

http://en.wikipedia.org/wiki/REPL

Obviously, a domain specific (console) application is not supporting a turing complete language, but many of the issues overlap, and a good REPL needs to be a very robust command line application. I'm sure you can learn a lot studying the design and implementation of a typical REPL. (Most of the REPLs are open source. (Try ruby or python for OO REPLs))

+2  A: 

It sounds as if you're building an interface that performs one of several distinct operations with each invocation. I'm not sure if you're referring to a "command-line" application (which does one action, then exits) or a CLI application (which displays a prompt and responds repeatedly to user input). In general, the former will be much simpler to build than the latter; I think it only makes sense to use a CLI if your application requires some persistent state that evolves over multiple commands. If you are tackling something like this, then alphazero is correct -- you should probably learn about REPLs and copy a good one.

In any case, the operation performed will depend on the arguments passed on the command-line, so I'll brainstorm about that part...

It's sensible think of the application as a set of distinct "Command" objects, one for each type of operation. The entry-point to the application should thus be a some sort of CommandLineDispatcher object that dispatches requests to the appropriate Command object.

To be modular, the dispatcher should be configured with an abstracted mapping (eg, a Hashtable) to associate each command token (usually the first word of the command-line string) to the Command object that handles it. The dispatcher may also handle common options-parsing, probably using some off-the-shelf "getopts" library to do the heavy lifting.

To start simple, each Command object could implement a consistent interface for doing its work; maybe something like this:

public void execute(List<String> args)

This way, the entry-point dispatcher just finds the Command being requested, and executes it.

Regarding error-handling: the execute() method might just throw an exception to communicate errors... Exception could be caught and processed by the dispatcher, or simply logged to the screen. Alternatively, failing Commands could invoke some shared usage function to combine an error message with general instructions. I don't think the "entry point" should necessarily be made aware of problems as you suggest; if you need robust error-handling (eg, for logging or alerting capabilities), this seems like it belongs in a separate component that could be provided to the Command object.

In general, a command-line application is no different than any other application that responds to user input -- you'll need a dispatcher to parse and route the input, and handlers (aka "controllers") to execute the supported operations. If you need other services (logging, alerting, database connectivity, etc), you'll do well to create separate components to isolate this logic and expose it with clean interfaces.

joshng
A: 

Is this a single-execution-per-invocation app, where you run it, it does something, and exits; or are you trying to write something with a user interface that prompts and accepts input (and maybe loops around to another prompt)?

le dorfier
A: 

For a program to have good architectural design especially in C# you should be familiar with OO design concepts to fully take advanage of OO paradigms

Izabela