473,383 Members | 1,863 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,383 software developers and data experts.

multiple references to only 1 object???

Hi,

I've implemented some type of cache, to be able to point with multiple
references to the same object. Although, this 1-object-stuff is broken if I
do a refresh from the DataBase.

For instance I declare 2 objects:
dim MyA as clsCompany
dim MyB as clsCompany

And now I ask them from my factory:
'MyA: gets me the company with ID 1
MyA = MyFactory.GetCompanyWithID(1, enumFromCacheOrDataBase)
'MyB: Same thing, but because it's alreaddy in the ache, it returns me the
same object
MyB = MyFactory.GetCompanyWithID(1, enumFromCacheOrDataBase)
So it I change now a property of MyA, the same property will be changed in
MyB, because they poitn to the same object.

But: If I now explicitly do a refresh from the database, and replace the
object in the cache with the new one, I get something totally different:
MyB = MyFactory.GetCompanyWithID(1, enumFromDatabase)
If I change now a property of MyA, it won't be changed in MyB: they are
linked to 2 different objects now! What can I do to have them always being
linked to the same object? If I refresh one, all the others must point to
the new object...
Any help our hitns will be really appreciated!

Thansk a lot in advance,

Pieter


Oct 12 '07 #1
3 1201
"Pieter" <pi****************@hotmail.comwrote in message
news:%2****************@TK2MSFTNGP05.phx.gbl...
Hi,

I've implemented some type of cache, to be able to point with multiple
references to the same object. Although, this 1-object-stuff is broken if
I
do a refresh from the DataBase.

For instance I declare 2 objects:
dim MyA as clsCompany
dim MyB as clsCompany

And now I ask them from my factory:
'MyA: gets me the company with ID 1
MyA = MyFactory.GetCompanyWithID(1, enumFromCacheOrDataBase)
'MyB: Same thing, but because it's alreaddy in the ache, it returns me the
same object
MyB = MyFactory.GetCompanyWithID(1, enumFromCacheOrDataBase)
So it I change now a property of MyA, the same property will be changed in
MyB, because they poitn to the same object.

But: If I now explicitly do a refresh from the database, and replace the
object in the cache with the new one, I get something totally different:
MyB = MyFactory.GetCompanyWithID(1, enumFromDatabase)
If I change now a property of MyA, it won't be changed in MyB: they are
linked to 2 different objects now! What can I do to have them always being
linked to the same object? If I refresh one, all the others must point to
the new object...
Any help our hitns will be really appreciated!

Thansk a lot in advance,
Once you've cached an object and supplied references to the object you
should not replace your cached object with a new instance. Instead provide
methods to re-load your objects internal state with new state gathered from
the DB.
--
Anthony Jones - MVP ASP/ASP.NET
Oct 12 '07 #2

I think you need to consider the Singleton design pattern.

http://www.dofactory.com/Patterns/PatternSingleton.aspx

And where the sample code has:
// Use 'Lazy initialization'
if (instance == null)
{
instance = new Singleton();
}

You'd have
instance = GetFromDatabaseSomehow(); //
You'll have to test this to see if it works for you or not. I think you'll
still run into a gotcha.

Are you doing this for webforms for winforms development? I'm guessing
winforms.
However, if something updates the singleton instance from another source,
you're gonna run into trouble.
I don't know if I've helped or not, take my advice with a grain of salt.
But I at least wanted to mention the Design Pattern.


"Pieter" <pi****************@hotmail.comwrote in message
news:%2****************@TK2MSFTNGP05.phx.gbl...
Hi,

I've implemented some type of cache, to be able to point with multiple
references to the same object. Although, this 1-object-stuff is broken if
I do a refresh from the DataBase.

For instance I declare 2 objects:
dim MyA as clsCompany
dim MyB as clsCompany

And now I ask them from my factory:
'MyA: gets me the company with ID 1
MyA = MyFactory.GetCompanyWithID(1, enumFromCacheOrDataBase)
'MyB: Same thing, but because it's alreaddy in the ache, it returns me the
same object
MyB = MyFactory.GetCompanyWithID(1, enumFromCacheOrDataBase)
So it I change now a property of MyA, the same property will be changed in
MyB, because they poitn to the same object.

But: If I now explicitly do a refresh from the database, and replace the
object in the cache with the new one, I get something totally different:
MyB = MyFactory.GetCompanyWithID(1, enumFromDatabase)
If I change now a property of MyA, it won't be changed in MyB: they are
linked to 2 different objects now! What can I do to have them always being
linked to the same object? If I refresh one, all the others must point to
the new object...
Any help our hitns will be really appreciated!

Thansk a lot in advance,

Pieter


Oct 12 '07 #3
On Oct 16, 11:36 am, James Crosswell <ja...@microforge.netwrote:
See http://pobox.com/~skeet/csharp/singleton.htmlfor more info.

Nice - the nested class at the end it quite an elegant solution.
It's handy if you really need the extra laziness. I tend just to use a
static variable initializer.
PS: You wouldn't be able to chuck an RSS feed on those articles would you?
There already is one :)
http://www.yoda.arachsys.com/csharp/rss.xml

I haven't written a new article for a long time though, due to work on
the book. Hopefully when I've finished the book I'll suddenly find
myself with so much free time that I'll be dying to add more articles!

Jon

Oct 16 '07 #4

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

Similar topics

6
by: Stephen Cook | last post by:
Having worked through the problems around enabling the document function using an XmlUrlResolver I started work on building a useful class to hide the intricacies. Trying to generalise the process...
2
by: Johann Blake | last post by:
I can hardly believe I'm the first one to report this, but having gone through the newsgroup, it appears that way. I would like to open a solution in the VS.NET IDE that consists of multiple...
2
by: george r smith | last post by:
I understand that when you compare (using ==) objects you are seeing if they both refer to the same object. My question why or when would you ever use multiple references to the same object. What...
9
by: Graham | last post by:
I have been having some fun learning and using the new Controls and methods in .Net 2.0 which will make my life in the future easier and faster. Specifically the new databinding practises and...
10
by: Steven Spits | last post by:
Hi, Because we have a large WebApp, back in 2002 we decided to use the following method: http://support.microsoft.com/default.aspx?scid=kb;en-us;307467 In short: Create a project "a" at...
5
by: | last post by:
Hi all, HttpWebRequest, and SoapHttpClientProtocol both expose a ClientCertificates property, which can hold multiple client certificates, but on the service side, it can only receive one client...
3
by: breeto | last post by:
If you've configured .NET Remoting to use more than one channel of the same type, for example two TcpClientChannels with unique names, when you want to create a proxy to a remote object how do you...
6
by: Joseph Geretz | last post by:
I have the following class which I am serializing and passing back and forth between my Web Service application and the client. public class Token : SoapHeader { public string SID; public...
3
by: Pieter | last post by:
Hi, I've implemented some type of cache, to be able to point with multiple references to the same object. Although, this 1-object-stuff is broken if I do a refresh from the DataBase. For...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...

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.