Alexander Trauzzi
Alexander Trauzzi

Reputation: 7396

Composing IObservables and cleaning up after registrations

I have some code in a class that takes FileSystemWatcher events and flattens them into an event in my domain:

(Please note, the *AsObservable methods are extensions from elsewhere in my project, they do what they say 🙂.)

            watcher = new FileSystemWatcher(ConfigurationFilePath);

            ChangeObservable = Observable
                .Merge(
                    watcher.ChangedAsObservable().Select((args) =>
                    {
                        return new ConfigurationChangedArgs
                        {
                            Type = ConfigurationChangeType.Edited,
                        };
                    }),
                    watcher.DeletedAsObservable().Select((args) =>
                    {
                        return new ConfigurationChangedArgs
                        {
                            Type = ConfigurationChangeType.Deleted,
                        };
                    }),
                    watcher.RenamedAsObservable().Select((args) => 
                    {
                        return new ConfigurationChangedArgs
                        {
                            Type = ConfigurationChangeType.Renamed,
                        };
                    })
                );

            ChangeObservable.Subscribe((args) =>
            {
                Changed.Invoke(this, args);
            });

Something that I'm trying to wrap my head around as I'm learning are best practices around naming, ownership and cleanup of the IObservable and IDisposable returned by code like this.

So, some specific questions:

Other tips and advice always appreciated! 😀


Upvotes: 1

Views: 49

Answers (1)

Shlomo
Shlomo

Reputation: 14350

Is it okay to leak IObservables from a class that creates them? For example, is the property I'm assigning this chain to okay to be public?

Yes.

Does the property name ChangeObservable align with what most people would consider best practice when using the .net reactive extensions?

Subjective question. Maybe FileChanges? The fact that it's an observable is clear from the type.

Do I need to call Dispose on any of my subscriptions to this chain, or is it safe enough to leave everything up to garbage collection when the containing class goes out of scope?

The ChangeObservable.Subscribe at the end could live forever, preventing the object from being garbage collected if the event is subscribed to, though that could also be your intention. Operator subscriptions are generally fine. I can't see the code for your ChangedAsObservable like functions. If they don't include a Subscribe or an event subscription, they're probably fine as well.

Keep in mind, I'm observing events from watcher, so there's some shared lifecycle there.

Since FileWatcher implements IDisposable, you should probably use Observable.Using around it so you can combine the lifecycles.

Is it okay to take an observable and wire them into an event on my own class (Changed in the example above), or is the idea to stay out of the native .net event system and leak my IObservable?

I would prefer to stay in Rx. The problem with event subscriptions is that they generally live forever. You lose the ability to control subscription lifecycle. They're also feel so much more primitive. But again, that's a bit subjective.

Upvotes: 3

Related Questions