guyl
guyl

Reputation: 2242

Program architecture questions

I have couple of questions about Program architecture / design.
1. I am refactoring one of my program completely, the purpose of the program is to transfer file from client side to server side (like drop box or cyber ark). It can create number of clients according to the configuration, its all under window service. lets look at the start code:

public class Master
{
  private List<Box> _boxes = new List<Box>();

  public Master()
  {
     Initialize(); ...
  }

  public void Run()
  {
     foreach(var box in _boxes)
     {
        box.Run();
      }
  }
  }

Each Box class is "Living" by its own, but if one of them fail with unmanaged exception then the whole service is lost, or by managing the resources better. my thought was that the window service is the master class that will start number of Boxes as a different Process, or using the appDomain class.

  1. My second question is - i simply lack the knowledge of program architecture / design, do any of you guys know of a recommended book / article / link to read ? Thanks

P.S. Does any of you also cant see the arrows and the StackExchange link in Google chrome browser ?

Upvotes: 1

Views: 225

Answers (3)

djna
djna

Reputation: 55937

I see two fundamental decisions here.

First, are there failure modes of a Box that should require the whole process in which is runs be restarted?

Then, if there are such failure modes can we limit the impact of the failure modes by separating the Boxes into their own process spaces? That is shall we increase isolation in order to improve overall stability?

The principle of isolation is very commonly applied, for example in Java EE applciation servers we may deliberately choose to the isolate some applications from others because some applciations are unstable - unstable JNI code tends to bring down the whole process.

My take is that this kind of isolation is usually needed because we have some reason not to trust some code. Now in your case you're writing the Box, is it really impossible to catch all the Exceptions and handle them? Even if a Box should need to stop can't we catch the problem and start a new instance?

So I'd be trying to harden my Box class so that isolation is not needed.

Upvotes: 1

Jalal Said
Jalal Said

Reputation: 16162

You can't use single try/catch in the foreach because you are running threads inside the Box class when calling box.Run(), however you should predict the exceptions inside the Box class and handle them there, but if you are unable to do that "maybe because you call another third party library that itself running some threads, that may cause exceptions", then is it an option for you to create and run each box in a different application domain? here an example how to do it, by doing so you can handle each box exception separately by handling each of the boxes` application domain exceptions:

myBoxDomain.UnhandledException += OnCurrentDomain_UnhandledException;

So you can handle each box failure here without affecting the other boxes.

Upvotes: 1

Mark Cidade
Mark Cidade

Reputation: 99997

Why don't you just handle any exceptions?

public void Run()
{
    foreach(var box in _boxes)
    {
       try { box.Run(); }
       catch(Exception ex){ /* handle exception */ }
    }
 }

Upvotes: 1

Related Questions