What about HasUserCode in Enterprise Core Objects – ECO the Model Driven Framework

This article was moved to MDriven Wiki What about HasUserCode in Enterprise Core Objects – MDriven Framework

Class Attributes in classes modeled with ECO has a property called HasUserCode. This post sets out to explain what effect the property has and what the intended use is.

Let us say that we have this model:


Focus the DueDate attribute and look at the Object inspector:


By default the HasUserCode property is false – this says that there are NO side effects or checks applied in code that influence or stop anyone from having the value set.

Let us generate the code:


The Invoice class is implemented as a partial class were Invoice.cs is yours and Invoice.eco.cs is maintained by the Framework.

Open up the Framework owned Invoice.eco.cs and look at the attribute definition:

Code Snippet
  1. [UmlElement(Id=“256adf06-35be-47e7-9189-e882c4bb779f”, Index=Eco_LoopbackIndices.DueDate)]
  2. public System.DateTime DueDate {
  3.   get {
  4.     return ((System.DateTime)(this.eco_Content.get_MemberByIndex(Eco_LoopbackIndices.DueDate)));
  5.   }
  6.   set {
  7.     this.eco_Content.set_MemberByIndex(Eco_LoopbackIndices.DueDate, ((object)(value)));
  8.   }
  9. }

As you can see there is no room for extravaganza while setting or reading the attribute “DueDate”.

Let us set HasUserCode to true for the DueDate attribute in the model – update the code and look at the Framework owned file again:

Code Snippet
  1. [UmlElement(Id=“256adf06-35be-47e7-9189-e882c4bb779f”, Index=Eco_LoopbackIndices.DueDate)]
  2. [UmlTaggedValue(“Eco.HasUserCode”, “True”)]
  3. public System.DateTime DueDate {
  4.   get {
  5.     this.DueDateReading();
  6.     System.DateTime res = ((System.DateTime)(this.eco_Content.get_MemberByIndex(Eco_LoopbackIndices.DueDate)));
  7.     this.DueDateRead(ref res);
  8.     return res;
  9.   }
  10.   set {
  11.     System.DateTime oldValue = this.DueDate;
  12.     System.DateTime newValue = value;
  13.     bool abortModification = false;
  14.     this.DueDateChanging(ref value, ref abortModification);
  15.     if (abortModification) {
  16.       return;
  17.     }
  18.     this.eco_Content.set_MemberByIndex(Eco_LoopbackIndices.DueDate, ((object)(value)));
  19.     this.DueDateChanged(oldValue, newValue, value);
  20.   }
  21. }

Notice the difference. We now have calls to DueDateReading, DueDateRead, DueDateChanging, DueDateChanged.

These are all Partial Methods declared by the framework. Partial method is a .net language construct that is perfect for Frameworks like ECO to use. ECO added these definitions to the Invoice class:

Code Snippet
  1. #region *** DueDate partial methods ***
  2. /// <summary>This method is called before the attribute DueDate is read</summary>
  3. partial void DueDateReading();
  4. /// <summary>This method is called after DueDate is read. It is possible to change the value here.</summary>
  5. partial void DueDateRead(ref DateTime value);
  6. /// <summary>This method is called before DueDate is modified, it is possible to change the value that is stored here or abort the modification</summary>
  7. partial void DueDateChanging(ref DateTime value, ref bool abortModification);
  8. /// <summary>This method is called after DueDate is modified</summary>
  9. /// <param name=”oldValue”>This is the value that DueDate had before the modification</param>
  10. /// <param name=”newValue”>This is the value that someone tried to set</param>
  11. /// <param name=”finalValue”>This is the value that DueDate has after the modification</param>
  12. partial void DueDateChanged(DateTime oldValue, DateTime newValue, DateTime finalValue);
  13. #endregion *** DueDate partial methods ***

You – as the developer – may need only one or two of these interception points – but ECO gave you all. Unused Partial Methods are however stripped by the compiler and cost you nothing in runtime.

Now we can head back to Invoice.cs – the implementation file that you use and the framework does not touch:

public partial class Invoice {


Very empty so far but we can now implement one of the interception points. Just write “partial” and see what comes up:


Pick the one you want and add your stuff:

Code Snippet
  1. partial void DueDateChanging(ref DateTime value, ref bool abortModification)
  2. {
  3.     if (value < this.Date.AddDays(5))
  4.         abortModification = true;
  5. }

Why not do this all the time?

The normal question after having explained HasUserCode is “Why would you ever want to turn HasUserCode off?“. It is a relevant question since the partial methods does not cost anything in runtime – we might as well leave the HasUserCode on all the time… Or?

The question is slightly more complex. ECO has the ambition to be as effective as possible and always use the most efficient way to get things done. As a result of this all the internal state of objects is not stored in the frontend object as such but in a centralized cache. This strategy gives us advantages on many levels – like holding object identities before they are fully loaded, knowing what we have loaded or not, signaling changes, ensuring association ends even if not completely loaded, execute models without generated code etc , etc.

So when following expressions for subscriptions – created automatically or explicitly, or expressions from ViewModels, Handles, Constraints or Actions – ECO does not really need to route the call thru the frontend object – it can head straight for the cache and cut a few cpu cycles from the workload – it might not be much but since Eco is a Framework these things add up and become important.

UNLESS the read or written attribute has the HasUserCode flag set – if it has ECO will always route access thru the frontside object.

So does this mean that HasUserCode==true makes my code slower? Technically yes – it will cost you a couple of cpu cycles – so “a lot slower?” No!. You should not worry about that if you need the interception points – but you should leave HasUserCode set to false if you have no intention of using the interception points.

This entry was posted in CO-Unique. Bookmark the permalink.

2 Responses to What about HasUserCode in Enterprise Core Objects – ECO the Model Driven Framework

Leave a Reply

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


This site uses Akismet to reduce spam. Learn how your comment data is processed.