Class MongoDBWatcher
- Namespace
- Cratis.Arc.MongoDB
- Assembly
- Cratis.Arc.MongoDB.dll
Represents an implementation of IMongoDBWatcher that maintains a single change stream connection per database per process.
[Singleton]
public class MongoDBWatcher : IMongoDBWatcher, IDisposable
- Inheritance
-
MongoDBWatcher
- Implements
- Inherited Members
- Extension Methods
Remarks
The watch is partitioned by the resolved database name so a multi-tenant application — where the name resolver appends the tenant to the database name — gets one change stream per tenant database rather than sharing the first caller's. A single shared watcher that cached one database served every tenant the first tenant's data. With connection resolution the watch is also partitioned by server, since two tenants may resolve to databases of the same name on different servers.
Constructors
MongoDBWatcher(IMongoDBClientFactory, IMongoDatabaseNameResolver, ILogger<MongoDBWatcher>, IMongoDBConnections?)
Represents an implementation of IMongoDBWatcher that maintains a single change stream connection per database per process.
public MongoDBWatcher(IMongoDBClientFactory clientFactory, IMongoDatabaseNameResolver databaseNameResolver, ILogger<MongoDBWatcher> logger, IMongoDBConnections? connections = null)
Parameters
clientFactoryIMongoDBClientFactoryThe IMongoDBClientFactory for creating MongoDB clients.
databaseNameResolverIMongoDatabaseNameResolverThe IMongoDatabaseNameResolver for resolving the database name.
loggerILogger<MongoDBWatcher>The logger.
connectionsIMongoDBConnectionsOptional IMongoDBConnections, so watches follow configured connection resolution.
Remarks
The watch is partitioned by the resolved database name so a multi-tenant application — where the name resolver appends the tenant to the database name — gets one change stream per tenant database rather than sharing the first caller's. A single shared watcher that cached one database served every tenant the first tenant's data. With connection resolution the watch is also partitioned by server, since two tenants may resolve to databases of the same name on different servers.
Methods
Dispose()
public void Dispose()
GetChanges(string)
Gets the changes of the shared change stream for a database, starting the stream when it is not already open.
public IObservable<ChangeStreamDocument<BsonDocument>> GetChanges(string databaseName)
Parameters
databaseNamestringName of the database whose changes to get.
Returns
- IObservable<ChangeStreamDocument<BsonDocument>>
A hot IObservable<T> of every change in the database, across all of its collections.
Remarks
Every subscription to one database shares a single change stream, so subscribing costs no database connection. A subscriber sees only changes that arrive after it subscribed.
Observe<TDocument>(Expression<Func<TDocument, bool>>?)
Start observing a collection using the shared database-level change stream.
public IMongoDBObserveBuilder<TDocument> Observe<TDocument>(Expression<Func<TDocument, bool>>? filter = null)
Parameters
filterExpression<Func<TDocument, bool>>Optional filter expression for the documents to observe.
Returns
- IMongoDBObserveBuilder<TDocument>
A IMongoDBObserveBuilder<TDocument> for chaining joins and finalizing the observation.
Type Parameters
TDocumentType of document to observe.