From f768922ce5da2443a4619f4ffb875005bec0c96f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Halil=20=C4=B0brahim=20Kalkan?= Date: Sat, 6 Jan 2018 11:40:27 +0300 Subject: [PATCH] Implementing replacing a dbcontext. --- .../AbpDeskEntityFrameworkCoreModule.cs | 3 +- .../Blogging/AbpDeskMongoBlogModule.cs | 2 +- src/AbpDesk/Web_PlugIns/AbpDesk.MongoBlog.dll | Bin 15360 -> 15360 bytes .../CommonDbContextRegistrationOptions.cs | 55 ++++++++++++++++-- ...mmonDbContextRegistrationOptionsBuilder.cs | 40 ++++++++++++- .../Repositories/RepositoryRegistrarBase.cs | 17 +++--- .../AbpEfCoreServiceCollectionExtensions.cs | 16 ++++- .../AbpDbContextRegistrationOptions.cs | 5 ++ ...IAbpDbContextRegistrationOptionsBuilder.cs | 2 - .../AbpIdentityEntityFrameworkCoreModule.cs | 6 +- .../AbpIdentityServerModule.cs | 4 +- .../AbpMemoryDbServiceCollectionExtensions.cs | 15 ++++- ...moryDbContextRegistrationOptionsBuilder.cs | 1 - .../MemoryDbContextRegistrationOptions.cs | 7 ++- .../AbpMongoDbServiceCollectionExtensions.cs | 17 +++++- .../MongoDbContextRegistrationOptions.cs | 7 ++- ...bpMultiTenancyEntityFrameworkCoreModule.cs | 2 +- .../RepositoryRegistration_Tests.cs | 33 ++++++----- .../AbpEfCoreTestSecondContextModule.cs | 11 ++++ .../TestApp/ThirdDbContext/IThirdDbContext.cs | 9 +++ .../TestApp/ThirdDbContext/ThirdDbContext.cs | 16 +++++ .../ThirdDbContextDummyEntity.cs | 9 +++ .../AbpEntityFrameworkCoreTestModule.cs | 9 +-- .../DbContext_Replace_Tests.cs | 28 +++++++++ .../EntityFrameworkCore/TestAppDbContext.cs | 7 ++- .../Abp/MemoryDb/AbpMemoryDbTestModule.cs | 2 +- .../Volo/Abp/TestApp/TestAppModule.cs | 1 + 27 files changed, 259 insertions(+), 65 deletions(-) create mode 100644 test/Volo.Abp.EntityFrameworkCore.Tests.SecondContext/Volo/Abp/EntityFrameworkCore/TestApp/ThirdDbContext/IThirdDbContext.cs create mode 100644 test/Volo.Abp.EntityFrameworkCore.Tests.SecondContext/Volo/Abp/EntityFrameworkCore/TestApp/ThirdDbContext/ThirdDbContext.cs create mode 100644 test/Volo.Abp.EntityFrameworkCore.Tests.SecondContext/Volo/Abp/EntityFrameworkCore/TestApp/ThirdDbContext/ThirdDbContextDummyEntity.cs create mode 100644 test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/DbContext_Replace_Tests.cs diff --git a/src/AbpDesk/AbpDesk.EntityFrameworkCore/AbpDesk/EntityFrameworkCore/AbpDeskEntityFrameworkCoreModule.cs b/src/AbpDesk/AbpDesk.EntityFrameworkCore/AbpDesk/EntityFrameworkCore/AbpDeskEntityFrameworkCoreModule.cs index 022d8cfc3a..6c3c22d53e 100644 --- a/src/AbpDesk/AbpDesk.EntityFrameworkCore/AbpDesk/EntityFrameworkCore/AbpDeskEntityFrameworkCoreModule.cs +++ b/src/AbpDesk/AbpDesk.EntityFrameworkCore/AbpDesk/EntityFrameworkCore/AbpDeskEntityFrameworkCoreModule.cs @@ -16,7 +16,8 @@ namespace AbpDesk.EntityFrameworkCore { services.AddAbpDbContext(options => { - options.WithDefaultRepositories(); + options.AddDefaultRepositories(); + options.ReplaceDbContext(); }); services.AddAssemblyOf(); diff --git a/src/AbpDesk/AbpDesk.MongoBlog/AbpDesk/Blogging/AbpDeskMongoBlogModule.cs b/src/AbpDesk/AbpDesk.MongoBlog/AbpDesk/Blogging/AbpDeskMongoBlogModule.cs index 5154184d51..8e5df159d1 100644 --- a/src/AbpDesk/AbpDesk.MongoBlog/AbpDesk/Blogging/AbpDeskMongoBlogModule.cs +++ b/src/AbpDesk/AbpDesk.MongoBlog/AbpDesk/Blogging/AbpDeskMongoBlogModule.cs @@ -20,7 +20,7 @@ namespace AbpDesk.Blogging { services.AddMongoDbContext(options => { - options.WithDefaultRepositories(); + options.AddDefaultRepositories(); }); services.Configure(options => diff --git a/src/AbpDesk/Web_PlugIns/AbpDesk.MongoBlog.dll b/src/AbpDesk/Web_PlugIns/AbpDesk.MongoBlog.dll index 207e4f6c0792d8dc50f8671b4ed5af95a9fcc063..879b5f90bd5119f9448d4fee92d477a960437704 100644 GIT binary patch delta 741 zcmXYsT}V_x6vxlG_j>2f{g~Z$MRCr zu1*Z-G_ldpAz+Q53WLz2tKd_je9rx}u!Y)OFeFo(8z!}G%>`H3XUz>ea<}GzaoAKH z&_?_$3)bl|@#hSLi3(kU32=!X)^dSaOJqM}#g$+JG_g#<5|SyL-a;cfVB6Z@<$7{? zs%=A0fGA`24_95FXl4@}aivyb|j1NNDI{y;CT-_Tuf z$B6p-)iBYaS{cC-7-q|&PlSZjQdpoPSPF|`QwMlyvzX4#mPjttm`eo2Nx>*FcJ1(y z7VtD9DYc3jWC|o8LbpS@$likjdMsP!jYhV$&HPeBk6(T*6+#^EH_EVve=v^XG>CI9Lb_BfkCM4_ZwhVALFuErXahNYSoY=ds?l|5h@5wZhvQ@of4e(L7*5;u36fkw1J+=#MDb#tZD zjH+qyNejrkV1v^E&p0K>RAR&lSCjD~jS&}A=&O4NNa3!-@<+kCo5b*r3J!DgQ6t}Yxqt3og6Y~tH2@WRX zL8j4cjItu?Bl$)`&u1O$>4yf#=d8~MbzDeq{IEHaWfQmz@&hgF? zv0{akp!c|r{Taz!jwo9$)W*;4<(N53og3(bL+ n@N#aqF8*oG8)t1LyRAh-B~?d;uGKxTOv public abstract class CommonDbContextRegistrationOptions : ICommonDbContextRegistrationOptionsBuilder { - public bool SpecifiedDefaultRepositoryTypes => DefaultRepositoryImplementationType != null && DefaultRepositoryImplementationTypeWithDefaultPrimaryKey != null; - + public Type OriginalDbContextType { get; } + + public List ReplacedDbContextTypes { get; } + + public Type DefaultRepositoryDbContextType { get; protected set; } + public Type DefaultRepositoryImplementationType { get; private set; } public Type DefaultRepositoryImplementationTypeWithDefaultPrimaryKey { get; private set; } @@ -24,12 +28,34 @@ namespace Volo.Abp.DependencyInjection public Dictionary CustomRepositories { get; } - public CommonDbContextRegistrationOptions() + public bool SpecifiedDefaultRepositoryTypes => DefaultRepositoryImplementationType != null && DefaultRepositoryImplementationTypeWithDefaultPrimaryKey != null; + + protected CommonDbContextRegistrationOptions(Type originalDbContextType) { + OriginalDbContextType = originalDbContextType; + DefaultRepositoryDbContextType = originalDbContextType; CustomRepositories = new Dictionary(); + ReplacedDbContextTypes = new List(); + } + + public ICommonDbContextRegistrationOptionsBuilder ReplaceDbContext() + { + return ReplaceDbContext(typeof(TOtherDbContext)); + } + + public ICommonDbContextRegistrationOptionsBuilder ReplaceDbContext(Type otherDbContextType) + { + if (!otherDbContextType.IsAssignableFrom(OriginalDbContextType)) + { + throw new AbpException($"{OriginalDbContextType.AssemblyQualifiedName} should inherit/implement {otherDbContextType.AssemblyQualifiedName}!"); + } + + ReplacedDbContextTypes.Add(otherDbContextType); + + return this; } - public ICommonDbContextRegistrationOptionsBuilder WithDefaultRepositories(bool includeAllEntities = false) + public ICommonDbContextRegistrationOptionsBuilder AddDefaultRepositories(bool includeAllEntities = false) { RegisterDefaultRepositories = true; IncludeAllEntitiesForDefaultRepositories = includeAllEntities; @@ -37,14 +63,31 @@ namespace Volo.Abp.DependencyInjection return this; } - public ICommonDbContextRegistrationOptionsBuilder WithCustomRepository() + public ICommonDbContextRegistrationOptionsBuilder AddDefaultRepositories(bool includeAllEntities = false) + { + return AddDefaultRepositories(typeof(TDefaultRepositoryDbContext), includeAllEntities); + } + + public ICommonDbContextRegistrationOptionsBuilder AddDefaultRepositories(Type defaultRepositoryDbContextType, bool includeAllEntities = false) + { + if (!defaultRepositoryDbContextType.IsAssignableFrom(OriginalDbContextType)) + { + throw new AbpException($"{OriginalDbContextType.AssemblyQualifiedName} should inherit/implement {defaultRepositoryDbContextType.AssemblyQualifiedName}!"); + } + + DefaultRepositoryDbContextType = defaultRepositoryDbContextType; + + return this; + } + + public ICommonDbContextRegistrationOptionsBuilder AddCustomRepository() { WithCustomRepository(typeof(TEntity), typeof(TRepository)); return this; } - public ICommonDbContextRegistrationOptionsBuilder WithDefaultRepositoryClasses([NotNull] Type repositoryImplementationType, [NotNull] Type repositoryImplementationTypeWithDefaultPrimaryKey) + public ICommonDbContextRegistrationOptionsBuilder SetDefaultRepositoryClasses([NotNull] Type repositoryImplementationType, [NotNull] Type repositoryImplementationTypeWithDefaultPrimaryKey) { Check.NotNull(repositoryImplementationType, nameof(repositoryImplementationType)); Check.NotNull(repositoryImplementationTypeWithDefaultPrimaryKey, nameof(repositoryImplementationTypeWithDefaultPrimaryKey)); diff --git a/src/Volo.Abp.Ddd/Volo/Abp/DependencyInjection/ICommonDbContextRegistrationOptionsBuilder.cs b/src/Volo.Abp.Ddd/Volo/Abp/DependencyInjection/ICommonDbContextRegistrationOptionsBuilder.cs index 5528ca8b19..167fbcad47 100644 --- a/src/Volo.Abp.Ddd/Volo/Abp/DependencyInjection/ICommonDbContextRegistrationOptionsBuilder.cs +++ b/src/Volo.Abp.Ddd/Volo/Abp/DependencyInjection/ICommonDbContextRegistrationOptionsBuilder.cs @@ -11,7 +11,29 @@ namespace Volo.Abp.DependencyInjection /// Registers repositories only for aggregate root entities by default. /// set to true to include all entities. /// - ICommonDbContextRegistrationOptionsBuilder WithDefaultRepositories(bool includeAllEntities = false); + ICommonDbContextRegistrationOptionsBuilder AddDefaultRepositories(bool includeAllEntities = false); + + /// + /// Registers default repositories for this DbContext. + /// Default repositories will use given . + /// + /// DbContext type that will be used by default repositories + /// + /// Registers repositories only for aggregate root entities by default. + /// set to true to include all entities. + /// + ICommonDbContextRegistrationOptionsBuilder AddDefaultRepositories(bool includeAllEntities = false); + + /// + /// Registers default repositories for this DbContext. + /// Default repositories will use given . + /// + /// DbContext type that will be used by default repositories + /// + /// Registers repositories only for aggregate root entities by default. + /// set to true to include all entities. + /// + ICommonDbContextRegistrationOptionsBuilder AddDefaultRepositories(Type defaultRepositoryDbContextType, bool includeAllEntities = false); /// /// Registers custom repository for a specific entity. @@ -19,7 +41,7 @@ namespace Volo.Abp.DependencyInjection /// /// Entity type /// Repository type - ICommonDbContextRegistrationOptionsBuilder WithCustomRepository(); + ICommonDbContextRegistrationOptionsBuilder AddCustomRepository(); /// /// Uses given class(es) for default repositories. @@ -27,6 +49,18 @@ namespace Volo.Abp.DependencyInjection /// Repository implementation type /// Repository implementation type for default primary key type () /// - ICommonDbContextRegistrationOptionsBuilder WithDefaultRepositoryClasses(Type repositoryImplementationType, Type repositoryImplementationTypeWithDefaultPrimaryKey); + ICommonDbContextRegistrationOptionsBuilder SetDefaultRepositoryClasses(Type repositoryImplementationType, Type repositoryImplementationTypeWithDefaultPrimaryKey); + + /// + /// Replaces given DbContext type with this DbContext type. + /// + /// The DbContext type to be replaced + ICommonDbContextRegistrationOptionsBuilder ReplaceDbContext(); + + /// + /// Replaces given DbContext type with this DbContext type. + /// + /// The DbContext type to be replaced + ICommonDbContextRegistrationOptionsBuilder ReplaceDbContext(Type otherDbContextType); } } \ No newline at end of file diff --git a/src/Volo.Abp.Ddd/Volo/Abp/Domain/Repositories/RepositoryRegistrarBase.cs b/src/Volo.Abp.Ddd/Volo/Abp/Domain/Repositories/RepositoryRegistrarBase.cs index 6c2edc1f18..ea3e1963f8 100644 --- a/src/Volo.Abp.Ddd/Volo/Abp/Domain/Repositories/RepositoryRegistrarBase.cs +++ b/src/Volo.Abp.Ddd/Volo/Abp/Domain/Repositories/RepositoryRegistrarBase.cs @@ -1,7 +1,6 @@ using System; using System.Collections.Generic; using Microsoft.Extensions.DependencyInjection; -using Volo.Abp.Data; using Volo.Abp.DependencyInjection; using Volo.Abp.Domain.Entities; using Volo.Abp.Reflection; @@ -18,7 +17,7 @@ namespace Volo.Abp.Domain.Repositories Options = options; } - public virtual void AddRepositories(IServiceCollection services, Type dbContextType) + public virtual void AddRepositories(IServiceCollection services) { foreach (var customRepository in Options.CustomRepositories) { @@ -27,24 +26,24 @@ namespace Volo.Abp.Domain.Repositories if (Options.RegisterDefaultRepositories) { - RegisterDefaultRepositories(services, dbContextType); + RegisterDefaultRepositories(services); } } - protected virtual void RegisterDefaultRepositories(IServiceCollection services, Type dbContextType) + protected virtual void RegisterDefaultRepositories(IServiceCollection services) { - foreach (var entityType in GetEntityTypes(dbContextType)) + foreach (var entityType in GetEntityTypes(Options.DefaultRepositoryDbContextType)) { if (!ShouldRegisterDefaultRepositoryFor(entityType)) { continue; } - RegisterDefaultRepository(services, dbContextType, entityType); + RegisterDefaultRepository(services, entityType); } } - protected void RegisterDefaultRepository(IServiceCollection services, Type dbContextType, Type entityType) + protected void RegisterDefaultRepository(IServiceCollection services, Type entityType) { var primaryKeyType = EntityHelper.GetPrimaryKeyType(entityType); var isDefaultPrimaryKey = primaryKeyType == typeof(Guid); @@ -59,8 +58,8 @@ namespace Volo.Abp.Domain.Repositories else { repositoryImplementationType = isDefaultPrimaryKey - ? GetRepositoryTypeForDefaultPk(dbContextType, entityType) - : GetRepositoryType(dbContextType, entityType, primaryKeyType); + ? GetRepositoryTypeForDefaultPk(Options.DefaultRepositoryDbContextType, entityType) + : GetRepositoryType(Options.DefaultRepositoryDbContextType, entityType, primaryKeyType); } services.AddDefaultRepository(entityType, repositoryImplementationType); diff --git a/src/Volo.Abp.EntityFrameworkCore/Microsoft/Extensions/DependencyInjection/AbpEfCoreServiceCollectionExtensions.cs b/src/Volo.Abp.EntityFrameworkCore/Microsoft/Extensions/DependencyInjection/AbpEfCoreServiceCollectionExtensions.cs index f7ff4b87e8..1236f17b2f 100644 --- a/src/Volo.Abp.EntityFrameworkCore/Microsoft/Extensions/DependencyInjection/AbpEfCoreServiceCollectionExtensions.cs +++ b/src/Volo.Abp.EntityFrameworkCore/Microsoft/Extensions/DependencyInjection/AbpEfCoreServiceCollectionExtensions.cs @@ -14,14 +14,24 @@ namespace Microsoft.Extensions.DependencyInjection { services.AddMemoryCache(); + var options = new AbpDbContextRegistrationOptions(typeof(TDbContext)); + optionsBuilder?.Invoke(options); + services.TryAddTransient(); services.TryAddTransient(DbContextOptionsFactory.Create); - var options = new AbpDbContextRegistrationOptions(); - optionsBuilder?.Invoke(options); + if (options.DefaultRepositoryDbContextType != typeof(TDbContext)) + { + services.TryAddTransient(options.DefaultRepositoryDbContextType, typeof(TDbContext)); + } + + foreach (var dbContextType in options.ReplacedDbContextTypes) + { + services.Replace(ServiceDescriptor.Transient(dbContextType, typeof(TDbContext))); + } new EfCoreRepositoryRegistrar(options) - .AddRepositories(services, typeof(TDbContext)); + .AddRepositories(services); return services; } diff --git a/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/DependencyInjection/AbpDbContextRegistrationOptions.cs b/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/DependencyInjection/AbpDbContextRegistrationOptions.cs index f2612f4b4b..484ceaa3ba 100644 --- a/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/DependencyInjection/AbpDbContextRegistrationOptions.cs +++ b/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/DependencyInjection/AbpDbContextRegistrationOptions.cs @@ -1,9 +1,14 @@ +using System; using Volo.Abp.DependencyInjection; namespace Volo.Abp.EntityFrameworkCore.DependencyInjection { public class AbpDbContextRegistrationOptions : CommonDbContextRegistrationOptions, IAbpDbContextRegistrationOptionsBuilder { + public AbpDbContextRegistrationOptions(Type originalDbContextType) + : base(originalDbContextType) + { + } } } \ No newline at end of file diff --git a/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/DependencyInjection/IAbpDbContextRegistrationOptionsBuilder.cs b/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/DependencyInjection/IAbpDbContextRegistrationOptionsBuilder.cs index db094128a6..0d812fb3cd 100644 --- a/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/DependencyInjection/IAbpDbContextRegistrationOptionsBuilder.cs +++ b/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/DependencyInjection/IAbpDbContextRegistrationOptionsBuilder.cs @@ -1,10 +1,8 @@ -using Volo.Abp.Data; using Volo.Abp.DependencyInjection; namespace Volo.Abp.EntityFrameworkCore.DependencyInjection { public interface IAbpDbContextRegistrationOptionsBuilder : ICommonDbContextRegistrationOptionsBuilder { - } } \ No newline at end of file diff --git a/src/Volo.Abp.Identity.EntityFrameworkCore/Volo/Abp/Identity/EntityFrameworkCore/AbpIdentityEntityFrameworkCoreModule.cs b/src/Volo.Abp.Identity.EntityFrameworkCore/Volo/Abp/Identity/EntityFrameworkCore/AbpIdentityEntityFrameworkCoreModule.cs index 8083fff741..fb3d42cb30 100644 --- a/src/Volo.Abp.Identity.EntityFrameworkCore/Volo/Abp/Identity/EntityFrameworkCore/AbpIdentityEntityFrameworkCoreModule.cs +++ b/src/Volo.Abp.Identity.EntityFrameworkCore/Volo/Abp/Identity/EntityFrameworkCore/AbpIdentityEntityFrameworkCoreModule.cs @@ -11,9 +11,9 @@ namespace Volo.Abp.Identity.EntityFrameworkCore { services.AddAbpDbContext(options => { - options.WithDefaultRepositories(); - options.WithCustomRepository(); - options.WithCustomRepository(); + options.AddDefaultRepositories(); + options.AddCustomRepository(); + options.AddCustomRepository(); }); services.AddAssemblyOf(); diff --git a/src/Volo.Abp.IdentityServer.EntityFrameworkCore/Volo/Abp/IdentityServer/EntityFrameworkCore/AbpIdentityServerModule.cs b/src/Volo.Abp.IdentityServer.EntityFrameworkCore/Volo/Abp/IdentityServer/EntityFrameworkCore/AbpIdentityServerModule.cs index b00b76a162..1d53416680 100644 --- a/src/Volo.Abp.IdentityServer.EntityFrameworkCore/Volo/Abp/IdentityServer/EntityFrameworkCore/AbpIdentityServerModule.cs +++ b/src/Volo.Abp.IdentityServer.EntityFrameworkCore/Volo/Abp/IdentityServer/EntityFrameworkCore/AbpIdentityServerModule.cs @@ -13,8 +13,8 @@ namespace Volo.Abp.IdentityServer.EntityFrameworkCore { services.AddAbpDbContext(options => { - options.WithDefaultRepositories(); - options.WithCustomRepository(); + options.AddDefaultRepositories(); + options.AddCustomRepository(); }); services.AddAssemblyOf(); diff --git a/src/Volo.Abp.MemoryDb/Microsoft/Extensions/DependencyInjection/AbpMemoryDbServiceCollectionExtensions.cs b/src/Volo.Abp.MemoryDb/Microsoft/Extensions/DependencyInjection/AbpMemoryDbServiceCollectionExtensions.cs index 2fbab6a9fd..cf6f9c7842 100644 --- a/src/Volo.Abp.MemoryDb/Microsoft/Extensions/DependencyInjection/AbpMemoryDbServiceCollectionExtensions.cs +++ b/src/Volo.Abp.MemoryDb/Microsoft/Extensions/DependencyInjection/AbpMemoryDbServiceCollectionExtensions.cs @@ -1,4 +1,5 @@ using System; +using Microsoft.Extensions.DependencyInjection.Extensions; using Volo.Abp.MemoryDb; using Volo.Abp.MemoryDb.DependencyInjection; @@ -9,13 +10,23 @@ namespace Microsoft.Extensions.DependencyInjection public static IServiceCollection AddMemoryDbContext(this IServiceCollection services, Action optionsBuilder = null) where TMemoryDbContext : MemoryDbContext { - var options = new MemoryDbContextRegistrationOptions(); + var options = new MemoryDbContextRegistrationOptions(typeof(TMemoryDbContext)); optionsBuilder?.Invoke(options); services.AddSingleton(); + if (options.DefaultRepositoryDbContextType != typeof(TMemoryDbContext)) + { + services.TryAddSingleton(options.DefaultRepositoryDbContextType, sp => sp.GetRequiredService()); + } + + foreach (var dbContextType in options.ReplacedDbContextTypes) + { + services.Replace(ServiceDescriptor.Singleton(dbContextType, sp => sp.GetRequiredService())); + } + new MemoryDbRepositoryRegistrar(options) - .AddRepositories(services, typeof(TMemoryDbContext)); + .AddRepositories(services); return services; } diff --git a/src/Volo.Abp.MemoryDb/Volo/Abp/MemoryDb/DependencyInjection/IMemoryDbContextRegistrationOptionsBuilder.cs b/src/Volo.Abp.MemoryDb/Volo/Abp/MemoryDb/DependencyInjection/IMemoryDbContextRegistrationOptionsBuilder.cs index 9fb3c21409..27081fe83c 100644 --- a/src/Volo.Abp.MemoryDb/Volo/Abp/MemoryDb/DependencyInjection/IMemoryDbContextRegistrationOptionsBuilder.cs +++ b/src/Volo.Abp.MemoryDb/Volo/Abp/MemoryDb/DependencyInjection/IMemoryDbContextRegistrationOptionsBuilder.cs @@ -1,4 +1,3 @@ -using Volo.Abp.Data; using Volo.Abp.DependencyInjection; namespace Volo.Abp.MemoryDb.DependencyInjection diff --git a/src/Volo.Abp.MemoryDb/Volo/Abp/MemoryDb/DependencyInjection/MemoryDbContextRegistrationOptions.cs b/src/Volo.Abp.MemoryDb/Volo/Abp/MemoryDb/DependencyInjection/MemoryDbContextRegistrationOptions.cs index a8150f988b..2d305ed0ee 100644 --- a/src/Volo.Abp.MemoryDb/Volo/Abp/MemoryDb/DependencyInjection/MemoryDbContextRegistrationOptions.cs +++ b/src/Volo.Abp.MemoryDb/Volo/Abp/MemoryDb/DependencyInjection/MemoryDbContextRegistrationOptions.cs @@ -1,10 +1,13 @@ -using Volo.Abp.Data; +using System; using Volo.Abp.DependencyInjection; namespace Volo.Abp.MemoryDb.DependencyInjection { public class MemoryDbContextRegistrationOptions : CommonDbContextRegistrationOptions, IMemoryDbContextRegistrationOptionsBuilder { - + public MemoryDbContextRegistrationOptions(Type originalDbContextType) + : base(originalDbContextType) + { + } } } \ No newline at end of file diff --git a/src/Volo.Abp.MongoDB/Microsoft/Extensions/DependencyInjection/AbpMongoDbServiceCollectionExtensions.cs b/src/Volo.Abp.MongoDB/Microsoft/Extensions/DependencyInjection/AbpMongoDbServiceCollectionExtensions.cs index 195ac135c4..c247e8374d 100644 --- a/src/Volo.Abp.MongoDB/Microsoft/Extensions/DependencyInjection/AbpMongoDbServiceCollectionExtensions.cs +++ b/src/Volo.Abp.MongoDB/Microsoft/Extensions/DependencyInjection/AbpMongoDbServiceCollectionExtensions.cs @@ -1,4 +1,5 @@ using System; +using Microsoft.Extensions.DependencyInjection.Extensions; using Volo.Abp.MongoDB; using Volo.Abp.MongoDB.DependencyInjection; @@ -9,13 +10,23 @@ namespace Microsoft.Extensions.DependencyInjection public static IServiceCollection AddMongoDbContext(this IServiceCollection services, Action optionsBuilder = null) //Created overload instead of default parameter where TMongoDbContext : AbpMongoDbContext { - var options = new MongoDbContextRegistrationOptions(); + var options = new MongoDbContextRegistrationOptions(typeof(TMongoDbContext)); optionsBuilder?.Invoke(options); - services.AddSingleton(); + services.TryAddSingleton(); + + if (options.DefaultRepositoryDbContextType != typeof(TMongoDbContext)) + { + services.TryAddSingleton(options.DefaultRepositoryDbContextType, sp => sp.GetRequiredService()); + } + + foreach (var dbContextType in options.ReplacedDbContextTypes) + { + services.Replace(ServiceDescriptor.Singleton(dbContextType, sp => sp.GetRequiredService())); + } new MongoDbRepositoryRegistrar(options) - .AddRepositories(services, typeof(TMongoDbContext)); + .AddRepositories(services); return services; } diff --git a/src/Volo.Abp.MongoDB/Volo/Abp/MongoDB/DependencyInjection/MongoDbContextRegistrationOptions.cs b/src/Volo.Abp.MongoDB/Volo/Abp/MongoDB/DependencyInjection/MongoDbContextRegistrationOptions.cs index 2c243cd871..6712f1e1ab 100644 --- a/src/Volo.Abp.MongoDB/Volo/Abp/MongoDB/DependencyInjection/MongoDbContextRegistrationOptions.cs +++ b/src/Volo.Abp.MongoDB/Volo/Abp/MongoDB/DependencyInjection/MongoDbContextRegistrationOptions.cs @@ -1,10 +1,13 @@ -using Volo.Abp.Data; +using System; using Volo.Abp.DependencyInjection; namespace Volo.Abp.MongoDB.DependencyInjection { public class MongoDbContextRegistrationOptions : CommonDbContextRegistrationOptions, IMongoDbContextRegistrationOptionsBuilder { - + public MongoDbContextRegistrationOptions(Type originalDbContextType) + : base(originalDbContextType) + { + } } } \ No newline at end of file diff --git a/src/Volo.Abp.MultiTenancy.EntityFrameworkCore/Volo/Abp/MultiTenancy/EntityFrameworkCore/AbpMultiTenancyEntityFrameworkCoreModule.cs b/src/Volo.Abp.MultiTenancy.EntityFrameworkCore/Volo/Abp/MultiTenancy/EntityFrameworkCore/AbpMultiTenancyEntityFrameworkCoreModule.cs index b85fc5f2ac..7a310864d8 100644 --- a/src/Volo.Abp.MultiTenancy.EntityFrameworkCore/Volo/Abp/MultiTenancy/EntityFrameworkCore/AbpMultiTenancyEntityFrameworkCoreModule.cs +++ b/src/Volo.Abp.MultiTenancy.EntityFrameworkCore/Volo/Abp/MultiTenancy/EntityFrameworkCore/AbpMultiTenancyEntityFrameworkCoreModule.cs @@ -12,7 +12,7 @@ namespace Volo.Abp.MultiTenancy.EntityFrameworkCore { services.AddAbpDbContext(options => { - options.WithDefaultRepositories(); + options.AddDefaultRepositories(); }); services.AddAssemblyOf(); diff --git a/test/Volo.Abp.Ddd.Tests/Volo/Abp/Domain/Repositories/RepositoryRegistration_Tests.cs b/test/Volo.Abp.Ddd.Tests/Volo/Abp/Domain/Repositories/RepositoryRegistration_Tests.cs index 88109c9670..fbbbc0c77c 100644 --- a/test/Volo.Abp.Ddd.Tests/Volo/Abp/Domain/Repositories/RepositoryRegistration_Tests.cs +++ b/test/Volo.Abp.Ddd.Tests/Volo/Abp/Domain/Repositories/RepositoryRegistration_Tests.cs @@ -17,12 +17,12 @@ namespace Volo.Abp.Domain.Repositories var services = new ServiceCollection(); - var options = new TestDbContextRegistrationOptions(); - options.WithDefaultRepositories(); + var options = new TestDbContextRegistrationOptions(typeof(MyFakeDbContext)); + options.AddDefaultRepositories(); //Act - new MyTestRepositoryRegistrar(options).AddRepositories(services, typeof(MyFakeDbContext)); + new MyTestRepositoryRegistrar(options).AddRepositories(services); //Assert @@ -38,12 +38,12 @@ namespace Volo.Abp.Domain.Repositories var services = new ServiceCollection(); - var options = new TestDbContextRegistrationOptions(); - options.WithDefaultRepositories(true); + var options = new TestDbContextRegistrationOptions(typeof(MyFakeDbContext)); + options.AddDefaultRepositories(true); //Act - new MyTestRepositoryRegistrar(options).AddRepositories(services, typeof(MyFakeDbContext)); + new MyTestRepositoryRegistrar(options).AddRepositories(services); //Assert @@ -59,14 +59,14 @@ namespace Volo.Abp.Domain.Repositories var services = new ServiceCollection(); - var options = new TestDbContextRegistrationOptions(); + var options = new TestDbContextRegistrationOptions(typeof(MyFakeDbContext)); options - .WithDefaultRepositories(true) - .WithCustomRepository(); + .AddDefaultRepositories(true) + .AddCustomRepository(); //Act - new MyTestRepositoryRegistrar(options).AddRepositories(services, typeof(MyFakeDbContext)); + new MyTestRepositoryRegistrar(options).AddRepositories(services); //Assert @@ -82,14 +82,14 @@ namespace Volo.Abp.Domain.Repositories var services = new ServiceCollection(); - var options = new TestDbContextRegistrationOptions(); + var options = new TestDbContextRegistrationOptions(typeof(MyFakeDbContext)); options - .WithDefaultRepositories(true) - .WithDefaultRepositoryClasses(typeof(MyTestCustomBaseRepository<,>), typeof(MyTestCustomBaseRepository<>)); + .AddDefaultRepositories(true) + .SetDefaultRepositoryClasses(typeof(MyTestCustomBaseRepository<,>), typeof(MyTestCustomBaseRepository<>)); //Act - new MyTestRepositoryRegistrar(options).AddRepositories(services, typeof(MyFakeDbContext)); + new MyTestRepositoryRegistrar(options).AddRepositories(services); //Assert @@ -196,7 +196,10 @@ namespace Volo.Abp.Domain.Repositories public class TestDbContextRegistrationOptions : CommonDbContextRegistrationOptions { - + public TestDbContextRegistrationOptions(Type originalDbContextType) + : base(originalDbContextType) + { + } } } } diff --git a/test/Volo.Abp.EntityFrameworkCore.Tests.SecondContext/Volo/Abp/EntityFrameworkCore/TestApp/SecondContext/AbpEfCoreTestSecondContextModule.cs b/test/Volo.Abp.EntityFrameworkCore.Tests.SecondContext/Volo/Abp/EntityFrameworkCore/TestApp/SecondContext/AbpEfCoreTestSecondContextModule.cs index 6e7f24139a..c72cc3cedf 100644 --- a/test/Volo.Abp.EntityFrameworkCore.Tests.SecondContext/Volo/Abp/EntityFrameworkCore/TestApp/SecondContext/AbpEfCoreTestSecondContextModule.cs +++ b/test/Volo.Abp.EntityFrameworkCore.Tests.SecondContext/Volo/Abp/EntityFrameworkCore/TestApp/SecondContext/AbpEfCoreTestSecondContextModule.cs @@ -1,4 +1,5 @@ using Microsoft.Extensions.DependencyInjection; +using Volo.Abp.EntityFrameworkCore.TestApp.ThirdDbContext; using Volo.Abp.Modularity; namespace Volo.Abp.EntityFrameworkCore.TestApp.SecondContext @@ -8,6 +9,16 @@ namespace Volo.Abp.EntityFrameworkCore.TestApp.SecondContext { public override void ConfigureServices(IServiceCollection services) { + services.AddAbpDbContext(options => + { + options.AddDefaultRepositories(); + }); + + services.AddAbpDbContext(options => + { + options.AddDefaultRepositories(); + }); + services.AddAssemblyOf(); } diff --git a/test/Volo.Abp.EntityFrameworkCore.Tests.SecondContext/Volo/Abp/EntityFrameworkCore/TestApp/ThirdDbContext/IThirdDbContext.cs b/test/Volo.Abp.EntityFrameworkCore.Tests.SecondContext/Volo/Abp/EntityFrameworkCore/TestApp/ThirdDbContext/IThirdDbContext.cs new file mode 100644 index 0000000000..99e6d6410b --- /dev/null +++ b/test/Volo.Abp.EntityFrameworkCore.Tests.SecondContext/Volo/Abp/EntityFrameworkCore/TestApp/ThirdDbContext/IThirdDbContext.cs @@ -0,0 +1,9 @@ +using Microsoft.EntityFrameworkCore; + +namespace Volo.Abp.EntityFrameworkCore.TestApp.ThirdDbContext +{ + public interface IThirdDbContext : IEfCoreDbContext + { + DbSet DummyEntities { get; set; } + } +} \ No newline at end of file diff --git a/test/Volo.Abp.EntityFrameworkCore.Tests.SecondContext/Volo/Abp/EntityFrameworkCore/TestApp/ThirdDbContext/ThirdDbContext.cs b/test/Volo.Abp.EntityFrameworkCore.Tests.SecondContext/Volo/Abp/EntityFrameworkCore/TestApp/ThirdDbContext/ThirdDbContext.cs new file mode 100644 index 0000000000..bb001dc8e4 --- /dev/null +++ b/test/Volo.Abp.EntityFrameworkCore.Tests.SecondContext/Volo/Abp/EntityFrameworkCore/TestApp/ThirdDbContext/ThirdDbContext.cs @@ -0,0 +1,16 @@ +using Microsoft.EntityFrameworkCore; + +namespace Volo.Abp.EntityFrameworkCore.TestApp.ThirdDbContext +{ + /* This dbcontext is just for testing to replace dbcontext from the application using AbpDbContextRegistrationOptions.ReplaceDbContext + */ + public class ThirdDbContext : AbpDbContext, IThirdDbContext + { + public DbSet DummyEntities { get; set; } + + public ThirdDbContext(DbContextOptions options) + : base(options) + { + } + } +} diff --git a/test/Volo.Abp.EntityFrameworkCore.Tests.SecondContext/Volo/Abp/EntityFrameworkCore/TestApp/ThirdDbContext/ThirdDbContextDummyEntity.cs b/test/Volo.Abp.EntityFrameworkCore.Tests.SecondContext/Volo/Abp/EntityFrameworkCore/TestApp/ThirdDbContext/ThirdDbContextDummyEntity.cs new file mode 100644 index 0000000000..f88dfc2cb8 --- /dev/null +++ b/test/Volo.Abp.EntityFrameworkCore.Tests.SecondContext/Volo/Abp/EntityFrameworkCore/TestApp/ThirdDbContext/ThirdDbContextDummyEntity.cs @@ -0,0 +1,9 @@ +using Volo.Abp.Domain.Entities; + +namespace Volo.Abp.EntityFrameworkCore.TestApp.ThirdDbContext +{ + public class ThirdDbContextDummyEntity : AggregateRoot + { + public string Value { get; set; } + } +} \ No newline at end of file diff --git a/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/AbpEntityFrameworkCoreTestModule.cs b/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/AbpEntityFrameworkCoreTestModule.cs index 37ef8850a1..b6b77d26f8 100644 --- a/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/AbpEntityFrameworkCoreTestModule.cs +++ b/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/AbpEntityFrameworkCoreTestModule.cs @@ -3,6 +3,7 @@ using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.DependencyInjection; using Volo.Abp.Autofac; using Volo.Abp.EntityFrameworkCore.TestApp.SecondContext; +using Volo.Abp.EntityFrameworkCore.TestApp.ThirdDbContext; using Volo.Abp.Modularity; using Volo.Abp.TestApp; using Volo.Abp.TestApp.EntityFrameworkCore; @@ -21,12 +22,8 @@ namespace Volo.Abp.EntityFrameworkCore services.AddAbpDbContext(options => { - options.WithDefaultRepositories(); - }); - - services.AddAbpDbContext(options => - { - options.WithDefaultRepositories(); + options.AddDefaultRepositories(); + options.ReplaceDbContext(); }); var inMemorySqlite = new SqliteConnection("Data Source=:memory:"); diff --git a/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/DbContext_Replace_Tests.cs b/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/DbContext_Replace_Tests.cs new file mode 100644 index 0000000000..a86797feed --- /dev/null +++ b/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/DbContext_Replace_Tests.cs @@ -0,0 +1,28 @@ +using Microsoft.Extensions.DependencyInjection; +using Shouldly; +using Volo.Abp.Domain.Repositories; +using Volo.Abp.EntityFrameworkCore.TestApp.ThirdDbContext; +using Volo.Abp.TestApp.EntityFrameworkCore; +using Xunit; + +namespace Volo.Abp.EntityFrameworkCore +{ + public class DbContext_Replace_Tests : EntityFrameworkCoreTestBase + { + private readonly IRepository _dummyRepository; + + public DbContext_Replace_Tests() + { + _dummyRepository = ServiceProvider.GetRequiredService>(); + } + + [Fact] + public void Should_Replace_DbContext() + { + (ServiceProvider.GetRequiredService() is TestAppDbContext).ShouldBeTrue(); + + (_dummyRepository.GetDbContext() is IThirdDbContext).ShouldBeTrue(); + (_dummyRepository.GetDbContext() is TestAppDbContext).ShouldBeTrue(); + } + } +} diff --git a/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/TestApp/EntityFrameworkCore/TestAppDbContext.cs b/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/TestApp/EntityFrameworkCore/TestAppDbContext.cs index 77373a84d5..f6088eb8a7 100644 --- a/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/TestApp/EntityFrameworkCore/TestAppDbContext.cs +++ b/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/TestApp/EntityFrameworkCore/TestAppDbContext.cs @@ -1,13 +1,16 @@ using Microsoft.EntityFrameworkCore; using Volo.Abp.EntityFrameworkCore; +using Volo.Abp.EntityFrameworkCore.TestApp.ThirdDbContext; using Volo.Abp.TestApp.Domain; namespace Volo.Abp.TestApp.EntityFrameworkCore { - public class TestAppDbContext : AbpDbContext + public class TestAppDbContext : AbpDbContext, IThirdDbContext { public DbSet People { get; set; } - + + public DbSet DummyEntities { get; set; } + public TestAppDbContext(DbContextOptions options) : base(options) { diff --git a/test/Volo.Abp.MemoryDb.Tests/Volo/Abp/MemoryDb/AbpMemoryDbTestModule.cs b/test/Volo.Abp.MemoryDb.Tests/Volo/Abp/MemoryDb/AbpMemoryDbTestModule.cs index 790e105b1d..86708d55ef 100644 --- a/test/Volo.Abp.MemoryDb.Tests/Volo/Abp/MemoryDb/AbpMemoryDbTestModule.cs +++ b/test/Volo.Abp.MemoryDb.Tests/Volo/Abp/MemoryDb/AbpMemoryDbTestModule.cs @@ -22,7 +22,7 @@ namespace Volo.Abp.MemoryDb services.AddMemoryDbContext(options => { - options.WithDefaultRepositories(); + options.AddDefaultRepositories(); }); services.AddAssemblyOf(); diff --git a/test/Volo.Abp.TestApp/Volo/Abp/TestApp/TestAppModule.cs b/test/Volo.Abp.TestApp/Volo/Abp/TestApp/TestAppModule.cs index 1714d543c4..145d361737 100644 --- a/test/Volo.Abp.TestApp/Volo/Abp/TestApp/TestAppModule.cs +++ b/test/Volo.Abp.TestApp/Volo/Abp/TestApp/TestAppModule.cs @@ -14,6 +14,7 @@ namespace Volo.Abp.TestApp public override void ConfigureServices(IServiceCollection services) { ConfigureAutoMapper(services); + services.AddAssemblyOf(); }