Single Child Object MarkAsDeleted returns New Object

Single Child Object MarkAsDeleted returns New Object

Old forum URL: forums.lhotka.net/forums/t/12720.aspx


jteneglio posted on Wednesday, August 13, 2014

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);
 }
 else
 {
     if (Object2 != null)
     {
         object2.MarkAsDeleted();
         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?

 

JonnyBee replied on Wednesday, August 13, 2014

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:

Copyright (c) Marimer LLC