Skip to content

Instantly share code, notes, and snippets.

@molnard
Last active March 22, 2019 20:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save molnard/27f3e3eae3eb8928ee93c7d980f6e4f6 to your computer and use it in GitHub Desktop.
Save molnard/27f3e3eae3eb8928ee93c7d980f6e4f6 to your computer and use it in GitHub Desktop.

IDisposable pattern

public class MyClass : IDisposable

#region IDisposable Support

	private volatile bool _disposedValue = false; // To detect redundant calls

	protected virtual void Dispose(bool disposing)
	{
		if (!_disposedValue)
		{
			if (disposing)
			{
				// TODO: dispose managed state (managed objects).
			}

			// TODO: free unmanaged resources (unmanaged objects) and override a finalizer below.
			// TODO: set large fields to null.

			_disposedValue = true;
		}
	}

	public void Dispose()
	{
		Dispose(true);
	}

#endregion IDisposable Support

CompositeDisposable

Instead of object.Dispose() use Disposable.Add(object)

Declaration

public MyClass
{
	private CompositeDisposable Disposables { get; } = new CompositeDisposable();

When disposing the object

    Disposables?.Dispose();

DisposeWith

if you subscribe to class members DO NOT USE DisposeWith(Disposables)

this.WhenAnyValue(x => x).Subscribe(_ =>
{

});

any other subscripiton must be disposed

model.WhenAnyValue(x => x).Subscribe(_ =>
{

}).DisposeWith(Disposables);

Event subsciptions

traditional pattern DO NOT USE

Global.ChaumianClient.StateUpdated += ChaumianClient_StateUpdated;

if (Global.ChaumianClient != null)
{
	Global.ChaumianClient.StateUpdated -= ChaumianClient_StateUpdated;
}

the RX style

Observable.FromEventPattern(CoinList, nameof(CoinList.SelectionChanged)).Subscribe(_ => SetFeesAndTexts());

Observable.FromEventPattern(
	Global.ChaumianClient,
	nameof(Global.ChaumianClient.StateUpdated))
	.ObserveOn(RxApp.MainThreadScheduler)
	.Subscribe(_ =>
	{
		RefreshSmartCoinStatus();
	}).DisposeWith(_disposables);

Property definition and notifychange

WRONG!

private void Coin_PropertyChanged(object sender, PropertyChangedEventArgs e)
{			
	if (e.PropertyName == nameof(CoinViewModel.Unspent))	
	{	
	}
}

USE PropertyHelper!

//define class member
private ObservableAsPropertyHelper<bool> _confirmed;

//initilize PropertyHelper
_confirmed = Model.WhenAnyValue(x => x.Confirmed).ToProperty(this, x => x.Confirmed).DisposeWith(_disposables);

//if invoked from other thread 
_confirmed = Model.WhenAnyValue(x => x.Confirmed).ToProperty(this, x => x.Confirmed, scheduler:RxApp.MainThreadScheduler).DisposeWith(_disposables);

//if we need to do something on change
this.WhenAnyValue(x => x.Confirmed, x => x.CoinJoinInProgress, x => x.Confirmations).Subscribe(_ => RefreshSmartCoinStatus());

Task.Run(async () => { while (true) { if (_notificationQueue.Any() && !_timer.IsEnabled) { var notification = _notificationQueue.Dequeue();

					await Dispatcher.UIThread.InvokeAsync(()=>DisplayNotification(notification));
				}
				await Task.Delay(TimeSpan.FromSeconds(0.1));
			}
		});
@danwalmsley
Copy link

Can I suggest an order to classes (at least viewmodels)

  1. private fields.
  2. ctor / finalizer
  3. properties
  4. commands
  5. public methods
  6. private methods

?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment