MarimerLLC / csla

A home for your business logic in any .NET application.
https://cslanet.com
MIT License
1.23k stars 394 forks source link

Blazor ViewModel with ManagedObjectLifetime save issue #4026

Closed russblair closed 2 weeks ago

russblair commented 2 weeks ago

Issue When a business object is changed and saved any subsequent change that is undone will revert to the original fetched value not the last saved value. Steps to reproduce: 1) Using the Blazor ViewModel with the ManageObjectLifetime set to true 2) Call the ViewModel RefreshAsync to Fetch an EditableRoot model 3) Make a change to a property 4) Save the model 5) Make a second change to a property 6) Cancel the change 7) Note that the model property has reverted to the original value and not the value after the save in step 4

Version and Platform CSLA version: 7.x OS: Windows Platform: Blazor

Problems 1) We discovered that the ViewModel code performs the ApplyEdit on the clone and not on the model. 2) Since the OnModelChanging is not called because the references are the same, the BeginEdit is not called.

Solution 1) Created unit test SaveThenCancel_ValidatePropertyValue to reproduce the issue 2) We moved the ApplyEdit from after the clone to before the clone so that it is applied to the model. 3) Added logic to the finally block of the save to call BeginEdit if the ManagedObjectLifetime is true and the EditLevel is zero. 4) SaveThenCancel_ValidatePropertyValue unit test now passes

rockfordlhotka commented 2 weeks ago

@russblair This will need to be applied to v8.x and main as well.

russblair commented 2 weeks ago

@rockfordlhotka - During the process of creating PRs for v8.x and main I discovered 2 fixes that need to be applied to v7.x. The first one is on BusinessBase.SaveAsync and will swallow exceptions and the second one only affect unit tests.