/** * ## Lifetimes * * If you are using an IOC container its always useful to know the best practices around the lifetime of your objects * In general we advise folks to register their ElasticClient instances as singleton. The client is thread safe * so sharing this instance over threads is ok. * Zooming in however the actual moving part that benefits the most of being static for most of the duration of your * application is ConnectionSettings. Caches are per ConnectionSettings. * In some applications it could make perfect sense to have multiple singleton IElasticClient's registered with different * connectionsettings. e.g if you have 2 functionally isolated Elasticsearch clusters. */ [U] public void InitialDisposeState() { var connection = new AConnection(); var connectionPool = new AConnectionPool(new Uri("http://localhost:9200")); var settings = new AConnectionSettings(connectionPool, connection); settings.IsDisposed.Should().BeFalse(); connectionPool.IsDisposed.Should().BeFalse(); connection.IsDisposed.Should().BeFalse(); }
/** * `ConnectionSettings`, `IConnectionPool` and `IConnection` all explicitly implement `IDisposable` */ [U] public void InitialDisposeState() { var connection = new AConnection(); var connectionPool = new AConnectionPool(new Uri("http://localhost:9200")); var settings = new AConnectionSettings(connectionPool, connection); settings.IsDisposed.Should().BeFalse(); connectionPool.IsDisposed.Should().BeFalse(); connection.IsDisposed.Should().BeFalse(); }
/** * Disposing the ConnectionSettings will dispose the IConnectionPool and IConnection it has a hold of */ [U] public void DisposingSettingsDisposesMovingParts() { var connection = new AConnection(); var connectionPool = new AConnectionPool(new Uri("http://localhost:9200")); var settings = new AConnectionSettings(connectionPool, connection); using (settings) { } settings.IsDisposed.Should().BeTrue(); connectionPool.IsDisposed.Should().BeTrue(); connection.IsDisposed.Should().BeTrue(); }
/** * Disposing an instance of `ConnectionSettings` will also dispose the `IConnectionPool` and `IConnection` it uses */ [U] public void DisposingSettingsDisposesMovingParts() { var connection = new AConnection(); var connectionPool = new AConnectionPool(new Uri("http://localhost:9200")); var settings = new AConnectionSettings(connectionPool, connection); using (settings) { } // <1> force the settings to be disposed settings.IsDisposed.Should().BeTrue(); connectionPool.IsDisposed.Should().BeTrue(); connection.IsDisposed.Should().BeTrue(); }