Side effects

This article describes how to catch changes to singlelinks as they evolve in MDrivenFramework.

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

I explained the HasUserCode property in a previous article that covers side effects on attributes, but what if you need side effects when your associations have changed?

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

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 of 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. Anything extra you do here may affect the system performance. (I do not want to scare you – I am just saying.) You will not notice anything – unless you make an error.

We decided if you want to use this particular cache hook, it should be an option. 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:

Side effects -1.png

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 page was edited 117 days ago on 08/28/2024. What links here