How to pass long list of parameters to method Or any other best way to achieve this
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 arg开发者_如何学Cuments.
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.
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)
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
class Project
{
public int projectId {get; set;}
public int parentProjectId {get; set;}
public string projectName {get; set;}
}
AssignValuesToDataTable(Project p)
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.
精彩评论