Adapter Assigning Related Entities and Firing Events

Posts   
 
    
Sam avatar
Sam
User
Posts: 95
Joined: 30-Jun-2004
# Posted on: 21-Feb-2006 23:29:09   

Let me see if I understand this correctly and then I will tell you the problem that I am having. If I have an Entity called MyEntity. This has a (m:1) related entity To Status. StatusCode is a foreign key in MyEntity. Now, using prefetch paths I get the status for MyEntity so that MyEntity.Status returns a StatusEntity for MyEntity. Now if I change MyEntity.StatusCode MyEntity.Status = null. This is expected as I have changed the foriegn key MyEntity no longer can have a reference to a StatusEntity without re-fetching. So, in playing around I discoved that something like following works great without having to re-fetch the entity:


StatusEntity status = listBox.SelectedItem as StatusEntity;
myEntity.Status = status;

Now my myEntity.Status returns the correct status and LLBL updates the myEntity.StatusCode. BEAUTIFUL exactly the behavior that I want. Unfortuantly I cannot figure out how to hook an event that tells me that assignment (myEntity.Status = status) took place. the myEntity.StatusCode changed event does not fire (event though the value is updated via the relationship). I have also tried to hook the myEntity.Status.EntityContentsChanged. No dice. This event is not firing either. This is a problem as I need to notify some objects when this value is changed. It also screws up databinding inwindows forms a little as StatusCode in myEntity is magically updated without notifying anybody.

Any ideas?

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39930
Joined: 17-Aug-2003
# Posted on: 22-Feb-2006 09:33:31   

The field event isn't fired as it's hard to get the event fired from the base class where the syncing takes place. The events are defined in the generated code (as they're bound to a name) and therefore not known to the base class. It would require a sometimes large routine to find the event when called from the base class.

The sync routine does fire the Entitycontentschanged event though, at least in the latest runtime code. Could you please check what's the version of your runtime libraries? (rmb on ormsupportclasses dll -> properties -> version tab.

Frans Bouma | Lead developer LLBLGen Pro
Sam avatar
Sam
User
Posts: 95
Joined: 30-Jun-2004
# Posted on: 22-Feb-2006 18:02:08   

I have wired up both:


            myEntity.EntityContentsChanged += new EventHandler(myEntity_EntityContentsChanged);
            myEntity.Status.EntityContentsChanged +=new EventHandler(Status_EntityContentsChanged);

        void myEntity_EntityContentsChanged(object sender, EventArgs e)
        {
            Debug.WriteLine("myEntity_EntityContentsChanged");
        }

        void Status_EntityContentsChanged);(object sender, EventArgs e)
        {
            Debug.WriteLine("Status_EntityContentsChanged);");
        }

EDIT: I lied the both events are firing. However only the myEntity.EntityContentsChanged is fireing when the StatusEntity is changed. This is a little problimatic for databinding as I don't know what in MyEntity has changed. Am I do call BiningSource.ResetBindings(false) every time myEntity.EntityContentsChanged? For every other "regular" property it behaves properly an I will be rebinding twice. Also, I will still have a problem in that I want to do some stuff in my business layer if the myEntity.Status (the entity) of MyEntity is changed. Thus hooking the EntityContentsChanged will cause this work to be done anytime any property is changed. I was thinking about making StatusCode a readonly field and requiring developers to assign to myEntity.Status (the entity). This way I could be sure that myEntity.Status was never nulled out by changing myEntity.StatusCode. How do I do work when just myEntity.Status (the entity) has changed.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39930
Joined: 17-Aug-2003
# Posted on: 23-Feb-2006 22:20:44   

I see the problem, and I have no clear answer. If the entity is in a collection, the collection will fire a listchanged event for the entity and the grid should update the row.

As I said, it's problematic for us to fix it properly in the current code. I will file a bug for it so it's fixed in v2, but that's of course not a solution to your current problem.

What you can do, which is a little lame, is this: myEntity.StatusCode = myStatus.Code; myEntity.Status = myStatus;

This will trigger the FK field set event. I know it's a 2 liner when you could have just 1 line of code, though it's what I can offer now.

Frans Bouma | Lead developer LLBLGen Pro
Sam avatar
Sam
User
Posts: 95
Joined: 30-Jun-2004
# Posted on: 23-Feb-2006 22:46:27   

What you can do, which is a little lame, is this:

Lame...maybe a little...but if it works!! I had already though of this but unfortunately I am working with properties bound to controls. However, I could bind to SelectedValue to ID and SelectedItem to Subentity. I think that should work. I will definitely use your solution in business layer. Thanks for your help!