Where are these 20-50 methods? All in a object or in different objects?
If you have 20-50 objects with the same method, you can write a base class
public absrtact class Base
{
public object TheMethod(string[] someParams)
{
ResouceObj resObj = null;
ResourceTrans resTran = null;
object returnVal=null;
try
{
resObj = GetResource();
resTran = resObj.StartTrans();
// METHOD SPECIFIC CODE GOES HERE
returnVal=this.TheMethodSpecificCode(); // do you need a return
value??
}
catch(System.Exception e)
{
LogError(e);
}
finally
{
resObj.Close();
resTran.Close();
}
return returnVal;
}
protected virtual object TheMethodSpecificCode(){return null;}
}
public class FirstChild:Base
{
protected override object TheMethodSpecificCode(){return "I'm the first
child";}
}
public class SelfClass:Base
{
protected override object TheMethodSpecificCode(){return this;}
}
public class RandomClass:Base
{
protected override object TheMethodSpecificCode(){return new
Random().Next;}
}
"Tim Smith" <ti*******@hotmail.com> schrieb im Newsbeitrag
news:a7**************************@posting.google.c om...
I have 20-50 methods (C# or Java) with the following identical content
which I wish to write only once!
public SomeObjOrVoid MethodDiffSigEachTime(SomeParams p) {
ResouceObj resObj = null;
ResourceTrans resTran = null;
SomeObjOrVoid returnVal = null;
try {
resObj = GetResource();
resTran = resObj.StartTrans();
// METHOD SPECIFIC CODE GOES HERE
} catch (Exception e) {
LogError(e);
} finally {
resObj.Close();
resTran.Close();
}
return returnVal;
}
I tried to use reflection but I cant pass in the method name since it
is a helper method and if I use a hard coded string (risking runtime
types), it loses on performance and exceptions lose a lot of
information.
There must some way to design around duplicating so much code for this
service orientated architecture...