multiple references to only 1 object???

P

Pieter

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
 
A

Anthony Jones

Pieter said:
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.
 
S

sloan

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.
 
S

sloan

Good catch Jon.

I got your book Jon (pre pdf actually), and got up to the "myth" section
last night.

You need to make a poster with the "myths" on it. I'd pay another $10 for
that.
Just so I wouldn't have to "get into it" at work with people sometimes.
 
J

Jon Skeet [C# MVP]

sloan said:
Good catch Jon.

I got your book Jon (pre pdf actually), and got up to the "myth" section
last night.

Cool - hope you enjoy it! (With any luck it shouldn't be long until
chapters 6 and 7 hit MEAP, at which point you'll have the whole of the
C# 2 material.)
You need to make a poster with the "myths" on it. I'd pay another $10 for
that.
Just so I wouldn't have to "get into it" at work with people
sometimes.

LOL. Maybe I'll come up with a single page PDF that people can print
out with suitably scary warnings, LARTs etc. That could be quite a
laugh.
 
P

Pieter

Hi,

I don't really see how a singelton could help me here? That would mean that
I can only have 1 instance of a clsCompany instead of only 1 instance of
each clsCompany...

Or how would hanndle it with your design pattern to be able to open
clsCompany with ID 1 and clsCompany with ID 2?

And it is for Windows Forms indeed.
 
S

sloan

Then I think you need to look at the MVC (model view controller) design
pattern.

Which allows multiple views of the same model (one way to think of the model
is the data source of information).

The controller is able to say to (each) view "hey, the model has been
updated" (either by one of the views, or by a seperate mechanism).

...
 
B

Brian Gideon

The second sample on that page is broken, unfortunately. It uses the
double-checked locking algorithm but without a volatile variable.

Seehttp://pobox.com/~skeet/csharp/singleton.htmlfor more info.

I can't believe they haven't fixed that yet. It's been wrong for
like...ever.
 
J

Jon Skeet [C# MVP]

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
 

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