Skip to content

Commit

Permalink
Merge pull request #101 from nventive/dev/jpl/fix
Browse files Browse the repository at this point in the history
fix: Don't throw ObjectDisposedException when calling RaisePropertyCh…
  • Loading branch information
jeanplevesque authored Jun 18, 2024
2 parents 7b2eb5b + 24b7c98 commit f49027a
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 f49027a

Please sign in to comment.