So I'm new to the concept of routed events, but I know that messages are flying everywhere when fields are changing via the RaiseDataMemberChanging / RaiseDataMemberChanged messages.

The quick question is how to I "listen" for those routed messages?

I would like my view model, in a MVVM correct matter, intercept a message that a field is being updated, deep down in the heirarchy of tables. If a change occurs in a child (a date range), that date range change has some business logic associated with it that works from top down.

I've know that I can use partial methods to inject code during the changing and changed events, but the design (one to one relationship) means that a change to the child, in the context of the child, has no reference the parent (Parent has a one to one reference to child, but child has no reference to parent).

Yes I can add the reference from the child to the parent (making the parent have both a one to one, as well as a one to many relationship), but during creation of the entity, this creates a problem - parent has to exist before the child entity exists, and the child entity has to exist before a reference to the parent can exist. This design requires an insert of the parent, and child, then an update of the child - which confuses WCF RIA Services during the inital creation process.

Sorry for the long winded explaination, but I'll even consider design changes if this all makes sense to anyone that cares to comment.

Any input would be appreciated.

有帮助吗?

解决方案

I'm using a subscription to the PropertyChanged events of the Entity in question. It seems like a lot of work to filter out all the events for a couple of fields.

Using RX, I'm hoping that the resources used are minimal, and the weak reference avoids the memory link issue when a strong reference is used to deal with events:

        Observable.FromEventPattern<PropertyChangedEventArgs>(this.FlowEntity, "PropertyChanged")
      .Where(pPropertyChanged => (
                                 pPropertyChanged.EventArgs.PropertyName.EndsWith("Date")) ||
                                 pPropertyChanged.EventArgs.PropertyName == "Capacity"
                                 )
      .Subscribe(pObserver => this.RaiseFlowEntityDateChanged(this, pObserver.EventArgs));

FlowEntity is the child entity that I'm monitoring from the parent. I then raise custom event using the parent entity, rather than the entity that actually holds the event.

I can't raise this event from the partial methods, as the child entity would not have the context of the parent.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top