Data Access Layer design question

  • Thread starter Thread starter Guy Noir
  • Start date Start date
G

Guy Noir

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.
 
Can't you abstract the connection code to another routine, sort of the way
the Enterprise Library does it?
 
Guy Noir said:
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
 
Actually, I use the Enterprise Library and EasyObjects. But if this works
for you, it will probably benefit others as well.
 
Back
Top