Print Page | Close Window

Recursive property interceptors

Printed From: IdeaBlade
Category: DevForce
Forum Name: DevForce 2010
Forum Discription: For .NET 4.0
URL: http://www.ideablade.com/forum/forum_posts.asp?TID=3570
Printed Date: 13-May-2026 at 2:59am


Topic: Recursive property interceptors
Posted By: stephenmcd1
Subject: Recursive property interceptors
Date Posted: 07-Aug-2012 at 10:18am
It seems like Property Interceptors can't call themselves (re-entrancy).  On the surface, that seems find but we are running into a case where the Property Interceptor for a property needs to call the same property on a related entity - and in that case, the second interceptor doesn't fire.

I'm not sure whether this is a feature or a bug or just us doing something wrong.  Since it's a bit confusing to explain, here is some sample code the explains the problem we are having.  This is an extra property we added to the partial class.  The interceptor on our property looks at that property on a parent entity:
    public partial class Employee
    {
        //Some random property that we have interceptors on
        public string FriendlyText
        {
            get { return PropertyMetadata.FriendlyText.GetValue(this); }
            set { PropertyMetadata.FriendlyText.SetValue(this, value); }
        }

        [AfterGet("FriendlyText")]
        //Our interceptor that we want to be "recursive"
        public void AfterGetText(PropertyInterceptorArgs<Employee, string> args)
        {
            //This is a bit of a contrived example but it illustrates the core problem.
            //This property depends on its parent so we re-derive the value each time.  And since it
            //  depends on the parent, it is recursive

            //If we don't report to anybody, we are the top dog
            if (ReportTo == null || ReportTo.EntityAspect.IsNullEntity)
            {
                args.Value = "Top Dog";
            }
            else
            {
                //Otherwise we report to somebody else.  Get their 'FriendlyText'
                args.Value = "Reports to " + ReportTo.FriendlyText;
            }
        }

        public partial class PropertyMetadata
        {
            public static readonly DataEntityProperty<Employee, string> FriendlyText =
                new DataEntityProperty<Employee, string>("FriendlyText", true, false, ConcurrencyStrategy.None, false, null, false);
        }
    }

Here is some text code that fails:
    private void TestFriendlyText()
    {
        //Make the boss that doesn't report to anybody
        var boss = new Employee();

        //It should be the top dog
        DebugFns.Assert(boss.FriendlyText == "Top Dog");

        //Make another person that reports to the boss
        var otherPerson = new Employee();
        otherPerson.ReportTo = boss;

        //Sanity check: The other person's report to should be Top Dog
        DebugFns.Assert(otherPerson.ReportTo.FriendlyText == "Top Dog");

        //And finally check our Friendly Text....but this line will fail because it actually equals
        //  "Reports to " - the FriendlyText of the ReportsTo return null.  :-(
        DebugFns.Assert(otherPerson.FriendlyText == "Reports to Top Dog");
    }




Replies:
Posted By: sbelini
Date Posted: 14-Aug-2012 at 3:12pm
Hi Stephen,
 
I've checked with our senior enginner and confirmed that Property interceptors are non-recursive by design.
 
Imagine if the "top dog" suddenly reports to the someone in the "lower ranks" of the company. (while this is not practical, it's still possible)
You could get into an infinite loop.
 
If you want to get the whole "chain of command" from employee A, you could still try something like:
 
  public void GetChainOfCommand(Employee emp) {
    if (!emp.Employee2.EntityAspect.IsNullEntity) {
      GetChainOfCommand(emp.Employee2);
    }
  }
 
and there assign your custom properties. However, you could still come accross the issue mentioned previously.
 
Silvio.
 
 



Print Page | Close Window