The below is a typical, for me, Update
method in L2S. I am still fairly new to a lot of this(L2S & business app development) but this just FEELs wrong. Like there MUST be a smarter way of doing this. Unfortunately, I am having trouble visualizing it and am hoping someone can provide an example or point me in the right direction.
To take a stab in the dark, would I have a Person Object
that has all these fields as Properties? Then what, though?
Is that redundant since L2S already mapped my Person Table to a Class?
Is this just 'how it goes', that you eventually end up passing 30 parameters(or MORE) to an UPDATE
statement at some point?
For reference, this is a business app using C#, WinForms, .Net 3.5, and L2S over SQL 2005 Standard.
Here is a typical Update Call for me. This is in a file(BLLConnect.cs) with other CRUD methods. Connect is the name of the DB that holds tblPerson
When a user clicks save()
this is what is eventually called with all of these fields having, potentially, been updated-->
public static void UpdatePerson(int personID, string userID, string titleID, string firstName, string middleName, string lastName, string suffixID,
string ssn, char gender, DateTime? birthDate, DateTime? deathDate, string driversLicenseNumber,
string driversLicenseStateID, string primaryRaceID, string secondaryRaceID, bool hispanicOrigin,
bool citizenFlag, bool veteranFlag, short ? residencyCountyID, short? responsibilityCountyID, string emailAddress,
string maritalStatusID)
{
using (var context = ConnectDataContext.Create())
{
var personToUpdate =
(from person in context.tblPersons
where person.PersonID == personID
select person).Single();
personToUpdate.TitleID = titleID;
personToUpdate.FirstName = firstName;
personToUpdate.MiddleName = middleName;
personToUpdate.LastName = lastName;
personToUpdate.SuffixID = suffixID;
personToUpdate.SSN = ssn;
personToUpdate.Gender = gender;
personToUpdate.BirthDate = birthDate;
personToUpdate.DeathDate = deathDate;
personToUpdate.DriversLicenseNumber = driversLicenseNumber;
personToUpdate.DriversLicenseStateID = driversLicenseStateID;
personToUpdate.PrimaryRaceID = primaryRaceID;
personToUpdate.SecondaryRaceID = secondaryRaceID;
personToUpdate.HispanicOriginFlag = hispanicOrigin;
personToUpdate.CitizenFlag = citizenFlag;
personToUpdate.VeteranFlag = veteranFlag;
personToUpdate.ResidencyCountyID = residencyCountyID;
personToUpdate.ResponsibilityCountyID = responsibilityCountyID;
personToUpdate.EmailAddress = emailAddress;
personToUpdate.MaritalStatusID = maritalStatusID;
personToUpdate.UpdateUserID = userID;
personToUpdate.UpdateDateTime = DateTime.Now;
context.SubmitChanges();
}
}
So, if I am understanding things correctly I would have a datacontext
reference in my winform UI that I would use when I populate the fields and ALSO for UPDATES, INSERTS, and DELETES. Correct?