|
dev
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Data Access Layer design questionlayer for command CRUD tasks using stored procedures. My question is more of a design point of view. In each of my methods Create, Read, Update, and Delete, it seems I am repeating a lot of code such as: <Code> // Create the connection and Open it SqlConnection conn = new SqlConnection(connectionString); conn.Open(); cmd = new SqlCommand(storedProcedure, conn); // Set the type of command to a stored procedure cmd.CommandType = CommandType.StoredProcedure; // Add the SP parameters to the command cmd.Parameters.AddRange(parameters); </Code> What's the opinion on this? Should I create a connection in a method and pass it back or just repeat this chunk of code in each method? I read some articles that mentioned that when passing connection references around one needs to be careful to close connections, etc. Can't you abstract the connection code to another routine, sort of the way
the Enterprise Library does it? Show quote "Guy Noir" <ahack***@gmail.com> wrote in message news:1143652003.054808.42730@i40g2000cwc.googlegroups.com... > Hi. I have a quick question. I am trying to create a quick data access > layer for command CRUD tasks using stored procedures. > > My question is more of a design point of view. > > In each of my methods Create, Read, Update, and Delete, it seems I am > repeating a lot of code such as: > > <Code> > // Create the connection and Open it > SqlConnection conn = new SqlConnection(connectionString); > conn.Open(); > > cmd = new SqlCommand(storedProcedure, conn); > > // Set the type of command to a stored procedure > cmd.CommandType = CommandType.StoredProcedure; > > // Add the SP parameters to the command > cmd.Parameters.AddRange(parameters); > </Code> > > What's the opinion on this? Should I create a connection in a method > and pass it back or just repeat this chunk of code in each method? I > read some articles that mentioned that when passing connection > references around one needs to be careful to close connections, etc. > "Guy Noir" <ahack***@gmail.com> wrote in message I had a similiar question - see the thread ADO Update Code Re usenews:1143652003.054808.42730@i40g2000cwc.googlegroups.com... > Hi. I have a quick question. I am trying to create a quick data access > layer for command CRUD tasks using stored procedures. > > My question is more of a design point of view. > > In each of my methods Create, Read, Update, and Delete, it seems I am > repeating a lot of code such as: > Anyway, I've put the OleDB code at http://h1.ripway.com/vayse/DataClass/ It might be of some use to you. It has Create, Update and Delete methods. Regards Vayse Actually, I use the Enterprise Library and EasyObjects. But if this works
for you, it will probably benefit others as well. Show quote "Vayse" <vayse@nospam.nospam> wrote in message news:uFTLnU%23UGHA.4864@TK2MSFTNGP12.phx.gbl... > "Guy Noir" <ahack***@gmail.com> wrote in message > news:1143652003.054808.42730@i40g2000cwc.googlegroups.com... >> Hi. I have a quick question. I am trying to create a quick data access >> layer for command CRUD tasks using stored procedures. >> >> My question is more of a design point of view. >> >> In each of my methods Create, Read, Update, and Delete, it seems I am >> repeating a lot of code such as: >> > > I had a similiar question - see the thread ADO Update Code Re use > Anyway, I've put the OleDB code at > http://h1.ripway.com/vayse/DataClass/ > > It might be of some use to you. It has Create, Update and Delete methods. > Regards > Vayse > |
|||||||||||||||||||||||