-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Users/henkkin/refactoring repository di registration without configur…
…ation (#2) * Fixed di for IRepository withour registering it for every EntityType Issue: when having multiple dbcontext, in the SqlServerRepository it is resolving multiple dbcontext until the right one is found (unnessessary overhead) * Fixed di for IRepository withour registering it for every EntityType Issue: when having multiple dbcontext, in the SqlServerRepository it is resolving multiple dbcontext until the right one is found (unnessessary overhead) * Made entitybehavior serviceproviders not required Added SqlServerDbContextForEntityResolver to resolve DbContext for the Entity type Removed ConfigureEntityTypes option * Removed unneeded locking and caching of DbContext * Updated documentation Co-authored-by: Henk Kin <[email protected]>
- Loading branch information
Showing
19 changed files
with
391 additions
and
357 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
7 changes: 7 additions & 0 deletions
7
...essClient.EntityFrameworkCore.SqlServer/Resolvers/ISqlServerDbContextForEntityResolver.cs
This file contains 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 |
---|---|---|
@@ -0,0 +1,7 @@ | ||
namespace DataAccessClient.EntityFrameworkCore.SqlServer.Resolvers | ||
{ | ||
internal interface ISqlServerDbContextForEntityResolver | ||
{ | ||
SqlServerDbContext Execute<TEntity>() where TEntity : class; | ||
} | ||
} |
61 changes: 61 additions & 0 deletions
61
...cessClient.EntityFrameworkCore.SqlServer/Resolvers/SqlServerDbContextForEntityResolver.cs
This file contains 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 |
---|---|---|
@@ -0,0 +1,61 @@ | ||
using System; | ||
using System.Linq; | ||
using Microsoft.EntityFrameworkCore; | ||
|
||
namespace DataAccessClient.EntityFrameworkCore.SqlServer.Resolvers | ||
{ | ||
internal class SqlServerDbContextForEntityResolver : ISqlServerDbContextForEntityResolver | ||
{ | ||
private readonly IServiceProvider _scopedServiceProvider; | ||
|
||
public SqlServerDbContextForEntityResolver(IServiceProvider scopedServiceProvider) | ||
{ | ||
_scopedServiceProvider = scopedServiceProvider; | ||
} | ||
|
||
public SqlServerDbContext Execute<TEntity>() where TEntity : class | ||
{ | ||
Type dbContextType = | ||
SqlServerDbContext.RegisteredEntityTypesPerDbContexts.Where(c => | ||
c.Value.Any(entityType => entityType == typeof(TEntity))).Select(x => x.Key).SingleOrDefault(); | ||
|
||
SqlServerDbContext dbContext = null; | ||
if (dbContextType != null) | ||
{ | ||
dbContext = ResolveDbContextInstance<TEntity>(_scopedServiceProvider, dbContextType); | ||
} | ||
|
||
if (dbContext == null) | ||
{ | ||
foreach (var registeredDbContextType in SqlServerDbContext.RegisteredDbContextTypes) | ||
{ | ||
dbContext = ResolveDbContextInstance<TEntity>(_scopedServiceProvider, registeredDbContextType); | ||
|
||
if (dbContext != null) | ||
{ | ||
break; | ||
} | ||
} | ||
} | ||
|
||
return dbContext; | ||
} | ||
|
||
private SqlServerDbContext ResolveDbContextInstance<TEntity>(IServiceProvider scopedServiceProvider, Type dbContextType) where TEntity : class | ||
{ | ||
var dbContextResolverType = typeof(ISqlServerDbContextResolver<>).MakeGenericType(dbContextType); | ||
var executeMethod = | ||
dbContextResolverType.GetMethod(nameof(ISqlServerDbContextResolver<SqlServerDbContext>.Execute)); | ||
var dbContext = | ||
executeMethod?.Invoke(scopedServiceProvider.GetService(dbContextResolverType), new object[0]) as | ||
SqlServerDbContext ?? | ||
throw new ArgumentNullException(nameof(SqlServerDbContext)); | ||
if (dbContext.Model.FindEntityType(typeof(TEntity)) != null) | ||
{ | ||
return dbContext; | ||
} | ||
|
||
return null; | ||
} | ||
} | ||
} |
Oops, something went wrong.