multiple references to only 1 object???

multiple references to only 1 object???

am 12.10.2007 11:56:16 von 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

Re: multiple references to only 1 object???

am 12.10.2007 12:19:43 von Anthony Jones

"Pieter" wrote in message
news:%23hAPjYLDIHA.1208@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

Re: multiple references to only 1 object???

am 12.10.2007 19:42:41 von 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.




"Pieter" wrote in message
news:%23hAPjYLDIHA.1208@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
>
>
>
>

Re: multiple references to only 1 object???

am 12.10.2007 20:00:21 von skeet

sloan wrote:
> 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.

--
Jon Skeet -
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Re: multiple references to only 1 object???

am 12.10.2007 21:00:34 von 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.




"Jon Skeet [C# MVP]" wrote in message
news:MPG.2179c6956ec1cbf5527@msnews.microsoft.com...
> sloan wrote:
>> 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.
>
> --
> Jon Skeet -
> http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
> If replying to the group, please do not mail me too

Re: multiple references to only 1 object???

am 12.10.2007 21:08:14 von skeet

sloan wrote:
> 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.

--
Jon Skeet -
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Re: multiple references to only 1 object???

am 15.10.2007 14:40:29 von 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.

"sloan" wrote in message
news:OV0eKdPDIHA.4476@TK2MSFTNGP06.phx.gbl...
>
> 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.
>

Re: multiple references to only 1 object???

am 15.10.2007 19:42:10 von 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).

...




"Pieter" wrote in message
news:eOtZTiyDIHA.3712@TK2MSFTNGP02.phx.gbl...
> 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.
>
> "sloan" wrote in message
> news:OV0eKdPDIHA.4476@TK2MSFTNGP06.phx.gbl...
>>
>> 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.
>>
>
>

Re: multiple references to only 1 object???

am 15.10.2007 21:51:20 von Brian Gideon

On Oct 12, 1:00 pm, Jon Skeet [C# MVP] wrote:
> sloan wrote:
> > 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.
>
> Seehttp://pobox.com/~skeet/csharp/singleton.htmlfor more info.
>
> --
> Jon Skeet - 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.

Re: multiple references to only 1 object???

am 16.10.2007 12:36:00 von James Crosswell

Jon Skeet [C# MVP] wrote:
> sloan wrote:
>> 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.

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

Re: multiple references to only 1 object???

am 16.10.2007 15:54:02 von skeet

On Oct 16, 11:36 am, James Crosswell wrote:
> > 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