troubleshooting Question

How can I avoid repeating the same parameters in 2 different methods

Avatar of Olukayode Oluwole
Olukayode OluwoleFlag for Canada asked on
C#* Parameters
10 Comments1 Solution24 ViewsLast Modified:
The 2 Methods below use exactly the same parameters
The first method-1 users the method to generate a list from database
records using a stored procedure

The second Method-2  uses the same parameters but this time to write or
delete a record

Method-1
 []public static List<LocationModel> GetUsers_Filtered(string locationid,string  locationdes,string thecompany,string dummyidx ,string  dowhat)
        {
            var p = new
            {
                locationidx = locationid,
                locdescriptionx = locationdes,
                companycodex = thecompany,
                idx = Convert.ToInt32(dummyidx),
                dowhatx = dowhat
            };
            return NewDataAccess.ReadData<LocationModel>("public.splocationcode_search", p);
        }[/code]

Method-2       
   []   public static void CreateLocation(string locationvalue, string locdescriptionvalue, string companycodevalue, int idvalue, string dowhatvalue)
        {
            // Note This parameter definition is good for inserts and updates and delete into/From the database
            var p = new
            {
                locationidx = locationvalue,
                locdescriptionx = locdescriptionvalue,
                companycodex = companycodevalue,
                idx = idvalue,
                dowhatx = dowhatvalue   // Can be UI DM or DP ie UpdateInsert, DeleteMarked or DeletePermanent
            };


            switch (LoginDetails.staticinsertupdatedeletetag)
            {
                case "IU":                                  // Update and Inserts Stored Procedure
                    NewDataAccess.WriteData("public.splocationcode_updateinsert", p);
                    break;
                case "DM":                                  //  Delete Marked
                    NewDataAccess.WriteData("public.splocationcode_delete", p);
                    break;
                default:
                    break;
            }

        }[/code]

The 2 methods work as required but i would if possible not like to
repeat the same set of parameters twice.

How can I refactor this code to ensure that the portion below is not repeated twice
 var p = new
            {
                locationidx = locationid,
                locdescriptionx = locationdes,
                companycodex = thecompany,
                idx = Convert.ToInt32(dummyidx),
                dowhatx = dowhat
            };


ASKER CERTIFIED SOLUTION
Dustin Saunders
Co-Founder and Chief Architect

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Top Expert 2016

The Distinguished Expert awards are presented to the top veteran and rookie experts to earn the most points in the top 50 topics.

Log in to continue reading
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform for $9.99/mo
View membership options
Unlock 1 Answer and 10 Comments.
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
The Value of Experts Exchange in My Daily IT Life

Experts Exchange (EE) has become my company's go-to resource to get answers. I've used EE to make decisions, solve problems and even save customers. OutagesIO has been a challenging project and... Keep reading >>

Mike

Owner of Outages.IO
Phoenix, Arizona, United States
Member Since 2016
Join a full scale community that combines the best parts of other tools into one platform.
Unlock 1 Answer and 10 Comments.
View membership options
“All of life is about relationships, and EE has made a virtual community a real community. It lifts everyone's boat.”
William Peck

Member since 2004