tags:

views:

281

answers:

3

I've recently moved from VB6 to VB.NET and I am finally getting there with understanding the inner workings. I've been looking at my company's existing codebase and I am a little suprised.

I understand that when VB.NET creates a string it see's if the string is in memory and if not creates a new instance of that string, otherwise it points the new variable to the old . However I wonder if it does this with other objects?

The reason I ask is my new company has a DATABASE object which basically wraps database connection info and database connections in a object.

We also have a BOOK object and a PAGES object.

My best practice in VB6 would be to create a DATABASE object and then pass (byRef) that to the BOOK and then PAGES object, so you have one DATABASE object passed to muliple books and then PAGES.

However, how they have done it is to create a new instance of the DATABASE object in each PAGE and each PAGE object, which means you could have muliple instances (thousands in fact) of objects which are in fact all the same.

Is this wise? Or do objects get handled like Strings?

+5  A: 

Your understanding of strings is only applicable to string constants - not strings which are created in any other way.

If the thousands of Database objects are all "the same" then it sounds like they should actually be passing a reference to the same object around. You need to understand the difference between a reference and an object.

Finally, nothing in your code should be holding onto connections, in all likelihood. It's almost always better to open the connection, do whatever you need to, and then close it again. The connection pooling system can make sure that the real connection to the database (which is relatively expensive to create) is reused.

Jon Skeet
@spacemonkeys: He is trying to politely tell you that you don't understand the concept at all. Follow his advice and read the article he links to and improve your understanding.
Geoffrey Chetwood
@Jon sorry just re read your answer, and you confirmed what I thought, understand the difference between byVal and byRef, but in each page or book the dim line is "Dim _Database as new DATABASE" and then the DATABASE sets itself using the same config file settings .. like I said somebody elses code
spacemonkeys
Right - in that case, that's definitely a bad thing. Use a factory which can cache the object (and return more references to it) or something similar. Aside from anything else, if it's genuinely parsing the config file settings each time that's pretty dumb.
Jon Skeet
I always thought config file settings were cached in .net. Is this not the case?
Sam Meldrum
@Sam: Standard config files (like app.config) are cached. In this case, I believe the implication is that DATABASE is parsing a homebrew config that was built in the VB6 app. Jon's right, it looks like it's reparsing and recreating every time, which is a bad way to go.
Greg D
+2  A: 

Strings are handled with a special case as other objects. While other objects are stuck on the heap (more detail here) They are interned (description is in the remarks section, http://msdn.microsoft.com/en-us/library/system.string.isinterned(VS.80).aspx.

Database connections are best handled by creating the connection as late as possible and closing it as soon as possible. While it may be ok to pass around connection info, you should not be passing around an actual connection.

StingyJack
Note that strings aren't automatically handled like that - only string constants are interned normally, unless you intern the string explicitly. It's not like every string you ever create is interned.
Jon Skeet
Agree but that doesn't actually answer the question, each PAGE or BOOK object contains a NEW DATABASE object, the handling of connections within the object is not of interest in this question, replace DATABASE with any other object name but keep in mind that the objects will be duplicate resource
spacemonkeys
+1  A: 

First of: a string is an object that is handled as a value type (because this is the behaviour you would expect) which means that whenever you do an assignment/operation (like concatenation, replace, ...), the value is copied into a new instance of the string class. So for instance: string t = "hallo"; t = t + " you"; contains 3 different string instances containing: "hallo", " you" and "hallo you";

Regarding the database object... look at ORM mappers (I personally use subsonic) Most of the time they use the following seperation: - a database object (like you have) managing the connection to the database - a controller or adapter that contains the logic for loading/retrieving, updating, deleting (SCUM-operations) for each object using the database object. - an object which contains the data (for instance a book object)

so now you have 1 database object (which can contain multiple connections but that's not often done...) a controller class which you use to create data (or domain objects, depending on how you define these), update or remove data. and many instances of the different data (/domain) objects you use...

Cohen