Skip to content

Commit

Permalink
fix: Don't throw ObjectDisposedException when calling RaisePropertyCh…
Browse files Browse the repository at this point in the history
…anged on a disposed VM to align with the RaisePropertyChangedInner behavior.
  • Loading branch information
jeanplevesque committed Jun 18, 2024
1 parent 7b2eb5b commit 24b7c98
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
18 changes: 17 additions & 1 deletion src/DynamicMvvm.Tests/Integration/IntegrationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,23 @@ public void A_disposed_VM_cannot_be_mutated()
Assert.Throws<ObjectDisposedException>(() => viewModel.SetErrors(errors: new Dictionary<string, IEnumerable<object>>()));
Assert.Throws<ObjectDisposedException>(() => viewModel.ClearErrors(nameof(MyViewModel.Counter)));
Assert.Throws<ObjectDisposedException>(() => viewModel.Dispatcher = new TestDispatcher());
Assert.Throws<ObjectDisposedException>(() => viewModel.RaisePropertyChanged(nameof(MyViewModel.Counter)));
}

[Fact]
public void A_disposed_VM_does_not_raise_PropertyChanged()
{
// Arrange
var viewModel = new MyViewModel(_serviceProvider);

var raised = false;
viewModel.PropertyChanged += (s, e) => raised = true;

// Act
viewModel.Dispose();
viewModel.Counter++;

// Assert
raised.Should().BeFalse();
}

[Fact]
Expand Down
8 changes: 6 additions & 2 deletions src/DynamicMvvm/ViewModel/ViewModelBase.PropertyChanged.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,18 @@ public partial class ViewModelBase
/// <inheritdoc />
public virtual void RaisePropertyChanged(string propertyName)
{
ThrowIfDisposed();

if (_isDisposing)
{
_logger.LogViewModelSkippedMethodBecauseDisposing_PropertyName(nameof(RaisePropertyChanged), GetType().Name, propertyName, Name);
return;
}

if (_isDisposed)
{
_logger.LogViewModelSkippedMethodBecauseDisposed_PropertyName(nameof(RaisePropertyChanged), GetType().Name, propertyName, Name);
return;
}

if (PropertyChanged == null)
{
return;
Expand Down

0 comments on commit 24b7c98

Please sign in to comment.