How to show messages depending of the program's nature
<p>Please, sorry for the long text.</p>

<p>I have a library of C# Classes which retrieves information from various systems, invokes external functions, validates this info and generates results of the validation to a file. This library can  indistinctly be invoked from: 1) a DOS console program, 2) from a Windows form or 3) from a windows service program.</p>

<p>Users have asked me for modifying the library so it can run in a verbose way, i.e. to show the date and time at certain steps, to briefly explain what a certain step or method is doing, etc. They also asked me for introducing exception handling and show error messages.</p>

<p>If I execute in console mode, I should show the messages on the console, if I execute from a Windows form I should send the messages to a message box or to a text box, and if I run from a Windows Service I should send the messages to a log file.</p>

<p>I have 2 questions:</p>

<p>1)  How should I modify the C# Classes so I can avoid the constant evaluation:
If program is in console mode writeline…..
Else if program is in windows forms mode messagebox or textbox.text …
Else if program is invoked from a windows service program writelogfile….</p>

<p>2)  If the above evaluation  is unavoidable, how can I know if the library of C# classes is being invoked by a console, windows form  windows service program?</p>

<p>Thanks!!</p>

<hr>

<p>Possible solution:
<hr></p>

<p>I read a little about MS Enterprise Library and ... it is too much for my modest requirement so I went for the pragmatic solution of Reed.</p>

<p>I share the way I solved the user requirements, it is a preliminary model before applying to the real situation so, before accepting the Reed solution, your comments to improve and learn are really welcome.</p>

<p>1) I created a little library to define all possible outputs, in this case I defined only the outputs to a console and to a WPF form:</p>

<p>namespace diClass
{
    public interface IVerbose
    {
        void show(string msg, object textBox);
        void notify(string msg);
    }</p>

<pre><code>public class ConsoleOutput : IVerbose
{
    public void show(string msg, object textBox)
    {
        System.Console.WriteLine(msg);
    }

    public void notify(string msg)
    {
        System.Console.WriteLine("Notif: " + msg);           
    }

}

public class WpfOutput : IVerbose
{
    public void show(string msg, object textBox)
    {
        TextBox tb = (TextBox)textBox;
        tb.Text += msg + "\r\n";
        System.Windows.Forms.Application.DoEvents();
    }
    public void notify(string msg)
    {
        MessageBox.Show(msg);
    }
}

public class Output
{
    IVerbose outputType;

    public IVerbose verboseType
    {
        get { return outputType; }
        set { outputType = value; }
    }

    public void show(string msg, object textBox)
    {
        outputType.show(msg, textBox);
    }

    public void notify(string msg)
    {
        outputType.notify(msg);
    }
}
</code></pre>

<p>}</p>

<p>2) Then I defined a console program which uses the above library:</p>

<pre><code>class Program
{
    static void Main(string[] args)
    {
        IVerbose verboseType = (IVerbose)Activator.CreateInstance(typeof(diClass.ConsoleOutput));
        Output o = new Output();
        o.verboseType = verboseType;
        object textBox1 = null;
        o.show(string.Format("{0}: Reading Accountig data", DateTime.Now.ToString("dd/MM/yy hh:mm:ss")), textBox1);
        o.notify(string.Format("{0}: Application error", DateTime.Now.ToString("dd/MM/yy hh:mm:ss")));
    }
}
</code></pre>

<p>3) Finally I defined a WPF program which also uses the above library:</p>

<pre><code>public partial class Window1 : Window
{
    public Window1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, RoutedEventArgs e)
    {
        IVerbose verboseType = (IVerbose)Activator.CreateInstance(typeof(diClass.WpfOutput));
        Output o = new Output();
        o.verboseType = verboseType;
        o.show(string.Format("{0}: Reading Accountig data", DateTime.Now.ToString("dd/MM/yy hh:mm:ss")), textBox1);
        o.notify(string.Format("{0}: Application error", DateTime.Now.ToString("dd/MM/yy hh:mm:ss")));
    }
}
</code></pre>

<p>In programs 2) and 3) the last two lines of code are equal, there is no <em>ifs</em> and, that is what I wanted as answer.</p>

<p>Regards.</p>
