单独发送参数与将类对象发送到被调用函数? [英] Sending parameters separately vs sending class object to called function?

查看:50
本文介绍了单独发送参数与将类对象发送到被调用函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设在多层架构中,我想通过调用函数并传递相应的值来将数据从1层发送到另一层。现在我应该使用哪些方法来实现更好的性能,方法是将参数单独传递给函数或者将值赋给类变量并传递该类对象。



示例:



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:

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











or

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

string result= GetData(obj);





一次可以有超过15个参数。

如果有更好的方法,请告诉我。



There can be more than 15 parameters at a time.
If there is any better way to do so please let me know it.

推荐答案

取决于该方法将如何处理它。

在正常情况下,如果类定义是公共的,传递对象可能会更好。

如果方法需要更改任何值,那么传递对象本身是最好的方法。

如果方法必须无法更改对象值,或者它必须不是允许访问某些值然后传递单个部分可能会更好。
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.


这在一定程度上取决于参数的数量及其数据类型。



基本上当你传递和对象引用时,你不会花费额外的内存,所以如果你反对的东西仍然存在而且不是太紧绑定到您的数据结构(可能导致复制松散),传递对象更有效。



边界情况是传递的参数是引用类型无论如何,但是你会复制它们的价值类型 https://msdn.microsoft.com/en- us / library / 9t0za5es.aspx

除非您使用的ref关键字也不是很性感。



一般来说,辩论可以围绕不可变数据对象的原理展开,由Eric Lippert撰写的优秀系列文章 http://blogs.msdn.com/b/ericlippert/archive/2007/11/13/immutability-in-c-part-one-很久以前的种类(immutability.aspx)。



指出虽然通常对象可以作为参数,但是当处理对象的方法修改时反过来,它们不再是不可变的,这会使编码变得不那么透明。

我自己忽略了一些东西(但是承认)。



我的个人偏好倾向于在向数据存储方向发展时,我倾向于转向参数,特别是在早期的EF游戏中,你无论如何都必须从数据库中读取实体来更新它。反过来从数据移动到其他层,移动就是建立你的数据载体对象,然后在从数据交互类型中实例化后从数据层发送它。



随着从特定版本的EF我们得到可拆卸性,但这通常还需要一些。

https://msdn.microsoft .com / zh-CN / library / vstudio / bb896271(v = vs.100).aspx



https://msdn.microsoft.com/en-us/library/vstudio/bb738697(v=vs .100).aspx
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


使用十五个值我会传递一个对象。
With fifteen values I'd go with passing an object.


这篇关于单独发送参数与将类对象发送到被调用函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