473,397 Members | 2,084 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

Best Practices for implementing db conn's from asp.net business ob

I am in a slight predicament trying to determine the most efficient and
effective way to connect/disconnect from a database within a business object
(c# dll). I'm also keeping in mind the concept of connecting late and
disconnecting early.

Background:
- multi-tier application (code-behind uses properties and methods of the
business object, the business object handles the data layer)

For instance (in an ASPX code-behind file):

MyClass MyObject = new MyClass();
MyObject.Property1 = x;
MyObject.DoSomething();

When should the MyClass class connect and disconnect from the database?
Right now I am looking at 3 avenues of approach:
1. Connect in the constructor, then disconnect in Dispose()
2. Connect and Disconnect from within DoSomething()
3. Implement additional methods called Connect() and Disconnect()

I like number 2 except that, what happens when I get into a looping
situation where I repetitively call DoSomething(). Number 1 is good, except
I have to explicitly call Dispose() and of course implement IDisposable. I
really hate number 3 because it seem like unnecessary steps performed by the
consumer of the business object.

I'm looking for answers in the realms of what the best answer is from a
solution design perspective... keeping in mind: performance, scalability,
maintainability, and all that other fun stuff.
Dec 20 '05 #1
6 1610
zq
Hi, Nate

First method could be problematic because of the database's per seat
licensing (if any) - if you occupy too many connections at once, next
"unlicensed" connection will be waiting inside an endless loop until a
license gets available.

Second method would be problematic if you call DoSomething too often in a
short period of time - if you have a for or a while loop and inside you call
DoSomething you'll have a big overhead of connecting-disconnecting for no
reason.

Third method gives you the most control.
"Nate" <Na**@discussions.microsoft.com> wrote in message
news:3B**********************************@microsof t.com...
I am in a slight predicament trying to determine the most efficient and
effective way to connect/disconnect from a database within a business
object
(c# dll). I'm also keeping in mind the concept of connecting late and
disconnecting early.

Background:
- multi-tier application (code-behind uses properties and methods of the
business object, the business object handles the data layer)

For instance (in an ASPX code-behind file):

MyClass MyObject = new MyClass();
MyObject.Property1 = x;
MyObject.DoSomething();

When should the MyClass class connect and disconnect from the database?
Right now I am looking at 3 avenues of approach:
1. Connect in the constructor, then disconnect in Dispose()
2. Connect and Disconnect from within DoSomething()
3. Implement additional methods called Connect() and Disconnect()

I like number 2 except that, what happens when I get into a looping
situation where I repetitively call DoSomething(). Number 1 is good,
except
I have to explicitly call Dispose() and of course implement IDisposable.
I
really hate number 3 because it seem like unnecessary steps performed by
the
consumer of the business object.

I'm looking for answers in the realms of what the best answer is from a
solution design perspective... keeping in mind: performance, scalability,
maintainability, and all that other fun stuff.

Dec 20 '05 #2
I'd actually go for number 2. The reason being that your probably using
connection pooling and as the connection is being created on the server
the connection string will be continually the same so the connection
you previously created will be reused. The creation of the connection
is the expensive operation. This then provides you with your scenario
where the connection is open for the minimum amount of time which is
good for scalability.

Dec 20 '05 #3
Hi Nate,
I would not explicitly worry about when to connect/disconnect from the DB
in your public interface of your object, that is really a secondary part
depending on the behaviour of your object. You are going to need to be able
to accomplish the following scenarios with your objects:

1. Create a new object (db connection may be required or not)
2. Fetch an existing object from the DB
3. Update an object (this may involve insertion / update as well as delete)

Scenario 1
For scenario 1 you probably won't need to connect to the database at all
(unless you need to fetch a unique id from the DB or something else) so you
can just say:
MyObject x = new MyObject();

there would be no database involvement at this point, there is no need to
open a connection to the DB, when you want to update the contents of the
object into the DB then you will open a connection.
Scenario 2
If you want to create an object that represents data in the DB then you will
connect to the DB fetch the data and put it in the object and disconnect,
again there is no need to keep a connection open in the object, opening and
closing connections is fast since they will be pooled anyway, only the first
open of a connection will take a performance hit. To fetch data into your
object you could say:

MyObject x = MyObject.Fetch(1234);

The static Fetch method knows how to connect to the db and populate an
instance of MyObject and return it to the user.
Scenario 3
Once you have updated your objects data then you will call an updte method
i.e.
MyObject x = new MyObject();
.... do some stuff
x.Update();

The update call will connect and then disconnect from the DB, calling the
appropriate insert,update or delete method depending if your object isDirty,
isNew or isDeleted. Your update method should also check to see if the
object isDirty, it would only save if the object isDirty in order to increase
performance, no point saving if you don't have to.
For your scenario 2 where you have the DoSomething method being called
multiple times and it is affecting the DB, I would say you want to avoid this
case, possibly by moving the logic you are using in the DB into the object so
that it is not necessary to update directly inside the method, hopefully your
business objects have enough knowledge that they can be used without having
to hit the db until an update time.

Hope that helps.
Mark Dawson
http://www.markdawson.org

"Nate" wrote:
I am in a slight predicament trying to determine the most efficient and
effective way to connect/disconnect from a database within a business object
(c# dll). I'm also keeping in mind the concept of connecting late and
disconnecting early.

Background:
- multi-tier application (code-behind uses properties and methods of the
business object, the business object handles the data layer)

For instance (in an ASPX code-behind file):

MyClass MyObject = new MyClass();
MyObject.Property1 = x;
MyObject.DoSomething();

When should the MyClass class connect and disconnect from the database?
Right now I am looking at 3 avenues of approach:
1. Connect in the constructor, then disconnect in Dispose()
2. Connect and Disconnect from within DoSomething()
3. Implement additional methods called Connect() and Disconnect()

I like number 2 except that, what happens when I get into a looping
situation where I repetitively call DoSomething(). Number 1 is good, except
I have to explicitly call Dispose() and of course implement IDisposable. I
really hate number 3 because it seem like unnecessary steps performed by the
consumer of the business object.

I'm looking for answers in the realms of what the best answer is from a
solution design perspective... keeping in mind: performance, scalability,
maintainability, and all that other fun stuff.

Dec 20 '05 #4
"Nate" <Na**@discussions.microsoft.com> wrote in message
news:3B**********************************@microsof t.com...
I am in a slight predicament trying to determine the most efficient and
effective way to connect/disconnect from a database within a business
object
(c# dll). I'm also keeping in mind the concept of connecting late and
disconnecting early.

Background:
- multi-tier application (code-behind uses properties and methods of the
business object, the business object handles the data layer)

For instance (in an ASPX code-behind file):

MyClass MyObject = new MyClass();
MyObject.Property1 = x;
MyObject.DoSomething();

When should the MyClass class connect and disconnect from the database?
Right now I am looking at 3 avenues of approach:
1. Connect in the constructor, then disconnect in Dispose()
2. Connect and Disconnect from within DoSomething()
3. Implement additional methods called Connect() and Disconnect()

I like number 2 except that, what happens when I get into a looping
situation where I repetitively call DoSomething(). Number 1 is good,
except
I have to explicitly call Dispose() and of course implement IDisposable.
I
really hate number 3 because it seem like unnecessary steps performed by
the
consumer of the business object.

I'm looking for answers in the realms of what the best answer is from a
solution design perspective... keeping in mind: performance, scalability,
maintainability, and all that other fun stuff.


My 2 cent answer is: None of the above.

As OLEDB takes care of connection pooling for you you should not keep a
connection alive in code. Connect, run some sql and then disconnect.

I typically add a class called DataConnection with one single static method
..Create() that looks kinda like this:

public static SqlConnnection Create()
{
SqlConnection s = new
SqlConnection(ConfigurationSettings.AppSettings["ConnectionString"]);
s.open();
return s;
}

Then in my code that runs sql i fetch a connection with the using
statement...

using (SqlConnection conn = DataConnection.Create())
{
SqlCommand blaha...
MyDataSet fubar...
...
....
} //At this point the connection goes back to the pool and other objects
will be collected by the GC at a later time. .Dispose() is a great thing
indeed.

The using keyword is a great language feature in C#. Use it!

Happy Coding
- Michael S
Dec 20 '05 #5
Nate,
In an ASP.NET application, best practices database access policy is to open
the connection just before you are about to use it, and close the connection
immediately after you are finished. There are exceptions to this
best-practices methodology, but they are rare.
Connection pooling will take care of the rest, provided your app uses the
same connection string.
Peter

--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com


"Nate" wrote:
I am in a slight predicament trying to determine the most efficient and
effective way to connect/disconnect from a database within a business object
(c# dll). I'm also keeping in mind the concept of connecting late and
disconnecting early.

Background:
- multi-tier application (code-behind uses properties and methods of the
business object, the business object handles the data layer)

For instance (in an ASPX code-behind file):

MyClass MyObject = new MyClass();
MyObject.Property1 = x;
MyObject.DoSomething();

When should the MyClass class connect and disconnect from the database?
Right now I am looking at 3 avenues of approach:
1. Connect in the constructor, then disconnect in Dispose()
2. Connect and Disconnect from within DoSomething()
3. Implement additional methods called Connect() and Disconnect()

I like number 2 except that, what happens when I get into a looping
situation where I repetitively call DoSomething(). Number 1 is good, except
I have to explicitly call Dispose() and of course implement IDisposable. I
really hate number 3 because it seem like unnecessary steps performed by the
consumer of the business object.

I'm looking for answers in the realms of what the best answer is from a
solution design perspective... keeping in mind: performance, scalability,
maintainability, and all that other fun stuff.

Dec 20 '05 #6
Looks like the consensus was #2,... Thank you all for replying.

"Peter Bromberg [C# MVP]" wrote:
Nate,
In an ASP.NET application, best practices database access policy is to open
the connection just before you are about to use it, and close the connection
immediately after you are finished. There are exceptions to this
best-practices methodology, but they are rare.
Connection pooling will take care of the rest, provided your app uses the
same connection string.
Peter

--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com


"Nate" wrote:
I am in a slight predicament trying to determine the most efficient and
effective way to connect/disconnect from a database within a business object
(c# dll). I'm also keeping in mind the concept of connecting late and
disconnecting early.

Background:
- multi-tier application (code-behind uses properties and methods of the
business object, the business object handles the data layer)

For instance (in an ASPX code-behind file):

MyClass MyObject = new MyClass();
MyObject.Property1 = x;
MyObject.DoSomething();

When should the MyClass class connect and disconnect from the database?
Right now I am looking at 3 avenues of approach:
1. Connect in the constructor, then disconnect in Dispose()
2. Connect and Disconnect from within DoSomething()
3. Implement additional methods called Connect() and Disconnect()

I like number 2 except that, what happens when I get into a looping
situation where I repetitively call DoSomething(). Number 1 is good, except
I have to explicitly call Dispose() and of course implement IDisposable. I
really hate number 3 because it seem like unnecessary steps performed by the
consumer of the business object.

I'm looking for answers in the realms of what the best answer is from a
solution design perspective... keeping in mind: performance, scalability,
maintainability, and all that other fun stuff.

Dec 20 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: T.S.Negi | last post by:
Dear All, Please suggest some of the best practices for writing SQL server stored procedures? I'm writing a business function (stored procedure), which calls many-stored procedure one after...
136
by: Matt Kruse | last post by:
http://www.JavascriptToolbox.com/bestpractices/ I started writing this up as a guide for some people who were looking for general tips on how to do things the 'right way' with Javascript. Their...
1
by: ABB | last post by:
What are the best practices to create n-tier application in Windows Forms? Links to white papers/articles are welcome. We have a medium-size accounting and stock application that are planning to...
4
by: DeepDiver | last post by:
I am developing an inventory database in SQL Server. I realize there are many commercial (as well as some non-commercial) inventory offerings, but my client has specific requirements that would...
1
by: jimdandy | last post by:
Hi all, Am looking for some guidance/advice in terms of best practices for deploying a MS BI project. We have a relatively large BI system that we need to deploy between DEV/QA/UAT/PROD and...
3
by: absoft | last post by:
What are the best practices for passing UserId and Password stored in the Session state on the UI layer e.g. ASP.NET Web Forms to the Business Layer or the Data Layer.
13
by: vizcayno | last post by:
Hello: Need your help in the "correct" definition of the next function. If necessary, I would like to know about a web site or documentation that tells me about best practices in defining...
2
by: hardieca | last post by:
Hi, I'd like to know if anyone knows of any resources detailing the best practices of validating rules in the business tier and providing helpful error messages to users in the UI tier. All the...
0
by: joshfink | last post by:
Hey guys, I am writing an application where I want to follow the best practices on error handling. This is what I have: I created an enum for various issues that could happen within the...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.