Side effects

From time to time you want to have side effects trigger when changing something in the objects. There are many valid reasons for this to happen – but it is not always the best way to solve things (just a heads up that you should stay declarative in your thinking and always think twice when your development starts to look reactive as side effects is a typical example of).

I explained the HasUserCode property in an article some time ago and that covers side effects on attributes, but what if you need side effects when your associations are changed?

Actually in earlier versions in ECO we offered HasUserCode on associations but it turned out that it was not a good solution – there were too many ways to make a association change (it has two ends to start with) to make the HasUserCode-approach execute correctly in every case.

So instead we introduced a new way to handle side effects on associations, a way that works consistently – every time – no matter which end of the association your code touched – the side effect code will trigger.

This new way is based on the ability to hook the objects cache of ECO (useable for a lot advanced stuff).

When one hooks the objects cache one must be careful – every single state change of domain objects will be managed by the cache – so anything extra you do here will effect the system performance. I do not want to scare you – I am just saying. You will not notice anything – unless you do something bad.

Anyway – we decided that it should be an option if you want to use this particular cache hook or not. You must “turn it on” in order to perform side effects on association changes.

This is how you turn it on, add this line early in your EcoSpace code :

new LinkOperationCache(FrontsidePolicy) { Enabled = true }

 

  1. public EcoProject1EcoSpace() : base()
  2. {
  3.     this.InitializeComponent();
  4.     new LinkOperationCache(FrontsidePolicy) { Enabled = true };
  5. }

 

Given this example model:

image

We can now use the two interfaces involved , IMultiLinkCatcher and ISingleLinkCatcher, in code:

  1. public partial class Class1:IMultiLinkCatcher
  2. {
  3.  
  4.   public void MultiLinkModified(IAssociationEnd ae)
  5.   {
  6.     int newcount=(this.AsIObject().Properties[ae] as IObjectList).Count;
  7.     System.Diagnostics.Trace.WriteLine("User changed the multilink " +ae.Owner.Name+"."+ae.Name +
  8.             ". It now has "+newcount.ToString()+ " objects, check single link end for details");
  9.   }
  10. }

 

And

  1. public partial class Class2:ISingleLinkCatcher
  2. {
  3.   public void SingleLinkModified(IAssociationEnd ae, IEcoObject oldValue, IEcoObject newValue)
  4.   {
  5.       string oldvaluestr="null";
  6.       string newvaluestr="null";
  7.       if (oldValue!=null && !oldValue.IsNull())
  8.         oldvaluestr=oldValue.ToString();
  9.       if (newValue != null && !newValue.IsNull())
  10.         newvaluestr=newValue.ToString();
  11.       System.Diagnostics.Trace.WriteLine("User changed value on Singlelink " +ae.Owner.Name+"."+ae.Name +
  12.         " from " + oldvaluestr + " to " + newvaluestr);
  13.   }
  14. }

This entry was posted in CO-Unique, Declarative, MDA, Packages. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

*