You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
If a store is used (User/Role etc) then the IMongoRepository repository is assigned to a static variable in "lazy assignment" pattern. Once that assignment is made every future instance of the store (e.g. in a brand new WebHost) will have the same initial database connection, regardless of what context is passed into the initializer (in the Startup). Basically meaning that it is impossible to switch connections, or create multiple webhosts with different connections.
Solution seems relatively trivial to change MongoRepository (and Context because the same paradigm) to member variables.
private readonly object _mongoRepositoryInitializationLock = new object(); private IMongoRepository _mongoRepository; private IMongoRepository MongoRepository { get { // double checked locking to prevent race to initialize the repository in multithreaded environment. if (_mongoRepository == null) { lock (_mongoRepositoryInitializationLock) { if (_mongoRepository == null) { _mongoRepository = new MongoRepository(Context); } } } return _mongoRepository; } }
I'm not even sure that MongoRepository is that expensive to create to warrant this pattern anyway.
Thanks
James
The text was updated successfully, but these errors were encountered:
Hi,
If a store is used (User/Role etc) then the IMongoRepository repository is assigned to a static variable in "lazy assignment" pattern. Once that assignment is made every future instance of the store (e.g. in a brand new WebHost) will have the same initial database connection, regardless of what context is passed into the initializer (in the Startup). Basically meaning that it is impossible to switch connections, or create multiple webhosts with different connections.
Solution seems relatively trivial to change MongoRepository (and Context because the same paradigm) to member variables.
private readonly object _mongoRepositoryInitializationLock = new object(); private IMongoRepository _mongoRepository; private IMongoRepository MongoRepository { get { // double checked locking to prevent race to initialize the repository in multithreaded environment. if (_mongoRepository == null) { lock (_mongoRepositoryInitializationLock) { if (_mongoRepository == null) { _mongoRepository = new MongoRepository(Context); } } } return _mongoRepository; } }
I'm not even sure that MongoRepository is that expensive to create to warrant this pattern anyway.
Thanks
James
The text was updated successfully, but these errors were encountered: