I need to advice or idea on how to implement the following. I have an interface with lots of methods each can throw exception (in fact it is WCF call). So the each call must be wrapped by try block
public interface ISomeInterface
{
MethodThatCanThrow1(Arg1 arg);
..
MethodThatCanThrow101(Arg2 arg);
}
Now we have a collection of objects
var items = new List<ISomeInterface>();
Now I have to call MethodThatCanThrow1 method in loop for every object. Method can throw exception, in that case I need to continue for remaining ojects
void CallMethodThatCanThrow1()
{
foreach(var item in items)
{
try
{
item.MethodThatCanThrow1(Arg1 arg);
}
catch(Exception ex)
{
// do something
}
}
}
Now I need to call MethodThatCanThrow2 So for second method I need to copypaste the try catch block stuff.
void CallMethodThatCanThrow2()
{
foreach(var item in items)
{
try
{
item.MethodThatCanThrow2(Arg2 arg);
}
catch(Exception ex)
{
// remove failed item from items
// continue foreach for the rest
}
}
}
So for the rest 101 method I have to copy paste the whole block only changing the method name.
So I am thinking about refactoring it. What I want is put try catch block in separate Methodand pass the Method Name that needs to be called
void CallMethodofISomeInterfaceForGivenReference(delegate methodProvide)
{
foreach(var item in items)
{
try
{
// take item and call method that is provided
}
catch(Exception ex)
{
// do something
}
}
}
Aucun commentaire:
Enregistrer un commentaire