473,769 Members | 1,959 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.GetCo mpanyWithID(1, enumFromCacheOr DataBase)
'MyB: Same thing, but because it's alreaddy in the ache, it returns me the
same object
MyB = MyFactory.GetCo mpanyWithID(1, enumFromCacheOr DataBase)
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.GetCo mpanyWithID(1, enumFromDatabas e)
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 1217
"Pieter" <pi************ ****@hotmail.co mwrote in message
news:%2******** ********@TK2MSF TNGP05.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.GetCo mpanyWithID(1, enumFromCacheOr DataBase)
'MyB: Same thing, but because it's alreaddy in the ache, it returns me the
same object
MyB = MyFactory.GetCo mpanyWithID(1, enumFromCacheOr DataBase)
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.GetCo mpanyWithID(1, enumFromDatabas e)
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 = GetFromDatabase Somehow(); //
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.co mwrote in message
news:%2******** ********@TK2MSF TNGP05.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.GetCo mpanyWithID(1, enumFromCacheOr DataBase)
'MyB: Same thing, but because it's alreaddy in the ache, it returns me the
same object
MyB = MyFactory.GetCo mpanyWithID(1, enumFromCacheOr DataBase)
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.GetCo mpanyWithID(1, enumFromDatabas e)
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...@microfor ge.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
2489
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 I've hit a snag. How do I resolve multiple external references? The transform method on a stylesheet only takes one resolver, not an array Stephen
2
2861
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 DLLs and a test application (an .EXE). The .EXE is my startup application. All the DLLs are shared components. This means that they contain a key and are stored in the GAC. When I go to run the test application in the IDE, it will not execute...
2
1912
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 would be a production code type example why you would set objOne = objTwo. Thanks grs
9
2778
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 wizards. But, I have found that trying to do something "outside the norm" adds a rather large level of complexity and/or data replication. Background I have been commissioned to create a web-based application for a client. It has a formsaunthentication...
10
1594
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 http//localhost/MyWebApp
5
4451
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 certificate, since it derives System.Web.Services.WebService class, and it's Context.Request.ClientCertificate is a single HttpClientCertificate object, is there a way to receive all the client certificates that is sent in the request? or does IIS...
3
14477
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 specify which channel you want that proxy to use? Thanks in advance.
6
3990
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 string UID; public string PWD; }
3
1282
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 instance I declare 2 objects: dim MyA as clsCompany dim MyB as clsCompany
0
9420
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10205
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10035
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9984
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9851
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7401
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6662
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5293
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3556
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.