Skip to content

Commit

Permalink
Users/henkkin/refactoring repository di registration without configur…
Browse files Browse the repository at this point in the history
…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
HenkKin and Henk Kin authored Mar 7, 2020
1 parent f6fc5e5 commit 37be544
Show file tree
Hide file tree
Showing 19 changed files with 391 additions and 357 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ public void WithDbContextPooling_WhenHavingNestedChildScopes_ItShouldResolveDbCo
serviceCollection.AddDataAccessClient<MyDbContext>(
conf => conf
.UsePooling(true)
.ConfigureEntityTypes(new[] {typeof(Entity)})
.ConfigureDbContextOptions(builder => builder
.UseInMemoryDatabase(nameof(WithDbContextPooling_WhenHavingNestedChildScopes_ItShouldResolveDbContextPerChildScope))
)
Expand All @@ -49,7 +48,6 @@ public void WithoutDbContextPooling_WhenHavingChildScope_ItShouldResolveDbContex
serviceCollection.AddDataAccessClient<MyDbContext>(
conf => conf
.UsePooling(false)
.ConfigureEntityTypes(new[] { typeof(Entity) })
.ConfigureDbContextOptions(builder => builder
.UseInMemoryDatabase(nameof(WithoutDbContextPooling_WhenHavingChildScope_ItShouldResolveDbContextPerChildScope))
)
Expand All @@ -71,7 +69,6 @@ public void WithDbContextPooling_WhenMultipleDbContextAreResolvedFromSameScope_I
serviceCollection.AddDataAccessClient<MyDbContext>(
conf => conf
.UsePooling(true)
.ConfigureEntityTypes(new[] { typeof(Entity) })
.ConfigureDbContextOptions(builder => builder
.UseInMemoryDatabase(nameof(WithDbContextPooling_WhenMultipleDbContextAreResolvedFromSameScope_ItShouldReturnSameDbContextInstance))
)
Expand All @@ -92,7 +89,6 @@ public void WithoutDbContextPooling_WhenMultipleDbContextAreResolvedFromSameScop
serviceCollection.AddDataAccessClient<MyDbContext>(
conf => conf
.UsePooling(false)
.ConfigureEntityTypes(new[] { typeof(Entity) })
.ConfigureDbContextOptions(builder => builder
.UseInMemoryDatabase(nameof(WithoutDbContextPooling_WhenMultipleDbContextAreResolvedFromSameScope_ItShouldReturnSameDbContextInstance))
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ public DbContextTestBase(string testName)
serviceCollection.AddDataAccessClient<TestDbContext>(
conf => conf
.UsePooling(true)
.ConfigureEntityTypes(new[] { typeof(TestEntity), typeof(TestEntityTranslation), typeof(TestEntityView) })
.ConfigureDbContextOptions(builder => builder
.UseInMemoryDatabase(testName).EnableSensitiveDataLogging().EnableDetailedErrors()
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ public void WhenWorkIsDoneWithCascadeTimingOnSaveChanges_ItShouldResetToCascadeT
IServiceCollection services = new ServiceCollection();

services.AddDataAccessClient<TestDbContext>(builder => builder
.ConfigureEntityTypes(new Type[]{})
.ConfigureDbContextOptions(optionsBuilder =>optionsBuilder
.UseInMemoryDatabase(
nameof(WhenWorkIsDoneWithCascadeTimingOnSaveChanges_ItShouldResetToCascadeTimingImmediate))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,13 @@ public void OnRegistering(IServiceCollection serviceCollection)

public Dictionary<string, dynamic> OnExecutionContextCreating(IServiceProvider scopedServiceProvider)
{
var userIdentifierProvider = scopedServiceProvider.GetRequiredService<IUserIdentifierProvider<TUserIdentifierType>>();
var userIdentifierProvider = scopedServiceProvider.GetService<IUserIdentifierProvider<TUserIdentifierType>>();

var context = new Dictionary<string, dynamic>
var context = new Dictionary<string, dynamic>();
if (userIdentifierProvider != null)
{
{typeof(IUserIdentifierProvider<TUserIdentifierType>).Name, userIdentifierProvider},
};
context.Add(typeof(IUserIdentifierProvider<TUserIdentifierType>).Name, userIdentifierProvider);
}

return context;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,18 @@ public void OnRegistering(IServiceCollection serviceCollection)

public Dictionary<string, dynamic> OnExecutionContextCreating(IServiceProvider scopedServiceProvider)
{
var localeIdentifierProvider = scopedServiceProvider.GetRequiredService<ILocaleIdentifierProvider<TLocaleIdentifierType>>();
var localizationConfiguration = scopedServiceProvider.GetRequiredService<ILocalizationConfiguration>();
var localeIdentifierProvider = scopedServiceProvider.GetService<ILocaleIdentifierProvider<TLocaleIdentifierType>>();
var localizationConfiguration = scopedServiceProvider.GetService<ILocalizationConfiguration>();

var context = new Dictionary<string, dynamic>
var context = new Dictionary<string, dynamic>();
if (localeIdentifierProvider != null)
{
{typeof(ILocaleIdentifierProvider<TLocaleIdentifierType>).Name, localeIdentifierProvider},
{typeof(ILocalizationConfiguration).Name, localizationConfiguration},
};
context.Add(typeof(ILocaleIdentifierProvider<TLocaleIdentifierType>).Name, localeIdentifierProvider);
}
if (localizationConfiguration != null)
{
context.Add(typeof(ILocalizationConfiguration).Name, localizationConfiguration);
}

return context;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,13 @@ public void OnRegistering(IServiceCollection serviceCollection)

public Dictionary<string, dynamic> OnExecutionContextCreating(IServiceProvider scopedServiceProvider)
{
var userIdentifierProvider = scopedServiceProvider.GetRequiredService<IUserIdentifierProvider<TUserIdentifierType>>();
var userIdentifierProvider = scopedServiceProvider.GetService<IUserIdentifierProvider<TUserIdentifierType>>();

var context = new Dictionary<string, dynamic>
var context = new Dictionary<string, dynamic>();
if (userIdentifierProvider != null)
{
{typeof(IUserIdentifierProvider<TUserIdentifierType>).Name, userIdentifierProvider},
};
context.Add(typeof(IUserIdentifierProvider<TUserIdentifierType>).Name, userIdentifierProvider);
}

return context;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,18 @@ public void OnRegistering(IServiceCollection serviceCollection)

public Dictionary<string, dynamic> OnExecutionContextCreating(IServiceProvider scopedServiceProvider)
{
var userIdentifierProvider = scopedServiceProvider.GetRequiredService<IUserIdentifierProvider<TUserIdentifierType>>();
var softDeletableConfiguration = scopedServiceProvider.GetRequiredService<ISoftDeletableConfiguration>();
var userIdentifierProvider = scopedServiceProvider.GetService<IUserIdentifierProvider<TUserIdentifierType>>();
var softDeletableConfiguration = scopedServiceProvider.GetService<ISoftDeletableConfiguration>();

var context = new Dictionary<string, dynamic>
var context = new Dictionary<string, dynamic>();
if (userIdentifierProvider != null)
{
{typeof(IUserIdentifierProvider<TUserIdentifierType>).Name, userIdentifierProvider},
{typeof(ISoftDeletableConfiguration).Name, softDeletableConfiguration},
};
context.Add(typeof(IUserIdentifierProvider<TUserIdentifierType>).Name, userIdentifierProvider);
}
if (softDeletableConfiguration != null)
{
context.Add(typeof(ISoftDeletableConfiguration).Name, softDeletableConfiguration);
}

return context;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,18 @@ public void OnRegistering(IServiceCollection serviceCollection)

public Dictionary<string, dynamic> OnExecutionContextCreating(IServiceProvider scopedServiceProvider)
{
var tenantIdentifierProvider = scopedServiceProvider.GetRequiredService<ITenantIdentifierProvider<TTenantIdentifierType>>();
var multiTenancyConfiguration = scopedServiceProvider.GetRequiredService<IMultiTenancyConfiguration>();
var tenantIdentifierProvider = scopedServiceProvider.GetService<ITenantIdentifierProvider<TTenantIdentifierType>>();
var multiTenancyConfiguration = scopedServiceProvider.GetService<IMultiTenancyConfiguration>();

var context = new Dictionary<string, dynamic>
var context = new Dictionary<string, dynamic>();
if (tenantIdentifierProvider != null)
{
{typeof(ITenantIdentifierProvider<TTenantIdentifierType>).Name, tenantIdentifierProvider},
{typeof(IMultiTenancyConfiguration).Name, multiTenancyConfiguration},
};
context.Add(typeof(ITenantIdentifierProvider<TTenantIdentifierType>).Name, tenantIdentifierProvider);
}
if (multiTenancyConfiguration != null)
{
context.Add(typeof(IMultiTenancyConfiguration).Name, multiTenancyConfiguration);
}

return context;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<PackageReleaseNotes></PackageReleaseNotes>
<Copyright>Henk Kin</Copyright>
<PackageTags>EntityFrameworkCore, EF, SqlServer, DependencyInjection, Multiple DbContext support, DataAccess, Repository, UnitOfWork, SoftDelete, Translation, RowVersioning, Multi-tenancy, Filtering, Paging, Sorting, Includes</PackageTags>
<Version>0.0.5</Version>
<Version>0.0.6</Version>
<PublishRepositoryUrl>true</PublishRepositoryUrl>
<EmbedUntrackedSources>true</EmbedUntrackedSources>
<IncludeSymbols>true</IncludeSymbols>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ namespace DataAccessClient.EntityFrameworkCore.SqlServer
public class DataAccessClientOptions
{
public Action<DbContextOptionsBuilder> DbContextOptionsBuilder { get; internal set; }
public Type[] EntityTypes { get; internal set; }
public bool UsePooling { get; internal set; }
public int? PoolSize { get; internal set; }
public IList<IEntityBehaviorConfiguration> CustomEntityBehaviors { get; internal set; } = new List<IEntityBehaviorConfiguration>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,6 @@ public virtual DataAccessClientOptionsBuilder ConfigureDbContextOptions(Action<D
return this;
}

public virtual DataAccessClientOptionsBuilder ConfigureEntityTypes(Type[] entityTypes)
{
_options.EntityTypes = entityTypes;
return this;
}

public virtual DataAccessClientOptionsBuilder UsePooling(bool usePooling, int? poolSize = null)
{
_options.UsePooling = usePooling;
Expand Down
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;
}
}
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;
}
}
}
Loading

0 comments on commit 37be544

Please sign in to comment.