A better UPDATE method in LINQ to SQL

Posted by Refracted Paladin on Stack Overflow See other posts from Stack Overflow or by Refracted Paladin
Published on 2010-04-26T19:16:49Z Indexed on 2010/04/26 19:53 UTC
Read the original article Hit count: 258

Filed under:
|
|
|
|

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();
        }
    }

© Stack Overflow or respective owner

Related posts about c#

Related posts about .NET