473,399 Members | 3,656 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,399 software developers and data experts.

DB connections inside constructors

Can i create Command,Connection objects once inside a class
constructor and use them again and again by changing their values and
destroy them instead of creating objects everytime in a class member
function.......

Feb 20 '07 #1
8 1481
On Feb 20, 2:15 pm, "weird0" <amiredi...@gmail.comwrote:
Can i create Command,Connection objects once inside a class
constructor and use them again and again by changing their values and
destroy them instead of creating objects everytime in a class member
function.......
Hi,

Sure, but that's going to make your code harder to read and maintain.
There would also be an increased risk of bugs making their way into a
release. Ask youself whether or not those objects logically represent
the state of the class.

Brian

Feb 20 '07 #2
Hi,

"weird0" <am********@gmail.comwrote in message
news:11**********************@p10g2000cwp.googlegr oups.com...
Can i create Command,Connection objects once inside a class
constructor and use them again and again by changing their values and
destroy them instead of creating objects everytime in a class member
function.......
Yep, a constructor is just the place for that.
Now, I advice you again doing what you plan, just create a new instance of
a command.

In case of a connection you should ALWAYS close the connection ASAP, in my
code I open and close the connection in the same method.

Maybe it's a good idea to look in the archives by "data access layer" you
will find good comments about how to handle connections/commands objects/
--
Ignacio Machin
machin AT laceupsolutions com
Feb 20 '07 #3
Best practices coding takes avantage of the ADO.NET connection pool, which is
usually a lot better at handling connections than any code you or I could
write.
The pattern is as follows:
1) Use the same connection string
2) Open the connection immediately before you use it.
3) Close the connection (and call Dispose on the Command) immediately after
using it.

This allows your connection objects to return to the ADO.NET connection pool
automatically, and they can be resurrected quickly and efficiently whenever
you need them again.

Peter

--
Site: http://www.eggheadcafe.com
UnBlog: http://petesbloggerama.blogspot.com
Short urls & more: http://ittyurl.net


"weird0" wrote:
Can i create Command,Connection objects once inside a class
constructor and use them again and again by changing their values and
destroy them instead of creating objects everytime in a class member
function.......

Feb 20 '07 #4
And constructors are not a good place for that type of code. Constructors
should be used to create the object and as little other work as is possible.

Dale
--
Dale Preston
MCAD C#
MCSE, MCDBA
"weird0" wrote:
Can i create Command,Connection objects once inside a class
constructor and use them again and again by changing their values and
destroy them instead of creating objects everytime in a class member
function.......

Feb 20 '07 #5
I throw my hat in with Dale on this one. Sometimes you really do need
to do some long-running operation in a constructor, but you should
avoid it if you can.

If there is some object that you want to use over and over in your
class, and it takes a long time to build, you might think about using
lazy construction: initialize it to null, and then the first time you
need it, construct it.

On Feb 20, 2:56 pm, Dale <dale0...@nospam.nospamwrote:
And constructors are not a good place for that type of code. Constructors
should be used to create the object and as little other work as is possible.

"weird0" wrote:
Can i create Command,Connection objects once inside a class
constructor and use them again and again by changing their values and
destroy them instead of creating objects everytime in a class member
function.......
Feb 21 '07 #6
HI,

"Dale" <da******@nospam.nospamwrote in message
news:CE**********************************@microsof t.com...
And constructors are not a good place for that type of code. Constructors
should be used to create the object and as little other work as is
possible.
Yes, but if the info to create the instance is in a DB you may need to get
it from there :)
Feb 21 '07 #7
On Feb 20, 11:21 pm, "Bruce Wood" <brucew...@canada.comwrote:
I throw my hat in with Dale on this one. Sometimes you really do need
to do some long-running operation in a constructor, but you should
avoid it if you can.

If there is some object that you want to use over and over in your
class, and it takes a long time to build, you might think about using
lazy construction: initialize it to null, and then the first time you
need it, construct it.
Bruce,

