473,387 Members | 3,787 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Method Design Help - delegates dont help

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...
Nov 16 '05 #1
2 1007
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...

Nov 16 '05 #2
Different services. We did try reflection but didnt gain much
benefit.

We did take a step back and looked at the overall design and realized
that the resource aquisition and usage could drop to a lower layer as
so many methods only used the resource once.

thanks

ti*******@hotmail.com (Tim Smith) wrote in message news:<a7**************************@posting.google. com>...
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...

Nov 16 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: bigoxygen | last post by:
Hi. I'm using a 3 tier FrontController Design for my web application right now. The problem is that I'm finding to have to duplicate a lot of code for similar functions; for example, listing...
3
by: memememe | last post by:
Is there a way to pass a method as a paramenter when you call a method, kinda like you do when you create an event handler. Basically I want to be able to call method A() and tell it to call method...
2
by: Jon Davis | last post by:
The garbage handler in the .NET framework is handy. When objects fall out of scope, they are automatically destroyed, and the programmer doesn't have to worry about deallocating the memory space...
1
by: R Reyes | last post by:
Hello All, I'm always looking for ways to improve my code. Most of the time (whenever I'm working on a project) I write a bunch of functions. Then after the project is finished, I put all the...
18
by: JohnR | last post by:
From reading the documentation, this should be a relatively easy thing. I have an arraylist of custom class instances which I want to search with an"indexof" where I'm passing an instance if the...
8
by: Gaetan | last post by:
Is is possible in C# to have the equivalent of an array of function pointers in C? I have a situation where a top level class exposes methods like Add, Delete, ... and a few child classes with...
7
by: traafat | last post by:
Hello guys, i am having a problem using Delegates, i am pretty new to C# , i am starting to fall in love with it its really good language (coming from Java and Delphi), now i am working on an...
9
by: raylopez99 | last post by:
Here are two different ways of achieving a mediator pattern: the first, using circular references (for lack of a better way to describe it), but not using delegates, with the second using...
4
by: Pallav singh | last post by:
Hi , when should i select Factory Method / Prototype Design Pattern during my design phase ?? as both look similar to me Thanks in Advance Thanks Pallav
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.