I'm creating a parsing application, which parses ~20 sites, ~7-15 values from each. Pseudocode looks like:
ParserA : ParserBase
{
public override SomeEntity Parse(...)
{
SomeEntity se = new SomeEntity();
//some code, parsing value1;
//some code, parsing value1;
//some code, parsing value1;
//some code, parsing value2;
//some code, parsing value2;
//some code, parsing value2;
//some code, parsing value3;
//some code, parsing value3;
//some code, parsing value3;
//some code, parsing value4;
//some code, parsing value4;
//some code, parsing value4;
...
return se;
}
}
ParserB : ParserBase {...}
ParserC : ParserBase {...}
...
etc.
As soon as parsers have never done well with html(layouts happen to change during time), I need to implement exceptionHandling and logging. I need to parse as much as possible, and errors must be logged. I know 2 ways to deal with it:
public override SomeEntity Parse(...)
{
SomeEntity se = new SomeEntity();
try {
//some code, parsing value1;
//some code, parsing value1;
//some code, parsing value1;
//some code, parsing value2;
//some code, parsing value2;
//some code, parsing value2;
//some code, parsing value3;
//some code, parsing value3;
//some code, parsing value3;
//some code, parsing value4;
//some code, parsing value4;
//some code, parsing value4;
...
}
catch (Exception e)
{
//Log
}
return se;
}
Pros: Easy to implement
Cons: if i get exc at value5, i have no chance to parse value6,7,.. etc.
2)
ParserA : ParserBase
{
public override SomeEntity Parse(...)
{
try
{
//some code, parsing value1;
//some code, parsing value1;
//some code, parsing value1;
}
catch(Exception e)
{
// Log
}
try
{
//some code, parsing value2;
//some code, parsing value2;
//some code, parsing value2;
catch(Exception e)
{
// Log
}
try
{
//some code, parsing value3;
//some code, parsing value3;
//some code, parsing value3;
catch(Exception e)
{
// Log
}
try
{
//some code, parsing value4;
//some code, parsing value4;
//some code, parsing value4;
catch(Exception e)
{
// Log
}
...
}
}
Pros: Everything, that can be parsed, is parsed;
Cons: Too much copypaste (remember 20 parsers, 7-15 values in each.
I want to write less, do more, so i've implemented Safecall function which takes delegate and executes it inside a try-catch block, and logs ot. So now i have to write this:
SafeCall( () => {
//some code, parsing value4;
//some code, parsing value4;
//some code, parsing value4;
});
instead of this:
try
{
//some code, parsing value4;
//some code, parsing value4;
//some code, parsing value4;
catch(Exception e)
{
// Log
}
Is this a good solution or i'm reinventing a square wheel?