如何将长参数列表传递给方法或实现此目的的任何其他最佳方法

发布于 2024-11-25 12:23:24 字数 1532 浏览 0 评论 0原文

我将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

来世叙缘 2024-12-02 12:23:24

传递一个代表数据的对象:

private DataTable AssignValuesToDataTable(DateTable dt, Project project)
{
     row["ProjectId"] = project.Id;
     row["ProjectName"] = project.Name;
     ...
}

当然

public class Project
{
    public int Id {get;set;}
    public string Name {get;set;}
    ...
}

,那么问题就变成了:为什么要使用DataTable根本?因为 Project 类是表达该数据的更好的隐喻/机制,并且 List (或 BindingList)是非常适合此类的集合。

(提示:我非常非常非常很少使用DataTable - 或者可能比这更少)

Pass an object that represents the data instead:

private DataTable AssignValuesToDataTable(DateTable dt, Project project)
{
     row["ProjectId"] = project.Id;
     row["ProjectName"] = project.Name;
     ...
}

with

public class Project
{
    public int Id {get;set;}
    public string Name {get;set;}
    ...
}

of course, then the question becomes : why use DataTable at all? since a Project class is a much better metaphor / mechanism for expressing that data, and List<Project> (or BindingList<Project>) is ideal for a collection of such.

(hint: I very, very, very rarely use DataTable - or maybe less frequently than that)

逆光下的微笑 2024-12-02 12:23:24

为什么不构造一个对象(所有公共属性)来表示您的数据并在其 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?

伊面 2024-12-02 12:23:24

您可以创建一个类,将参数添加为属性,然后将此类的实例传递给您的方法

class Project
{
    public int projectId {get; set;}
    public int parentProjectId {get; set;}
    public string projectName {get; set;}
}

分配值到数据表(项目p)

You can create a class, add the parameters as properties and then pass an instance of this class to your method

class Project
{
    public int projectId {get; set;}
    public int parentProjectId {get; set;}
    public string projectName {get; set;}
}

AssignValuesToDataTable(Project p)

弃爱 2024-12-02 12:23:24

将您的方法更改为:

private DataTable AssignValuesToDataTable(DataTable dt, DataRow row)
{
 dt.Rows.Add(row);
}

您提前创建新的 DataRow 并直接从源为其分配值。然后将新的 DataRow 传递给要插入的方法。显然,您可能希望在添加之前在方法中进行一些检查,以确保新行匹配所有约束和条件。

What about changing your method to:

private DataTable AssignValuesToDataTable(DataTable dt, DataRow row)
{
 dt.Rows.Add(row);
}

You create the new DataRow ahead and asign the values to it directly from the source. Then pass along the new DataRow 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文