CSLA .NET

From Rockford Lhotka's Expert C# 2005 and VB 2005 Business Objects books

Welcome to CSLA .NET Sign in | Join | Help
in Search

General strategy for reporting broken rules from child object via root?

Last post 07-23-2008, 8:22 PM by rsbaker0. 7 replies.
Sort Posts: Previous Next
  •  07-18-2008, 5:03 PM 24801

    General strategy for reporting broken rules from child object via root?

    What is the recommended approach to allow for the consumer of a root object to know what is broken when the broken rule is in a child?

    It's easy enough to implement IsValid() so that this validity is properly handled, but if the consumer of the root object decides to test IsValid() first, the BrokenRulesCollection of the root object will be empty, so there isn't anything useful to display except "Sorry, it's broken".

    Similarly, if the root object is throwing an exception because you called Save() and it isn't valid, the root doesn't know at that time which child is at fault.

    Are we supposed to reimplement some sort of traversal of the object hierarchy to locate the offending child, or is there something in the framework that will help?  (CSLA 3.0.x)

     

  •  07-19-2008, 6:04 PM 24806 in reply to 24801

    RE: General strategy for reporting broken rules from child object via root?

    I posted a generic class code to do this here: http://forums.lhotka.net/forums/post/23717.aspx

    Feel free to use / modify it as you see fit.

     

    Sergey Barskiy

    Senior Consultant

    office: 678.405.0687 | mobile: 404.388.1899

    cid:_2_0648EA840648E85C001BBCB886257279
    Microsoft Worldwide Partner of the Year | Custom Development Solutions, Technical Innovation

     

    From: rsbaker0 [mailto:cslanet@lhotka.net]
    Sent: Friday, July 18, 2008 6:04 PM
    To: Sergey Barskiy
    Subject: [CSLA .NET] General strategy for reporting broken rules from child object via root?

     

    What is the recommended approach to allow for the consumer of a root object to know what is broken when the broken rule is in a child?

    It's easy enough to implement IsValid() so that this validity is properly handled, but if the consumer of the root object decides to test IsValid() first, the BrokenRulesCollection of the root object will be empty, so there isn't anything useful to display except "Sorry, it's broken".

    Similary, if the root object is throwing an exception because you called Save() and it isn't valid, the root doesn't know at that time which child is at fault.

    Are we supposed to reimplement some sort of traversal of the object hierarchy to locate the offending child, or is there something in the framework that will help?  (CSLA 3.0.x)

     



  •  07-20-2008, 7:32 AM 24812 in reply to 24806

    Re: RE: General strategy for reporting broken rules from child object via root?

    Thanks! This certainly will help me get what I need.

    I think what I'll do is use your logic to implement a "BrokenObjectsCollection". That allows the consumer to have a little more control over how the error is handled.

     

  •  07-21-2008, 10:02 AM 24831 in reply to 24812

    Re: RE: General strategy for reporting broken rules from child object via root?

    Ouch!

    It turns out that if you traverse the hierarchy using Properties, then lazy-loaded collections get loaded when the the property value is accessed.

    I could access the backing fields themselves instead which would avoid this problem, but I'd be concerned about partial trust issues since these fields are private.

    Can a member function of a class reflect on it's own private members without trust issues?

  •  07-21-2008, 11:34 PM 24856 in reply to 24831

    Re: RE: General strategy for reporting broken rules from child object via root?

    rsBaker,

    Not exactly sure if this is something you're looking for but I posted an example of working with broken rules for nested child objects. I do not handle the lazy loading case but check it out and hopefully it helps.

    John
  •  07-22-2008, 6:03 PM 24869 in reply to 24856

    Re: RE: General strategy for reporting broken rules from child object via root?

    Thanks! I think this will be very helpful.

    I solved the lazy loading dilemma using a new attribute that indicates if the property lazy-loads the collection. Unless the attribute is present and explicitly set to false, I don't recurse into that property when enumerating broken rules. I'd prefer a more general solution, but I can make this work.

    Randy

     

  •  07-23-2008, 8:19 AM 24879 in reply to 24869

    Re: RE: General strategy for reporting broken rules from child object via root?

    I posted some code for how I do this a long time ago.

    The key is that the list of "owned objects" (and collections) is filled by traversing the Fields not the Properties. This effectively bypasses any lazy loaded properties.

    Joe

     

  •  07-23-2008, 8:22 PM 24902 in reply to 24879

    Re: RE: General strategy for reporting broken rules from child object via root?

    I can certainly do this, but I didn't know if there were trust issues since the fields were private. Of course, .NET could be smart enough to let an object reflect on its own private fields, but I would think private fields of child objects could present a trust issue.

    (I'm a newbie at trust issues, though, so this is speculation and it would be nice if someone new definitively whether this was a problem)

View as RSS news feed in XML

Please contact Magenic for your .NET consulting and CSLA .NET mentoring needs.
Please consider making a donation to help support the ongoing development of CSLA .NET.

Make donation through PayPal - it's fast, free and secure!
Why donate?
Powered by Community Server, by Telligent Systems