Vibrant discussion about CSLA .NET and using the framework to build great business applications.

Forum has moved

New location: CSLA .NET forum

CSLA .NET Resources:
  • CSLA .NET forum
  • CSLA .NET home page
  • Single Child Object MarkAsDeleted returns New Object

    rated by 0 users
    Answered (Not Verified) This post has 0 verified answers | 1 Reply | 1 Follower

    Top 150 Contributor
    60 Posts
    Jean posted on Wed, Aug 13 2014 11:56 AM

    I've been reading on Child Object on this forum and I didn't see an exact match for my issue, here it is

    I have a 1:1 relationship object 

    In Object1 I have this

    private static readonly PropertyInfo<IObject2> Object2Property = RegisterProperty<IObject2Property>(p => p.Object2Property, RelationshipTypes.Child);

    public IObject2 Object2
       get { return GetProperty(Object2Property ); }
       set { SetProperty(Object2Property , value); }

    The issue here is that Object2 can be set to Null when another field is set to 0 using a business Rule like 

     if (this.OnOrderQty > 0)
         var newObject2 = new Object2();
         newObject2.Property1 = this.OnOrderQty;

          context.AddOutValue(Object2Property, newObject2);
         if (Object2 != null)
             context.AddOutValue(Object2Property, object2);


    The problem I'm having is

    If I leave the code this way when I save Object1 and Object2 is MarkAsDelete, it does delete the Object2 (Calls the Delete function of Object2) but It when it returns to the UI (WinRT) Object2 is still there and the isNew is set to true

    What I would like is the object to be deleted and set to Null so Object1.Object2 would return null not a new Object2 with the same properties that the one i Just deleted.


    Am I doing something wrong here?


    All Replies

    Top 10 Contributor
    2,279 Posts
    Answered (Not Verified) JonnyBee replied on Wed, Aug 13 2014 11:54 PM
    Suggested by JonnyBee

    Yes, you are doing something wrong. 

    My preferred solution is to override and add some logic to CanReadProperty so that the user is not allowed to read the value when a certain condition happens. 

    This will make GetProperty return the default value for the type (ie: NULL) while you still retain the original object in the underlying object structure. 

    Next I would override Save/SaveAsync (depending on which version of CSLA) to mark the Child2 for delete if the given condition exists. This gives the most flexible solution that the user may edit the properties back and forth - and only when the user wants to save is when your code should mark the Child2 object for Delete. 

    So the key concepts:

    • do not "destroy" the underlying object structure 
    • use authorization (business) logic to mask away the underlying object 
    • only mark for delete when user actually says he/she is finished and wants to save. 

    Jonny Bekkum, Norway CslaContrib Coordinator

    Page 1 of 1 (2 items) | RSS

    Copyright (c) 2006-2014 Marimer LLC. All rights reserved.
    Email admin@lhotka.net for support.
    Powered by Community Server (Non-Commercial Edition), by Telligent Systems