Skip to content

Fix Race conditions, thread-safety issues, and code quality in Developer Balance sample#751

Draft
Dhivya-SF4094 wants to merge 5 commits intodotnet:mainfrom
Dhivya-SF4094:fix-748
Draft

Fix Race conditions, thread-safety issues, and code quality in Developer Balance sample#751
Dhivya-SF4094 wants to merge 5 commits intodotnet:mainfrom
Dhivya-SF4094:fix-748

Conversation

@Dhivya-SF4094
Copy link
Copy Markdown

Note

Are you waiting for the changes in this PR to be merged?
It would be very helpful if you could test the resulting artifacts from this PR and let us know in a comment if this change resolves your issue. Thank you!

This pull request introduces several important improvements to the data access layer and UI navigation logic of the DeveloperBalance application. The main focus is on making database initialization and table management thread-safe, optimizing queries to reduce database load, and improving code consistency and safety in navigation commands.

Description of Change

  • Updated ClearTables() from async void to async Task and ensured it is properly awaited in LoadSeedDataAsync() to maintain correct execution order during seed initialization.

  • Modified table drop logic in SeedDataService.ClearTables() to execute sequentially instead of using Task.WhenAll(). Now only _projectRepository.DropTableAsync() and _categoryRepository.DropTableAsync() are called, ensuring dependent tables are dropped exactly once.

  • Renamed incorrectly named file TaskRespository.cs to TaskRepository.cs to align with class naming and maintain repository naming consistency.

  • Introduced SemaphoreSlim _initLock in all repositories and implemented a double-check locking pattern in Init() to ensure thread-safe initialization. Updated DropTableAsync() methods to execute under the same lock to prevent concurrent reset conditions.

  • Optimized ProjectRepository.ListAsync() by replacing per-project queries with bulk retrieval of tasks and tags, followed by in-memory grouping and assignment to projects.

  • Removed unused ProjectsTags model class (Models/ProjectsTags.cs) that was not referenced anywhere in the codebase.

  • Corrected SQL parameter usage in TagRepository.ListAsync(int projectID) by adding the missing @ prefix to ProjectID.

  • Updated NavigateToProject to return Task.CompletedTask when the project is null, ensuring consistent asynchronous behavior.

  • Added missing namespace declaration (namespace DeveloperBalance.Data;) to JsonContext.cs to align with project structure and maintain consistency.

  • Updated DisplaySnackbarAsync and DisplayToastAsync to use using var with CancellationTokenSource, ensuring proper disposal of resources.

These changes collectively make the application more robust, efficient, and maintainable.

Issue Fixes
Fixes #748

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

Successfully merging this pull request may close these issues.

Race conditions, thread-safety issues, and code quality in Developer Balance sample

1 participant