|
dev
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
multiple references to only 1 object???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 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. Show quote "Pieter" <pieterNOSPAMcoucke@hotmail.com> 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 > > > >
Show quote
"Pieter" <pieterNOSPAMcoucke@hotmail.com> wrote in message Once you've cached an object and supplied references to the object younews:%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, > 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 sloan <sl***@ipass.net> wrote:
> I think you need to consider the Singleton design pattern. The second sample on that page is broken, unfortunately. It uses the > > http://www.dofactory.com/Patterns/PatternSingleton.aspx double-checked locking algorithm but without a volatile variable. See http://pobox.com/~skeet/csharp/singleton.html for more info. -- 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 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. Show quote "Pieter" <pieterNOSPAMcoucke@hotmail.com> 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 > > > > 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. Show quote "Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message news:MPG.2179c6956ec1cbf5527@msnews.microsoft.com... > sloan <sl***@ipass.net> 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 - <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 sloan <sl***@ipass.net> wrote:
> Good catch Jon. Cool - hope you enjoy it! (With any luck it shouldn't be long until > > I got your book Jon (pre pdf actually), and got up to the "myth" section > last night. 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 LOL. Maybe I'll come up with a single page PDF that people can print > that. > Just so I wouldn't have to "get into it" at work with people > sometimes. out with suitably scary warnings, LARTs etc. That could be quite a laugh. -- 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 sloan <sl***@ipass.net> wrote:
> I think you need to consider the Singleton design pattern. The second sample on that page is broken, unfortunately. It uses the > > http://www.dofactory.com/Patterns/PatternSingleton.aspx double-checked locking algorithm but without a volatile variable. See http://pobox.com/~skeet/csharp/singleton.html for more info. -- 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 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. Show quote "Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message news:MPG.2179c6956ec1cbf5527@msnews.microsoft.com... > sloan <sl***@ipass.net> 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 - <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 sloan <sl***@ipass.net> wrote:
> Good catch Jon. Cool - hope you enjoy it! (With any luck it shouldn't be long until > > I got your book Jon (pre pdf actually), and got up to the "myth" section > last night. 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 LOL. Maybe I'll come up with a single page PDF that people can print > that. > Just so I wouldn't have to "get into it" at work with people > sometimes. out with suitably scary warnings, LARTs etc. That could be quite a laugh. -- 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 Jon Skeet [C# MVP] wrote:
> sloan <sl***@ipass.net> wrote: Nice - the nested class at the end it quite an elegant solution.>> 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. 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 On Oct 16, 11:36 am, James Crosswell <ja***@microforge.net> wrote:
> > See http://pobox.com/~skeet/csharp/singleton.htmlfor more info. It's handy if you really need the extra laziness. I tend just to use a> > Nice - the nested class at the end it quite an elegant solution. 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 On Oct 12, 1:00 pm, Jon Skeet [C# MVP] <sk***@pobox.com> wrote:
> sloan <sl***@ipass.net> wrote: I can't believe they haven't fixed that yet. It's been wrong for> > 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 - <sk***@pobox.comhttp://www.pobox.com/~skeet Blog:http://www.msmvps.com/jon.skeet > If replying to the group, please do not mail me too like...ever. Jon Skeet [C# MVP] wrote:
> sloan <sl***@ipass.net> wrote: Nice - the nested class at the end it quite an elegant solution.>> 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. 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 On Oct 16, 11:36 am, James Crosswell <ja***@microforge.net> wrote:
> > See http://pobox.com/~skeet/csharp/singleton.htmlfor more info. It's handy if you really need the extra laziness. I tend just to use a> > Nice - the nested class at the end it quite an elegant solution. 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 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. Show quote "sloan" <sl***@ipass.net> 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. > 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). ... Show quote "Pieter" <pieterNOSPAMcoucke@hotmail.com> 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" <sl***@ipass.net> 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. >> > > On Oct 12, 1:00 pm, Jon Skeet [C# MVP] <sk***@pobox.com> wrote:
> sloan <sl***@ipass.net> wrote: I can't believe they haven't fixed that yet. It's been wrong for> > 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 - <sk***@pobox.comhttp://www.pobox.com/~skeet Blog:http://www.msmvps.com/jon.skeet > If replying to the group, please do not mail me too like...ever. 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. Show quote "sloan" <sl***@ipass.net> 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. > 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). ... Show quote "Pieter" <pieterNOSPAMcoucke@hotmail.com> 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" <sl***@ipass.net> 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. >> > > |
|||||||||||||||||||||||