简体   繁体   中英

C#: Some ideas about application structure

I have a project and am right now at a point, where it's growing complexity forces me to think about it's structure. To explain it in a few sentences:

  • I have a main application as UI. It allows the user to run different tests and should display status updates and results.
  • I have a whole bunch of classes containing different tests on some hardware. All those classes implement an iTest interface to ensure compatibility.

Where I am right now: I want to have status updates (which I don't get right now). I need some kind of StatusUpdate-Event fired in the Test-Classes and receive those events in my UI. I'm new to delegates and event handlers so I have no clue which way is the best to choose for this. Following the answer from here " How to add an event to a class " means I'd have to add a event handler each time I instantiate one of my test classes. That would generate a whole load of redundant code that I think is unnecessary.

I'd like to have one function in my UI named sth. like "StatusUpdateEventHandler(string textToDisplay)". It should not matter, which Test-Class invokes this event.

Can anyone give me some direction where to go at? Thank's a lot :)

I'll use a base class that have common properties/methods/events to report progress back to the UI. Quickly, this is how I would approach the problem:

Create an event Handler to report progress:

public delegate void StatusChangedEventHandler(object sender, StatusChangedEventArgs e);

Create an abstract base class:

public abstract class TestBase:ITest
{

    // An event that clients can use to be notified whenever the
    // elements of the list change.
    public event StatusChangedEventHandler StatusChanged;
    protected virtual void OnStatusChanged(StatusChangedEventArgs e)
    {
        if (StatusChanged != null)
            StatusChanged(this, e);
    }

    protected virtual void ReportProgress (int percentage, string step)
    {
        OnStatusChanged(new StatusChangedEventArgs()
        {
            Percentage = percentage,
            Step = step
        });
    }

    public abstract void Execute();

}

Create a EventArgs that will be used to get data from the event in your UI:

public class StatusChangedEventArgs:EventArgs
{
    public int Percentage { get; set; }
    public string Step { get; set; }    
}

Then make sure that your tests use the base test class:

public class SimpleTest : TestBase
{
    public override void Execute()
    {
        ReportProgress(10, "STEP 1");
        ReportProgress(25, "STEP 2");
        ReportProgress(55, "STEP 3");
        ReportProgress(70, "STEP 4");
        ReportProgress(100, "STEP 5");

    }
}

Then hook up the event to your UI, here's my implementation in a console application:

class Program
{
    static void Main(string[] args)
    {
        SimpleTest simpleTest = new SimpleTest();

        simpleTest.StatusChanged += SimpleTest_StatusChanged;

        simpleTest.Execute();

        Console.ReadLine();

    }

    private static void SimpleTest_StatusChanged(object sender, StatusChangedEventArgs e)
    {
        Console.WriteLine($"{e.Percentage} - {e.Step}");
    }
}

I couldn't say you provided a lot of info to suggest a really helpful response. :-) It seems to me you look for something like generic events , see example within the article. You'd subscribe to the events which are fired by the test units, so the relevant string is passed along with each particular event. Besides, you could consider some class hierarchy to support your test units.

Hope it brings you some fresh thoughts to find the direction.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM