Click here to Skip to main content
16,016,693 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Suppose In a multi layer architecture i want to send data from 1 layer to another by calling a function and passing the respective value .Now which methods should I use to do so for better performance, by passing the parameters separately to the function or assigning values to class variable and by passing that class object.

Example:

C#
int ID="01";
string Name="test";
String gender="Male";
string result= GetData(ID,Name,gender);


or

C#
Employee obj=new Employee();
obj.ID="01";
obj.Name="Test";
obj.Gender="Male";

string result= GetData(obj);


There can be more than 15 parameters at a time.
If there is any better way to do so please let me know it.
Posted
Updated 26-Jul-15 19:03pm
v3

Depends what the method is going to do doing with it.
Under normal circumstances, passing the object will probably be better - provided the class definition is public.
And if the method needs to change any of the values, then passing the object itself is the best way.
If the method must not be able to change the object values, or if it must not be allowed access to some values then passing the individual parts may be better.
 
Share this answer
 
Comments
pranav8265 27-Jul-15 1:25am    
thanks its really helpful.

What about the performance ?
OriginalGriff 27-Jul-15 1:46am    
Sending the object itself will be faster, because the system doesn't have to stack and unstack 15 references - just one.
pranav8265 27-Jul-15 1:51am    
thank you sir for clearing my doubt ...
:)
This somewhat depends on the amount of parameters and their datatypes.

Basically when you pass and object reference you're not spending extra memory, so if you object ever existed anyway and isn't too tightly tied to your data structure (which could cause copying to loose that,) passing the object is more efficient.

The border case is when the parameters being passed are reference types anyway, but with value types you'll be copying them https://msdn.microsoft.com/en-us/library/9t0za5es.aspx
unless of cause you're using ref keywords which is also not very sexy.

Generally the debate can revolve around the principle of immutable data objects started off with an excellent series by Eric Lippert http://blogs.msdn.com/b/ericlippert/archive/2007/11/13/immutability-in-c-part-one-kinds-of-immutability.aspx quite a while ago.

Point being that though Generally objects are fine as parameters, when methods working on objects modify them, they in turn are nolonger immutable, which can make coding less transparent.
Something i at large ignore (but acknowlege) myself.

My personal perference goes towards that when moving towards data storage i tend to move towards parameters, as especially with early days EF in play, you'll anyway have to read the entity from database to update it. Inversely moving from data to other layers the move is to establish your data carrier object og dispach it from data layer somewhere after having instantiated it from your data interacting type.

With EF from a certain version we get Detachability, but that often still takes some doing.
https://msdn.microsoft.com/en-us/library/vstudio/bb896271(v=vs.100).aspx
and
https://msdn.microsoft.com/en-us/library/vstudio/bb738697(v=vs.100).aspx
 
Share this answer
 
Comments
pranav8265 27-Jul-15 1:55am    
Thats a nice explanation .Thank you..
With fifteen values I'd go with passing an object.
 
Share this answer
 
Comments
pranav8265 27-Jul-15 1:21am    
thanks for the rely,

i am also thinking of doing the same but will it not affect the performance ??
PIEBALDconsult 27-Jul-15 2:14am    
Yes, for the better. But my primary concern is maintenance.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900