I concur. If you're just initializing IDbConnection or IDbCommand
objects then fine. But, once you call Open or ExecuteReader or
whatever it immediately goes from minimal work to a significant time
consuming operation. That's work that may not be obvious to the
developer since the constructor doesn't have a descriptive name. If
the semantics of the class require a signficant operation for
instantiation then create an adequately named static factory method
that clearly describes what the operation might do.

I've brought up the example of the StreamReader before as something
that does not adhere to the framework design guidelines. It's
constructor will actually open a file, but no where in the
documentation is that made explicitly clear. To be fair, it can be
implied from looking at the exceptions it throws, examples, or by
noticing that the class doesn't have an Open method, but wouldn't it
have been better to just create a static StreamReader.Open method
which clearly indicates that a potentially significant operation is
happening?

Brian

Feb 21 '07 #8
In "Framework Design Guidelines" (Abrams, Cwalina et. al.) they agonize over
the fact that some very early "1.0" design decisions were vritually
impossible to fix in later versions because the amount of breakage would have
been unacceptable. I suspect your example is one of them.
Peter

--
Site: http://www.eggheadcafe.com
UnBlog: http://petesbloggerama.blogspot.com
Short urls & more: http://ittyurl.net


"Brian Gideon" wrote:
On Feb 20, 11:21 pm, "Bruce Wood" <brucew...@canada.comwrote:
I throw my hat in with Dale on this one. Sometimes you really do need
to do some long-running operation in a constructor, but you should
avoid it if you can.

If there is some object that you want to use over and over in your
class, and it takes a long time to build, you might think about using
lazy construction: initialize it to null, and then the first time you
need it, construct it.

Bruce,

I concur. If you're just initializing IDbConnection or IDbCommand
objects then fine. But, once you call Open or ExecuteReader or
whatever it immediately goes from minimal work to a significant time
consuming operation. That's work that may not be obvious to the
developer since the constructor doesn't have a descriptive name. If
the semantics of the class require a signficant operation for
instantiation then create an adequately named static factory method
that clearly describes what the operation might do.

I've brought up the example of the StreamReader before as something
that does not adhere to the framework design guidelines. It's
constructor will actually open a file, but no where in the
documentation is that made explicitly clear. To be fair, it can be
implied from looking at the exceptions it throws, examples, or by
noticing that the class doesn't have an Open method, but wouldn't it
have been better to just create a static StreamReader.Open method
which clearly indicates that a potentially significant operation is
happening?

Brian

Feb 24 '07 #9

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

Similar topics

14
by: Roger Binns | last post by:
It appears that xmlrpclib and/or SimpleXMLRPCServer always use new connections for each request. I have been trying to make them reuse the existing connection but can't find where. Is there any...
28
by: Daniel | last post by:
Hello =) I have an object which contains a method that should execute every x ms. I can use setInterval inside the object construct like this - self.setInterval('ObjectName.methodName()',...
42
by: Edward Diener | last post by:
Coming from the C++ world I can not understand the reason why copy constructors are not used in the .NET framework. A copy constructor creates an object from a copy of another object of the same...
8
by: Maddman | last post by:
Newbie here. I've got my database set up in SQL 2000, and have started an Access adp for a front end. I have 10 licenses, and at the moment the only accesses are the server through Remote...
5
by: news | last post by:
Well, I wrote my first PHP class today. Yeah! But to get it to work, in each function within the class I have to repeat the database connection lines, and that just seems redundant; there has to...
17
by: Peter Proost | last post by:
Hi Group, I've got an interesting problem, I don't know if this is the right group but I think so because everything I've read about it so far says it's a .net problem. Here's the problem, we're...
4
by: Josh Close | last post by:
Is there a way to remove idle connections? My postgres server is getting serveral hundred idle connections. It's due to a postgres .NET provider not closing the connections properly. I don't want...
8
by: arun | last post by:
Hello Group, I have a class class Simple2 { publica: Simple2(); list<int> *l1; list<int> l2; };
8
by: weird0 | last post by:
The scenario is that i have two forms, Form1( main form) and Form2 and both of them opened. Form1 has called Form2 inside of its constructor. class Form1 { const int NodeLimit=30; Node...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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
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.