New Posts New Posts RSS Feed: Phantom objects?
  FAQ FAQ  Forum Search   Calendar   Register Register  Login Login

Phantom objects?

 Post Reply Post Reply
Author
Sinnema View Drop Down
Groupie
Groupie
Avatar

Joined: 23-Mar-2009
Location: Muri AG, EU, CH
Posts: 54
Post Options Post Options   Quote Sinnema Quote  Post ReplyReply Direct Link To This Post Topic: Phantom objects?
    Posted: 27-Feb-2010 at 4:31am
Hi Ward,
 
You're at it again ;-)
 
You got it wrong on the multiple EntityManagers part. We use only 1 EntityManager in the Application. In another thread we discussed this and I answered that we do use contexts on each level in our Architecture (View, ViewModel, Model) but that all retrieve 1 EntityManager from the Model layer (Singleton Pattern used on the Model). I do see your point on the double reference of the EntityManager in my example. I'll try and simplyfy that.
 
I'll investigate if the Entities are detached, but as far as I remember they are not. I'll have to get back to you on that.
 
I know it is difficult to answer questions like these when you're not in possession of the code that uses DevForce. We could however set up a remote session (like we did last year) to investigate some of the problems we're having at the moment (remember the Refetch Problem thread too).
 
Regards
Paul Sinnema
Diartis AG
 


Edited by Sinnema - 02-Mar-2010 at 4:36am
Senior Developer at Diartis AG in Switzerland.
Back to Top
WardBell View Drop Down
IdeaBlade
IdeaBlade
Avatar

Joined: 31-Mar-2009
Location: Emeryville, CA,
Posts: 338
Post Options Post Options   Quote WardBell Quote  Post ReplyReply Direct Link To This Post Posted: 27-Feb-2010 at 12:12am
Hi Paul
 
Sorry but your reference to DefaultManager undermines your case.
 
I know you use multiple managers. I cannot have confidence in your example when you reference any EntityManager (DefaultManager in this case) which might be different from the manager of the entity raising the OnPropertyChanged.
 
I have never, ever, seen two entities of the same type within the same EntityManager that have the same EntityKey. That's because it is not physically possible.
 
You can prove me wrong. But the only way to do so is if your test is something like:
 
  object phantom =
     this.EntityAspect.EntityManager
         // This next line from your code is pure obfuscation
         // and simply returns the same EntityManager you started with
         .GetEntityGroup(this.GetType()).EntityManager
         .FindEntity(this.EntityKey); 
 
  if (!ReferenceEquals(phantom, this))
     throw new InvalidOperationException("2 entities in the same manager with the same key")
 
My bet is that either (a) "this" entity is a detached instance (highly probable) or (b) this.EntityAspect.EntityManager is not the same as ...KLIBDefaultManager
 
I'll say it again: it is improper to refer to a specific EntityManager from within an entity. You should always use the entity's own EntityManager which you can reach with this phrase: "this.EntityAspect.EntityManager".
 
If something seems strange, always check the EntityManager of the entity and always check the this.EntityAspect.EntityState to verify your assumptions. Otherwise you are bound to fool yourself.
 
My bet is that the entity invoking OnPropertyChanged is detached. Something is attempting to modify this detached entity when you don't think it should. Either that or the one in the collection is detached.
 
The next question is "why are you ... or why is the collection ... holding on to such an entity and how did it become detached?"
 
Can't answer that question without context. To get that context, you would be wise to narrow it down to a simple reproducible case.
 


Edited by WardBell - 27-Feb-2010 at 12:13am
Back to Top
Sinnema View Drop Down
Groupie
Groupie
Avatar

Joined: 23-Mar-2009
Location: Muri AG, EU, CH
Posts: 54
Post Options Post Options   Quote Sinnema Quote  Post ReplyReply Direct Link To This Post Posted: 25-Feb-2010 at 1:07am
Bump
Senior Developer at Diartis AG in Switzerland.
Back to Top
Sinnema View Drop Down
Groupie
Groupie
Avatar

Joined: 23-Mar-2009
Location: Muri AG, EU, CH
Posts: 54
Post Options Post Options   Quote Sinnema Quote  Post ReplyReply Direct Link To This Post Posted: 20-Feb-2010 at 8:13am
Hi,
 
We repeatedly get, what we call, Phantom objects from DevForce during SaveChanges() via the OnPropertyChanged event. We added a test to stop these object from being processed because our ObservableList classes can't find them in their internal lists. Please look at the code below. Do you agree this is weird and should not happen?
 
What we see in the debugger when we catch a phantom object is that it is almost a perfect copy of the original (this) but that it is the object before it was changed.
 
We've got the feeling that DevForce somewhow deep-copies objects (fi as a before image or rollback mechanism) but forgets to decouple events from these objects. 
 
Regards
Paul Sinnema
Diartis AG
 
protected override void OnPropertyChanged(System.ComponentModel.PropertyChangedEventArgs e)
{
 base.OnPropertyChanged(e);
 object phantom = ModelContext.Instance.KLIBDefaultManager.GetEntityGroup(this.GetType()).EntityManager.FindEntity(this.EntityKey);
 if (object.ReferenceEquals(phantom, this))
 {
  // Process the object
 }
 else
 {
  // Do nothing
 }
 ...


Edited by Sinnema - 20-Feb-2010 at 8:18am
Senior Developer at Diartis AG in Switzerland.
Back to Top
 Post Reply Post Reply

Forum Jump Forum Permissions View Drop Down