Pass a lot of parameters?

  • Thread starter Thread starter nick
  • Start date Start date
N

nick

I have a function to update a database table with 40 columns. currently I
created the function with 40 parameters which is very ugly, any best
practice to handle this situation?
 
Nick,

I would recommend creating a structure which exposes public fields which
you can set to the values of each of the columns. The fields would be named
after the columns, of course. Then, you just pass that in.

Hope this helps.
 
nick said:
I have a function to update a database table with 40 columns. currently I
created the function with 40 parameters which is very ugly, any best
practice to handle this situation?

Create a type which encapsulates a row of the database, then pass an
instance of that type (or more likely, a reference to an instance of
that type).
 
To go one step further even, when it's this many fields at a time, you may
want to make a class, the represents one row of your data, and have a public
property for each field. The difference between this and a struct is that
you can do additional handling (like checking for field lengths and ensuring
bounds, etc) on each of them.. Because when you have this much data to
update, just doing this field validation can be a handful, so it might be
easier to just save all of that, for this class. for example:

public string LastName
{
set
{
if ( value.Length > 40 )
throw new Exception("LastName must be less than 40 characters");
m_LastName = value;
}
}

HTH

Nicholas Paldino said:
Nick,

I would recommend creating a structure which exposes public fields
which you can set to the values of each of the columns. The fields would
be named after the columns, of course. Then, you just pass that in.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

nick said:
I have a function to update a database table with 40 columns. currently I
created the function with 40 parameters which is very ugly, any best
practice to handle this situation?
 
Hi,

The cleaner solution is to use a struct. Others solutions may be using an
ArrayList .

Are you keeping those 40 columns in independent variables in your program?
Probably you have them grouped under either a class, struct or even a
DAtaset, you could just that in this function..


cheers,
 
Thanks, in a multiple tier system, in which layer should the type be
defined? or a common module?
 
Sounds good to add additional check.

However, if I've defined a class with 40 columns and validations code, how
to handle the situation that I need only a couple of the columns (say, the
Primary Key columns) in some other functions? handle the validation
separately? or define a class for each column (sounds too much)?

RCS said:
To go one step further even, when it's this many fields at a time, you may
want to make a class, the represents one row of your data, and have a
public property for each field. The difference between this and a struct
is that you can do additional handling (like checking for field lengths
and ensuring bounds, etc) on each of them.. Because when you have this
much data to update, just doing this field validation can be a handful, so
it might be easier to just save all of that, for this class. for example:

public string LastName
{
set
{
if ( value.Length > 40 )
throw new Exception("LastName must be less than 40
characters");
m_LastName = value;
}
}

HTH

Nicholas Paldino said:
Nick,

I would recommend creating a structure which exposes public fields
which you can set to the values of each of the columns. The fields would
be named after the columns, of course. Then, you just pass that in.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

nick said:
I have a function to update a database table with 40 columns. currently I
created the function with 40 parameters which is very ugly, any best
practice to handle this situation?
 
nick said:
Thanks, in a multiple tier system, in which layer should the type be
defined? or a common module?

I wouldn't like to say for sure - it depends on the rest of your
system. The question to ask yourself is "Does it represent a valid
business object, or is it database-specific?"
 
I assume you mean like if you are JUST updating an email address for
example, and you just need a PK and the email address??

I'd say still use this option, but just use the pieces that you need:

UserRow objUR = new UserRow();
objUR.PrimaryKey = 143;
objUR.EmailAddress = (e-mail address removed);

UpdateEmail(objUR);

and inside UpdateEmail, it will just pick off what it needs, those two
fields. That way you could still use the same logic.. I think that's
reasonable? Off the top of my head, that doesn't strike me as horrible and I
think any badness from doing it that way, is countered by the fact that you
can have all of your field logic in one place and you are reusing it (for
inserts, updates and field-specific updates)..

For what it's worth.. HTH


nick said:
Sounds good to add additional check.

However, if I've defined a class with 40 columns and validations code, how
to handle the situation that I need only a couple of the columns (say, the
Primary Key columns) in some other functions? handle the validation
separately? or define a class for each column (sounds too much)?

RCS said:
To go one step further even, when it's this many fields at a time, you
may want to make a class, the represents one row of your data, and have a
public property for each field. The difference between this and a struct
is that you can do additional handling (like checking for field lengths
and ensuring bounds, etc) on each of them.. Because when you have this
much data to update, just doing this field validation can be a handful,
so it might be easier to just save all of that, for this class. for
example:

public string LastName
{
set
{
if ( value.Length > 40 )
throw new Exception("LastName must be less than 40
characters");
m_LastName = value;
}
}

HTH

Nicholas Paldino said:
Nick,

I would recommend creating a structure which exposes public fields
which you can set to the values of each of the columns. The fields
would be named after the columns, of course. Then, you just pass that
in.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

I have a function to update a database table with 40 columns. currently
I created the function with 40 parameters which is very ugly, any best
practice to handle this situation?
 
Back
Top