Scope of objects and best practices

R

Rob

Rather new to vb.net...

I am wondering if the following is possible and sounds reasonable...

Can connection objects and datasets be defined once (at program load) and be
available for the life of the program ? Posssible ? Bad idea ? Over
simplistic ?

Regarding Connections...
I am not saying that the conncetion should be left open, just defined... in
other words, have it such that...

When Program loads...
Public strConn as String = "define the cnn here"
Public cnn as New SqlConnection(strConn )

When I want to use the connection...
cnn.Open()
' Do whatever
cnn.Close()

Does Auto garbage collection destroy cnn periodically ?


Regarding Datasets....
Also, may I establish a dataset in the same manner ?

Let's say I have 4 forms that use the same data set in the sasme program....
may I just establish it once, fill it, then use the data from that "fill" on
several forms ? Maybe have a button that would "re-fill" it upon a user's
request ? Would user changes to data within the dataset synch across all
the forms as well since it is the same data set (object) ?

Thanks
 
G

Guest

You can use global variables defined in a module as "Public" or in a class
using shared properties.

As far as being a bad idea, it doesn't all encapsulation of classes that use
these variables but if you're a single programmer and not part of a
programming team then it's up to you how you organize your program so long as
you realize that updates to it a year from now are easier with encapsulation.

One way that I do it is to set up a DataBase Class which does all accessing
of the data base with items like datasets as shared properties.
 
M

m.posseth

Can connection objects and datasets be defined once (at program load) and
be
available for the life of the program ? Posssible ? Bad idea ? Over
simplistic ?

well we are getting now on thin ice .... :)

As a true OOP aproach is a challenge response modell i.ow. i throw
something in and expect something out everyhting that was used in the middle
to come to this result will be set and destroyed inmediatly after it has
done its job . ( verry simplistic said )

However if you are a programmer who deals with a lot of data that is verry
costly to produce ( in terms that the querys are verry expensive ) you are
probably better of using a Singleton aproach
in wich you can even create a data caching mechanism .


so to give you a straight answer .... i believe that the best aproach is ..
to use as few resources as possible with a maximum on performance for your
app


regards

Michel Posseth [MCP]
 
D

david

Rather new to vb.net...

I am wondering if the following is possible and sounds reasonable...

Can connection objects and datasets be defined once (at program load) and be
available for the life of the program ? Posssible ? Bad idea ? Over
simplistic ?

Regarding connections, best practice generally dictates that you create
the connection when you want it and dispose it as soon as possible.
Because of connection pooling there's very little cost to this, and it
keeps the code much, much cleaner and more flexible.

Datasets are a bit more complex, and IMHO the structure of you app
should determine this. If your various forms really are multiple
views on the same dataset, then it's probably better for all forms
to share that dataset, rather than having complex sync operations to
ensure that all four datasets contain the same data or constantly
refilling the dataset just in case another form has changed the
underlying data.

Personally, I'd still shy away from a simple dataset global variable
though. Depending on the app I'd either have some kind of Singleton
class controlling access to the dataset or have the main form "own" the
dataset and pass it to the other forms.

David
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top