Link to home
Start Free TrialLog in
Avatar of Olukayode Oluwole
Olukayode OluwoleFlag for Canada

asked on

Can i make the No of fields in a GetObject Method any no of items

I have in my c# application a need to write into a postgresql database

I use a simple GetObject method shown below

[code]private void WriteRecord()
        {
            var p = GetObject(LocationIDValue.Text, LocDescriptionValue.Text, LoginDetails.staticcompany, Convert.ToInt32(idValue.Text), LoginDetails.staticinsertupdatedeletetag);
            NewDataAccess.WriteData("public.splocationcode_updateinsert", p);
        }[/code]

It takes fields value from the screen passes them to GetObject method to define
parameters before  using these to write in to my postgresql database

The GetObject Method is shown below

       []public static object GetObject(string parameter01, string parameter02, string parameter03, int parameter04, string parameter05)
        {
           
           
            return new
               {
                    locationidx = parameter01,
                    locdescriptionx = parameter02,
                    companycodex = parameter03,
                    idx = parameter04,
                    dowhatx = parameter05

               };
         
        }[/code]

The applications works fine for this Table with 5  fields

The problem i now have is if i have to address  writing to another table with fields more or less than 5 fields.
Do I have to provide a  GetObject method with the no of parameters equal to the no of fields in that Table.. If Yes will i have to do this for every table in my application ??

Is there a way to make the no of items in the GetObject method have any no of fields.

I was thinking of checking the GetObject Method into a library and then pass the no of fields as  string  and the parameters   as a Datatable

I just dont know if my thinking  is right or if there is another way to achieve the same goal of using the GetObject method to address  2 tables with different no of fields

I will be grateful for any suggestion.

Olukay




Avatar of ste5an
ste5an
Flag of Germany image

Just a comment:

The problem i now have is if i have to address  writing to another table with fields more or less than 5 fields.
This looks terribly dynamic. This is not the way to go with ,NET and a RDBMS as both systems are strongly typed. Someone mentioned Dapper, which I don't know. Maybe there it is the concept. But it really sounds wrong.

Last but not least: A custom method in a normal program can be never called GetObject. Get is reserved for explicit getter methods. And object is an internal name. What is the purpose of it`? This should be part of the name.
Avatar of Olukayode Oluwole

ASKER

Thanks for your comment. I really needed an expert to help review and ventilate my thoughts. ..  I actually use Dapper in this project but I am not sure if that will affect my solution.

What is the Purpose ??
In  my C#  project each time i have to write , read or delete from the database I have a statement like
GlobalConfig.Connection.GetTaxGroupRates();
(There each statement above goes to execute a method )

For a project with about  100 Tables that means having equivalent of these statement written about 300 times (ie read, write and delete  x No of Table)

I then came across the method below that allow me read from my postgresql database without using GlobalCofig......

[]public static List<T> ReadData<T>(string sql, object p)
        {
           
            var output = new List<T>();        // If you want to return an empty list instead, use this
           
                using (var conn = new NpgsqlConnection(LoginDetails.staticconnectionstring))
                {
                     output = conn.Query<T>(sql, p, commandType: CommandType.StoredProcedure).ToList();
                   
                }
 
            return output;
        }[/code]

That mean if I am able to specify my stored procedure (called sql)  and the parameters  ( called  p) i can just within my user interface do my read with the method above.

So the purpose was to reduce avoid repeating

Globalconfig.........

Is there any other way i can achieve my Goal

Thanks

Olukay



ASKER CERTIFIED SOLUTION
Avatar of ste5an
ste5an
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks for pointing me in the right direction

I will try to read up using Dapper to specify the parameters

If I do not succeed I will  request  a Dapper specialist  to help

look at my syntax issues.

Once again Thanks