Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stores use a static IMongoRepository #51

Closed
jhadwen opened this issue Jan 27, 2022 · 0 comments
Closed

Stores use a static IMongoRepository #51

jhadwen opened this issue Jan 27, 2022 · 0 comments

Comments

@jhadwen
Copy link
Contributor

jhadwen commented Jan 27, 2022

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant