How does object reuse work in .NET?

I recently switched from VB6 to VB.NET and I finally got there with an understanding of the inner workings. I was looking at my company's existing codebase and I am a little surprised.

My understanding is that when VB.NET creates a string, it sees if the string is in memory and if it doesn't create a new instance of that string, otherwise it points the new variable to the old one. However, I am wondering if this does it with other objects?

The reason I am asking is my new company has a DATABASE object that basically wraps the database connection and database connection information in an 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 BOOK object and then PAGES, so you have one DATABASE object that is passed to mark up the books and then PAGES.

However, the way they did it was to create a new instance of the DATABASE object on every PAGE and every PAGE, which means you could have multiple instances (actually thousands) of objects that are actually all the same.

Is it wise? Or are objects treated like strings?

0


source to share


3 answers


Your understanding of strings only applies to string constants - not strings that are created in any other way.

If thousands of database objects are all "the same", then it looks like they should actually pass a reference to the same object. You need to understand the difference between link and object .



Finally, nothing in your code should be about connections, in all likelihood. It's almost always best to open the connection, do whatever you need to do, and then close it again. A pooling system can ensure that the actual database connection (which is relatively expensive to create) is reused.

+5


source


Strings are treated on a special case like other objects. While other objects are stuck on the heap (more details here ) They are interned (description is in the notes 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 nice to skip the connection information, you shouldn't be able to bypass the actual connection.

+2


source


First: a string is an object that is treated as a value type (because this is the behavior you 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 example: the line t = "hallo"; t = t + "you"; contains 3 different string instances containing: "hallo", "you" and "hallo you";

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

So now you have 1 database object (which can contain multiple connections, but this is not often done ...) the controller class that you use to create data (or domain objects, depending on how you define them) , update or delete data. and many instances of various data objects (/ domain) you are using ...

+1


source







All Articles