Search code examples
hookmdriven

HasUserCode for associations?


In MDriven it's possible to set HasUserCode for attributes, to generate hook methods that are called e.g. when the attribute is about to be changed.

I would need a similar ability for a couple of associations, or to be more precise: for a couple of linkroles.

For example, for linkrole MyClass.RelatedThing I would like to perform an action when someone does myClassObj.RelatedThing = thatThing; or myClassObj.RelatedThing = null;.

All the linkroles that I need this for are single-links, but it would be nice to know also how it could be done for multi-links.

I already have a separate cache inserted, that performs actions when attributes and links are changed, but I don't like to put this functionality in a central place like that, because it concerns only a few specific classes and linkroles, while the cache is a global "thing".


Solution

  • The issue that was with HasUserCode for relations was that it was not fool proof. Since you can set a relation in both ends confusion started when single link ends was implicitly set by adding to its other end.

    This confusion caused so much uncertainty that we removed the HasUserCode options from associationsends.

    One option is to assign the PropertyChangedEventHandler PropertyChanged event of the object/classes you need to track.

    Note that this is the .net INotifyPropertChange strategy and it will not allow you to halt an assignment - but you will see that it has happened.