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
Anthony Jones - 12 Oct 2007 11:19 GMT
> Hi,
>
[quoted text clipped - 26 lines]
>
> 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.

Signature
Anthony Jones - MVP ASP/ASP.NET
sloan - 12 Oct 2007 18:42 GMT
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.
> Hi,
>
[quoted text clipped - 28 lines]
>
> Pieter
Jon Skeet [C# MVP] - 12 Oct 2007 19:00 GMT
> I think you need to consider the Singleton design pattern.
>
> http://www.dofactory.com/Patterns/PatternSingleton.aspx
The second sample on that page is broken, unfortunately. It uses the
double-checked locking algorithm but without a volatile variable.
See http://pobox.com/~skeet/csharp/singleton.html for more info.

Signature
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
sloan - 12 Oct 2007 20:00 GMT
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.
>> I think you need to consider the Singleton design pattern.
>>
[quoted text clipped - 4 lines]
>
> See http://pobox.com/~skeet/csharp/singleton.html for more info.
Jon Skeet [C# MVP] - 12 Oct 2007 20:08 GMT
> 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.

Signature
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Brian Gideon - 15 Oct 2007 20:51 GMT
> > I think you need to consider the Singleton design pattern.
>
[quoted text clipped - 8 lines]
> Jon Skeet - <sk...@pobox.com>http://www.pobox.com/~skeet Blog:http://www.msmvps.com/jon.skeet
> If replying to the group, please do not mail me too
I can't believe they haven't fixed that yet. It's been wrong for
like...ever.
James Crosswell - 16 Oct 2007 11:36 GMT
>> I think you need to consider the Singleton design pattern.
>>
[quoted text clipped - 4 lines]
>
> See http://pobox.com/~skeet/csharp/singleton.html for more info.
Nice - the nested class at the end it quite an elegant solution.
PS: You wouldn't be able to chuck an RSS feed on those articles would you?
Best Regards,
James Crosswell
Microforge.net LLC
http://www.microforge.net
Jon Skeet [C# MVP] - 16 Oct 2007 14:54 GMT
> > 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
Pieter - 15 Oct 2007 13:40 GMT
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.
> I think you need to consider the Singleton design pattern.
>
[quoted text clipped - 20 lines]
> 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.
sloan - 15 Oct 2007 18:42 GMT
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).
..
> Hi,
>
[quoted text clipped - 31 lines]
>> 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.