Home All Groups Group Topic Archive Search About

Data Access Layer design question

Author
29 Mar 2006 5:06 PM
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.

Author
30 Mar 2006 3:22 AM
Matt Noonan
Can't you abstract the connection code to another routine, sort of the way
the Enterprise Library does it?


--
Matt Noonan
EasyObjects.NET: The O/RM for the Enterprise Library
http://www.easyobjects.net


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.
>
Author
30 Mar 2006 10:31 AM
Vayse
"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
Author
31 Mar 2006 1:36 AM
Matt Noonan
Actually, I use the Enterprise Library and EasyObjects. But if this works
for you, it will probably benefit others as well.


--
Matt Noonan
EasyObjects.NET: The O/RM for the Enterprise Library
http://www.easyobjects.net


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
>

AddThis Social Bookmark Button