-
-
Notifications
You must be signed in to change notification settings - Fork 10
Refactor MetadataProvider to improve table metadata retrieval logic #81
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
Merged
+52
−50
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
102 changes: 52 additions & 50 deletions
102
src/PhenX.EntityFrameworkCore.BulkInsert/Metadata/MetadataProvider.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,50 +1,52 @@ | ||
| using Microsoft.EntityFrameworkCore; | ||
| using Microsoft.EntityFrameworkCore.Infrastructure; | ||
|
|
||
| using PhenX.EntityFrameworkCore.BulkInsert.Abstractions; | ||
|
|
||
| namespace PhenX.EntityFrameworkCore.BulkInsert.Metadata; | ||
|
|
||
| internal sealed class MetadataProvider | ||
| { | ||
| private Dictionary<Type, Dictionary<Type, TableMetadata>> _tablesPerContext = new(); | ||
|
|
||
| public TableMetadata GetTableInfo<T>(DbContext context) | ||
| { | ||
| var tables = GetTables(context); | ||
|
|
||
| if (!tables.TryGetValue(typeof(T), out var table)) | ||
| { | ||
| throw new InvalidOperationException($"Cannot find metadata for type '{typeof(T)}'."); | ||
| } | ||
|
|
||
| return table; | ||
| } | ||
|
|
||
| private Dictionary<Type, TableMetadata> GetTables(DbContext context) | ||
| { | ||
| lock (_tablesPerContext) | ||
| { | ||
| var type = context.GetType(); | ||
| if (_tablesPerContext.TryGetValue(context.GetType(), out var tables)) | ||
| { | ||
| return tables; | ||
| } | ||
|
|
||
| var provider = context.GetService<IBulkInsertProvider>(); | ||
|
|
||
| tables = context.Model.GetEntityTypes() | ||
| // Filter out entities without an associated table | ||
| // See also https://learn.microsoft.com/en-us/ef/core/modeling/keyless-entity-types | ||
| .Where(x => x.GetTableName() is not null) | ||
| .GroupBy(x => x.ClrType) | ||
| .ToDictionary( | ||
| x => x.Key, | ||
| x => new TableMetadata(x.First(), provider.SqlDialect)); | ||
|
|
||
| _tablesPerContext[type] = tables; | ||
|
|
||
| return tables; | ||
| } | ||
| } | ||
| } | ||
| using Microsoft.EntityFrameworkCore; | ||
| using Microsoft.EntityFrameworkCore.Infrastructure; | ||
|
|
||
| using PhenX.EntityFrameworkCore.BulkInsert.Abstractions; | ||
|
|
||
| namespace PhenX.EntityFrameworkCore.BulkInsert.Metadata; | ||
|
|
||
| internal sealed class MetadataProvider | ||
| { | ||
| private readonly Dictionary<Type, Dictionary<Type, TableMetadata>> _tablesPerContext = new(); | ||
|
|
||
| public TableMetadata GetTableInfo<T>(DbContext context) | ||
| { | ||
| lock (_tablesPerContext) | ||
| { | ||
| var type = context.GetType(); | ||
|
|
||
| if (!_tablesPerContext.TryGetValue(type, out var tables)) | ||
| { | ||
| tables = new Dictionary<Type, TableMetadata>(); | ||
| _tablesPerContext[type] = tables; | ||
| } | ||
|
|
||
| var modelType = typeof(T); | ||
|
|
||
| if (tables.TryGetValue(modelType, out var table)) | ||
| { | ||
| return table; | ||
| } | ||
|
|
||
| var entityType = context.Model.FindEntityType(modelType); | ||
| if (entityType == null) | ||
| { | ||
| throw new InvalidOperationException($"The type '{modelType.FullName}' is not part of the model for the current context."); | ||
| } | ||
|
|
||
| // Filter out entities without an associated table | ||
| // See also https://learn.microsoft.com/en-us/ef/core/modeling/keyless-entity-types | ||
| if (entityType.GetTableName() is null) | ||
| { | ||
| throw new InvalidOperationException($"The type '{modelType.FullName}' is not mapped to a table in the database or is keyless."); | ||
| } | ||
|
|
||
| var provider = context.GetService<IBulkInsertProvider>(); | ||
|
|
||
| var tableMetadata = new TableMetadata(entityType, provider.SqlDialect); | ||
| tables[modelType] = tableMetadata; | ||
|
|
||
| return tableMetadata; | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
IBulkInsertProviderservice is retrieved inside the lock on every cache miss. Consider moving this retrieval outside the lock (after checking for cache miss but before the lock, or after the model type check) to reduce lock contention, asGetService<T>()is typically thread-safe.