如何将长参数列表传递给方法或实现此目的的任何其他最佳方法
我将 20 多个不同类型的参数传递给方法。应该有一些干净的方法来解决这个问题。你能帮我一下吗?
我可以传递包含所有这 20 多个参数的对象数组,但在目标方法中我必须对类型进行检查。这是传递一长串参数的好方法吗?
代码:代码示例不完整
private DataTable CreateDataTable(string[] args)
{
DataTable dt = new DataTable();
dt.Clear();
foreach (var arg in args)
{
dt.Columns.Add(arg);
}
return dt;
}
我可以在此方法中传递数组,因为所有参数都具有相同类型。
DataTable dt = CreateDataTable(new string[] { "ProjectId",
"ParentProjectId",
"ProjectName",
"CreationDate");
现在,我有超过 20 个不同类型的值,如下所示
int projectId = 100;
int? parentProjectId = SomeCondition ? 10 : null;
string projectName = "Good Project"
DateTime createdDate = DateTime.Now;
.
.
.
在这个方法中,我会将值分配给列。
AssignValuesToDataTable(dt, Arguments list ......)
// Implementation would be like this. Here I am passing 20+ arguments.
private DataTable AssignValuesToDataTable(DataTable dt, arguments list ........)
{
DataRow row = dt.NewRow();
row["ProjectId"] = projectId;
.
.
.
dt.Rows.Add(row);
}
你能帮忙吗?我正在使用 C#4
编辑: 上面的代码是我的真实代码的示例,但我更感兴趣的是知道实现此目的的最佳方法是什么。
出自《编码恐怖》(杰夫·阿特伍德)
方法的参数越多,它就越复杂。限制 给定方法中所需的参数数量,或使用对象 组合参数。
以上引用来自这篇博文。 代码味道
谢谢。
I am passing 20+ arguments of different types to method. There should be some clean way to pass this. Can you please help me.
I can pass array of object having all these 20+ arguments but in target method I have to put checks on type. Is this good way to pass long list of arguments.
Code: Sample from code not full
private DataTable CreateDataTable(string[] args)
{
DataTable dt = new DataTable();
dt.Clear();
foreach (var arg in args)
{
dt.Columns.Add(arg);
}
return dt;
}
I can pass array in this method because all arguments are of same type.
DataTable dt = CreateDataTable(new string[] { "ProjectId",
"ParentProjectId",
"ProjectName",
"CreationDate");
Now here I have more than 20+ values of diff types like following
int projectId = 100;
int? parentProjectId = SomeCondition ? 10 : null;
string projectName = "Good Project"
DateTime createdDate = DateTime.Now;
.
.
.
In this method I would assign values to columns.
AssignValuesToDataTable(dt, Arguments list ......)
// Implementation would be like this. Here I am passing 20+ arguments.
private DataTable AssignValuesToDataTable(DataTable dt, arguments list ........)
{
DataRow row = dt.NewRow();
row["ProjectId"] = projectId;
.
.
.
dt.Rows.Add(row);
}
Can you please help. I am using C#4
EDIT:
Above code is an example from my real code but I am more interesting to know that what is best method to achieve this.
From Coding Horror (Jeff Atwood)
The more parameters a method has, the more complex it is. Limit the
number of parameters you need in a given method, or use an object to
combine the parameters.
Above quote is from this blog post.
Code Smells
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
传递一个代表数据的对象:
当然
,那么问题就变成了:为什么要使用
DataTable
根本?因为Project
类是表达该数据的更好的隐喻/机制,并且List
(或BindingList
)是非常适合此类的集合。(提示:我非常非常非常很少使用
DataTable
- 或者可能比这更少)Pass an object that represents the data instead:
with
of course, then the question becomes : why use
DataTable
at all? since aProject
class is a much better metaphor / mechanism for expressing that data, andList<Project>
(orBindingList<Project>
) is ideal for a collection of such.(hint: I very, very, very rarely use
DataTable
- or maybe less frequently than that)为什么不构造一个对象(所有公共属性)来表示您的数据并在其 c-tor 中分配值,然后将其传递给您的方法?
使用数据表的任何原因?
Why not constructing an object (all Public properties) to represent your data and assign values in its c-tor, then pass it to your method?
Any reason of using a datatable?
您可以创建一个类,将参数添加为属性,然后将此类的实例传递给您的方法
You can create a class, add the parameters as properties and then pass an instance of this class to your method
将您的方法更改为:
您提前创建新的
DataRow
并直接从源为其分配值。然后将新的DataRow
传递给要插入的方法。显然,您可能希望在添加之前在方法中进行一些检查,以确保新行匹配所有约束和条件。What about changing your method to:
You create the new
DataRow
ahead and asign the values to it directly from the source. Then pass along the newDataRow
to your method to be inserted. Obviously you might want to do some checks within your method to make sure the new Row matches all constraints and conditions, before adding it.