diff --git a/framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/DependencyInjection/AbpCommonDbContextRegistrationOptions.cs b/framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/DependencyInjection/AbpCommonDbContextRegistrationOptions.cs index dbd46811f2..babe1da1f9 100644 --- a/framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/DependencyInjection/AbpCommonDbContextRegistrationOptions.cs +++ b/framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/DependencyInjection/AbpCommonDbContextRegistrationOptions.cs @@ -29,6 +29,8 @@ namespace Volo.Abp.DependencyInjection public Dictionary CustomRepositories { get; } + public List DefaultRepositories { get; } + public bool SpecifiedDefaultRepositoryTypes => DefaultRepositoryImplementationType != null && DefaultRepositoryImplementationTypeWithoutKey != null; protected AbpCommonDbContextRegistrationOptions(Type originalDbContextType, IServiceCollection services) @@ -38,6 +40,7 @@ namespace Volo.Abp.DependencyInjection DefaultRepositoryDbContextType = originalDbContextType; CustomRepositories = new Dictionary(); ReplacedDbContextTypes = new List(); + DefaultRepositories = new List(); } public IAbpCommonDbContextRegistrationOptionsBuilder ReplaceDbContext() @@ -82,6 +85,23 @@ namespace Volo.Abp.DependencyInjection return AddDefaultRepositories(typeof(TDefaultRepositoryDbContext), includeAllEntities); } + public IAbpCommonDbContextRegistrationOptionsBuilder AddDefaultRepository() + { + return AddDefaultRepository(typeof(TEntity)); + } + + public IAbpCommonDbContextRegistrationOptionsBuilder AddDefaultRepository(Type entityType) + { + if (!typeof(IEntity).IsAssignableFrom(entityType)) + { + throw new AbpException($"Given entityType is not an entity: {entityType.AssemblyQualifiedName}. It must implement {typeof(IEntity<>).AssemblyQualifiedName}."); + } + + DefaultRepositories.AddIfNotContains(entityType); + + return this; + } + public IAbpCommonDbContextRegistrationOptionsBuilder AddRepository() { AddCustomRepository(typeof(TEntity), typeof(TRepository)); @@ -118,4 +138,4 @@ namespace Volo.Abp.DependencyInjection CustomRepositories[entityType] = repositoryType; } } -} \ No newline at end of file +} diff --git a/framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/DependencyInjection/IAbpCommonDbContextRegistrationOptionsBuilder.cs b/framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/DependencyInjection/IAbpCommonDbContextRegistrationOptionsBuilder.cs index 53d2f142cd..ebe49718b2 100644 --- a/framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/DependencyInjection/IAbpCommonDbContextRegistrationOptionsBuilder.cs +++ b/framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/DependencyInjection/IAbpCommonDbContextRegistrationOptionsBuilder.cs @@ -39,6 +39,20 @@ namespace Volo.Abp.DependencyInjection /// IAbpCommonDbContextRegistrationOptionsBuilder AddDefaultRepositories(Type defaultRepositoryDbContextType, bool includeAllEntities = false); + /// + /// Registers custom repository for a specific entity. + /// + /// Entity type + IAbpCommonDbContextRegistrationOptionsBuilder AddDefaultRepository(); + + + /// + /// Registers default repository for a specific entity. + /// + /// + /// + IAbpCommonDbContextRegistrationOptionsBuilder AddDefaultRepository(Type entityType); + /// /// Registers custom repository for a specific entity. /// Custom repositories overrides default repositories. diff --git a/framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/Repositories/RepositoryRegistrarBase.cs b/framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/Repositories/RepositoryRegistrarBase.cs index 548a636eff..ef686e7001 100644 --- a/framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/Repositories/RepositoryRegistrarBase.cs +++ b/framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/Repositories/RepositoryRegistrarBase.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Linq; using Microsoft.Extensions.DependencyInjection; using Volo.Abp.DependencyInjection; using Volo.Abp.Domain.Entities; @@ -17,20 +18,29 @@ namespace Volo.Abp.Domain.Repositories } public virtual void AddRepositories() + { + RegisterCustomRepositories(); + + RegisterDefaultRepositories(); + + RegisterSpecifiedDefaultRepositories(); + } + + protected virtual void RegisterCustomRepositories() { foreach (var customRepository in Options.CustomRepositories) { Options.Services.AddDefaultRepository(customRepository.Key, customRepository.Value, replaceExisting: true); } - - if (Options.RegisterDefaultRepositories) - { - RegisterDefaultRepositories(); - } } protected virtual void RegisterDefaultRepositories() { + if (!Options.RegisterDefaultRepositories) + { + return; + } + foreach (var entityType in GetEntityTypes(Options.OriginalDbContextType)) { if (!ShouldRegisterDefaultRepositoryFor(entityType)) @@ -42,6 +52,17 @@ namespace Volo.Abp.Domain.Repositories } } + protected virtual void RegisterSpecifiedDefaultRepositories() + { + foreach (var entityType in Options.DefaultRepositories) + { + if (!Options.CustomRepositories.ContainsKey(entityType)) + { + RegisterDefaultRepository(entityType); + } + } + } + protected virtual void RegisterDefaultRepository(Type entityType) { Options.Services.AddDefaultRepository( @@ -92,4 +113,4 @@ namespace Volo.Abp.Domain.Repositories protected abstract Type GetRepositoryType(Type dbContextType, Type entityType, Type primaryKeyType); } -} \ No newline at end of file +} diff --git a/framework/test/Volo.Abp.Ddd.Tests/Volo/Abp/Domain/Repositories/RepositoryRegistration_Tests.cs b/framework/test/Volo.Abp.Ddd.Tests/Volo/Abp/Domain/Repositories/RepositoryRegistration_Tests.cs index 85696fae21..32584f293e 100644 --- a/framework/test/Volo.Abp.Ddd.Tests/Volo/Abp/Domain/Repositories/RepositoryRegistration_Tests.cs +++ b/framework/test/Volo.Abp.Ddd.Tests/Volo/Abp/Domain/Repositories/RepositoryRegistration_Tests.cs @@ -189,6 +189,60 @@ namespace Volo.Abp.Domain.Repositories services.ShouldContainTransient(typeof(IRepository), typeof(MyTestCustomBaseRepository)); } + [Fact] + public void Should_Register_Default_Repository() + { + //Arrange + + var services = new ServiceCollection(); + + var options = new TestDbContextRegistrationOptions(typeof(MyFakeDbContext), services); + options.AddDefaultRepository(); + + //Act + + new MyTestRepositoryRegistrar(options).AddRepositories(); + + //MyTestAggregateRootWithoutPk + services.ShouldNotContainService(typeof(IReadOnlyRepository)); + services.ShouldNotContainService(typeof(IBasicRepository)); + services.ShouldNotContainService(typeof(IRepository)); + + //MyTestAggregateRootWithGuidPk + services.ShouldContainTransient(typeof(IReadOnlyRepository), typeof(MyTestDefaultRepository)); + services.ShouldContainTransient(typeof(IBasicRepository), typeof(MyTestDefaultRepository)); + services.ShouldContainTransient(typeof(IRepository), typeof(MyTestDefaultRepository)); + services.ShouldContainTransient(typeof(IReadOnlyRepository), typeof(MyTestDefaultRepository)); + services.ShouldContainTransient(typeof(IBasicRepository), typeof(MyTestDefaultRepository)); + services.ShouldContainTransient(typeof(IRepository), typeof(MyTestDefaultRepository)); + } + + [Fact] + public void Should_Not_Register_Default_Repository_If_Registered_Custom_Repository() + { + //Arrange + + var services = new ServiceCollection(); + + var options = new TestDbContextRegistrationOptions(typeof(MyFakeDbContext), services); + options + .AddDefaultRepository() + .AddRepository();; + + //Act + + new MyTestRepositoryRegistrar(options).AddRepositories(); + + //MyTestAggregateRootWithGuidPk + services.ShouldContainTransient(typeof(IReadOnlyRepository), typeof(MyTestAggregateRootWithDefaultPkCustomRepository)); + services.ShouldContainTransient(typeof(IBasicRepository), typeof(MyTestAggregateRootWithDefaultPkCustomRepository)); + services.ShouldContainTransient(typeof(IRepository), typeof(MyTestAggregateRootWithDefaultPkCustomRepository)); + services.ShouldContainTransient(typeof(IReadOnlyRepository), typeof(MyTestAggregateRootWithDefaultPkCustomRepository)); + services.ShouldContainTransient(typeof(IReadOnlyBasicRepository), typeof(MyTestAggregateRootWithDefaultPkCustomRepository)); + services.ShouldContainTransient(typeof(IBasicRepository), typeof(MyTestAggregateRootWithDefaultPkCustomRepository)); + services.ShouldContainTransient(typeof(IRepository), typeof(MyTestAggregateRootWithDefaultPkCustomRepository)); + } + public class MyTestRepositoryRegistrar : RepositoryRegistrarBase { public MyTestRepositoryRegistrar(AbpCommonDbContextRegistrationOptions options) diff --git a/modules/audit-logging/src/Volo.Abp.AuditLogging.EntityFrameworkCore/Volo/Abp/AuditLogging/EntityFrameworkCore/IAuditLoggingDbContext.cs b/modules/audit-logging/src/Volo.Abp.AuditLogging.EntityFrameworkCore/Volo/Abp/AuditLogging/EntityFrameworkCore/IAuditLoggingDbContext.cs index 684c5fc55f..12330463f3 100644 --- a/modules/audit-logging/src/Volo.Abp.AuditLogging.EntityFrameworkCore/Volo/Abp/AuditLogging/EntityFrameworkCore/IAuditLoggingDbContext.cs +++ b/modules/audit-logging/src/Volo.Abp.AuditLogging.EntityFrameworkCore/Volo/Abp/AuditLogging/EntityFrameworkCore/IAuditLoggingDbContext.cs @@ -7,6 +7,6 @@ namespace Volo.Abp.AuditLogging.EntityFrameworkCore [ConnectionStringName(AbpAuditLoggingDbProperties.ConnectionStringName)] public interface IAuditLoggingDbContext : IEfCoreDbContext { - DbSet AuditLogs { get; set; } + DbSet AuditLogs { get; } } } \ No newline at end of file diff --git a/modules/blogging/src/Volo.Blogging.EntityFrameworkCore/Volo/Blogging/EntityFrameworkCore/IBloggingDbContext.cs b/modules/blogging/src/Volo.Blogging.EntityFrameworkCore/Volo/Blogging/EntityFrameworkCore/IBloggingDbContext.cs index ecb7c282d4..4eb8f35c03 100644 --- a/modules/blogging/src/Volo.Blogging.EntityFrameworkCore/Volo/Blogging/EntityFrameworkCore/IBloggingDbContext.cs +++ b/modules/blogging/src/Volo.Blogging.EntityFrameworkCore/Volo/Blogging/EntityFrameworkCore/IBloggingDbContext.cs @@ -16,14 +16,14 @@ namespace Volo.Blogging.EntityFrameworkCore { DbSet Users { get; } - DbSet Blogs { get; set; } + DbSet Blogs { get; } - DbSet Posts { get; set; } + DbSet Posts { get; } - DbSet Comments { get; set; } + DbSet Comments { get; } - DbSet PostTags { get; set; } + DbSet PostTags { get; } - DbSet Tags { get; set; } + DbSet Tags { get; } } } diff --git a/modules/cms-kit/src/Volo.CmsKit.EntityFrameworkCore/Volo/CmsKit/EntityFrameworkCore/ICmsKitDbContext.cs b/modules/cms-kit/src/Volo.CmsKit.EntityFrameworkCore/Volo/CmsKit/EntityFrameworkCore/ICmsKitDbContext.cs index 42b0a5da89..0299856341 100644 --- a/modules/cms-kit/src/Volo.CmsKit.EntityFrameworkCore/Volo/CmsKit/EntityFrameworkCore/ICmsKitDbContext.cs +++ b/modules/cms-kit/src/Volo.CmsKit.EntityFrameworkCore/Volo/CmsKit/EntityFrameworkCore/ICmsKitDbContext.cs @@ -16,16 +16,16 @@ namespace Volo.CmsKit.EntityFrameworkCore [ConnectionStringName(CmsKitDbProperties.ConnectionStringName)] public interface ICmsKitDbContext : IEfCoreDbContext { - DbSet Contents { get; set; } - DbSet Comments { get; set; } - DbSet User { get; set; } - DbSet Reactions { get; set; } - DbSet Ratings { get; set; } - DbSet Tags { get; set; } - DbSet EntityTags { get; set; } - DbSet Pages { get; set; } - DbSet Blogs { get; set; } - DbSet BlogPosts { get; set; } - DbSet MediaDescriptors { get; set; } + DbSet Contents { get; } + DbSet Comments { get; } + DbSet User { get; } + DbSet Reactions { get; } + DbSet Ratings { get; } + DbSet Tags { get; } + DbSet EntityTags { get; } + DbSet Pages { get; } + DbSet Blogs { get; } + DbSet BlogPosts { get; } + DbSet MediaDescriptors { get; } } } \ No newline at end of file diff --git a/modules/docs/src/Volo.Docs.EntityFrameworkCore/Volo/Docs/EntityFrameworkCore/IDocsDbContext.cs b/modules/docs/src/Volo.Docs.EntityFrameworkCore/Volo/Docs/EntityFrameworkCore/IDocsDbContext.cs index 8edd1b74a0..4d79609718 100644 --- a/modules/docs/src/Volo.Docs.EntityFrameworkCore/Volo/Docs/EntityFrameworkCore/IDocsDbContext.cs +++ b/modules/docs/src/Volo.Docs.EntityFrameworkCore/Volo/Docs/EntityFrameworkCore/IDocsDbContext.cs @@ -11,10 +11,10 @@ namespace Volo.Docs.EntityFrameworkCore [ConnectionStringName(DocsDbProperties.ConnectionStringName)] public interface IDocsDbContext : IEfCoreDbContext { - DbSet Projects { get; set; } + DbSet Projects { get; } - DbSet Documents { get; set; } + DbSet Documents { get; } - DbSet DocumentContributors { get; set; } + DbSet DocumentContributors { get; } } } diff --git a/modules/feature-management/src/Volo.Abp.FeatureManagement.EntityFrameworkCore/Volo/Abp/FeatureManagement/EntityFrameworkCore/IFeatureManagementDbContext.cs b/modules/feature-management/src/Volo.Abp.FeatureManagement.EntityFrameworkCore/Volo/Abp/FeatureManagement/EntityFrameworkCore/IFeatureManagementDbContext.cs index 5f1f3f19b2..4189c2011e 100644 --- a/modules/feature-management/src/Volo.Abp.FeatureManagement.EntityFrameworkCore/Volo/Abp/FeatureManagement/EntityFrameworkCore/IFeatureManagementDbContext.cs +++ b/modules/feature-management/src/Volo.Abp.FeatureManagement.EntityFrameworkCore/Volo/Abp/FeatureManagement/EntityFrameworkCore/IFeatureManagementDbContext.cs @@ -9,6 +9,6 @@ namespace Volo.Abp.FeatureManagement.EntityFrameworkCore [ConnectionStringName(FeatureManagementDbProperties.ConnectionStringName)] public interface IFeatureManagementDbContext : IEfCoreDbContext { - DbSet FeatureValues { get; set; } + DbSet FeatureValues { get; } } } diff --git a/modules/identity/src/Volo.Abp.Identity.EntityFrameworkCore/Volo/Abp/Identity/EntityFrameworkCore/IIdentityDbContext.cs b/modules/identity/src/Volo.Abp.Identity.EntityFrameworkCore/Volo/Abp/Identity/EntityFrameworkCore/IIdentityDbContext.cs index f76ff7b9c3..feae4ddd65 100644 --- a/modules/identity/src/Volo.Abp.Identity.EntityFrameworkCore/Volo/Abp/Identity/EntityFrameworkCore/IIdentityDbContext.cs +++ b/modules/identity/src/Volo.Abp.Identity.EntityFrameworkCore/Volo/Abp/Identity/EntityFrameworkCore/IIdentityDbContext.cs @@ -7,16 +7,16 @@ namespace Volo.Abp.Identity.EntityFrameworkCore [ConnectionStringName(AbpIdentityDbProperties.ConnectionStringName)] public interface IIdentityDbContext : IEfCoreDbContext { - DbSet Users { get; set; } + DbSet Users { get; } - DbSet Roles { get; set; } + DbSet Roles { get; } - DbSet ClaimTypes { get; set; } + DbSet ClaimTypes { get; } - DbSet OrganizationUnits { get; set; } + DbSet OrganizationUnits { get; } - DbSet SecurityLogs { get; set; } + DbSet SecurityLogs { get; } - DbSet LinkUsers { get; set; } + DbSet LinkUsers { get; } } } diff --git a/modules/identityserver/src/Volo.Abp.IdentityServer.EntityFrameworkCore/Volo/Abp/IdentityServer/EntityFrameworkCore/IIdentityServerDbContext.cs b/modules/identityserver/src/Volo.Abp.IdentityServer.EntityFrameworkCore/Volo/Abp/IdentityServer/EntityFrameworkCore/IIdentityServerDbContext.cs index 41845c432b..965c5da7da 100644 --- a/modules/identityserver/src/Volo.Abp.IdentityServer.EntityFrameworkCore/Volo/Abp/IdentityServer/EntityFrameworkCore/IIdentityServerDbContext.cs +++ b/modules/identityserver/src/Volo.Abp.IdentityServer.EntityFrameworkCore/Volo/Abp/IdentityServer/EntityFrameworkCore/IIdentityServerDbContext.cs @@ -17,64 +17,64 @@ namespace Volo.Abp.IdentityServer.EntityFrameworkCore { #region ApiResource - DbSet ApiResources { get; set; } + DbSet ApiResources { get; } - DbSet ApiResourceSecrets { get; set; } + DbSet ApiResourceSecrets { get; } - DbSet ApiResourceClaims { get; set; } + DbSet ApiResourceClaims { get; } - DbSet ApiResourceScopes { get; set; } + DbSet ApiResourceScopes { get; } - DbSet ApiResourceProperties { get; set; } + DbSet ApiResourceProperties { get; } #endregion #region ApiScope - DbSet ApiScopes { get; set; } + DbSet ApiScopes { get; } - DbSet ApiScopeClaims { get; set; } + DbSet ApiScopeClaims { get; } - DbSet ApiScopeProperties { get; set; } + DbSet ApiScopeProperties { get; } #endregion #region IdentityResource - DbSet IdentityResources { get; set; } + DbSet IdentityResources { get; } - DbSet IdentityClaims { get; set; } + DbSet IdentityClaims { get; } - DbSet IdentityResourceProperties { get; set; } + DbSet IdentityResourceProperties { get; } #endregion #region Client - DbSet Clients { get; set; } + DbSet Clients { get; } - DbSet ClientGrantTypes { get; set; } + DbSet ClientGrantTypes { get; } - DbSet ClientRedirectUris { get; set; } + DbSet ClientRedirectUris { get; } - DbSet ClientPostLogoutRedirectUris { get; set; } + DbSet ClientPostLogoutRedirectUris { get; } - DbSet ClientScopes { get; set; } + DbSet ClientScopes { get; } - DbSet ClientSecrets { get; set; } + DbSet ClientSecrets { get; } - DbSet ClientClaims { get; set; } + DbSet ClientClaims { get; } - DbSet ClientIdPRestrictions { get; set; } + DbSet ClientIdPRestrictions { get; } - DbSet ClientCorsOrigins { get; set; } + DbSet ClientCorsOrigins { get; } - DbSet ClientProperties { get; set; } + DbSet ClientProperties { get; } #endregion - DbSet PersistedGrants { get; set; } + DbSet PersistedGrants { get; } - DbSet DeviceFlowCodes { get; set; } + DbSet DeviceFlowCodes { get; } } } diff --git a/modules/permission-management/src/Volo.Abp.PermissionManagement.EntityFrameworkCore/Volo/Abp/PermissionManagement/EntityFrameworkCore/IPermissionManagementDbContext.cs b/modules/permission-management/src/Volo.Abp.PermissionManagement.EntityFrameworkCore/Volo/Abp/PermissionManagement/EntityFrameworkCore/IPermissionManagementDbContext.cs index 9319e75f5e..03c96cf2c5 100644 --- a/modules/permission-management/src/Volo.Abp.PermissionManagement.EntityFrameworkCore/Volo/Abp/PermissionManagement/EntityFrameworkCore/IPermissionManagementDbContext.cs +++ b/modules/permission-management/src/Volo.Abp.PermissionManagement.EntityFrameworkCore/Volo/Abp/PermissionManagement/EntityFrameworkCore/IPermissionManagementDbContext.cs @@ -7,6 +7,6 @@ namespace Volo.Abp.PermissionManagement.EntityFrameworkCore [ConnectionStringName(AbpPermissionManagementDbProperties.ConnectionStringName)] public interface IPermissionManagementDbContext : IEfCoreDbContext { - DbSet PermissionGrants { get; set; } + DbSet PermissionGrants { get; } } } \ No newline at end of file diff --git a/modules/setting-management/src/Volo.Abp.SettingManagement.EntityFrameworkCore/Volo/Abp/SettingManagement/EntityFrameworkCore/ISettingManagementDbContext.cs b/modules/setting-management/src/Volo.Abp.SettingManagement.EntityFrameworkCore/Volo/Abp/SettingManagement/EntityFrameworkCore/ISettingManagementDbContext.cs index eca448f732..aa0305d37c 100644 --- a/modules/setting-management/src/Volo.Abp.SettingManagement.EntityFrameworkCore/Volo/Abp/SettingManagement/EntityFrameworkCore/ISettingManagementDbContext.cs +++ b/modules/setting-management/src/Volo.Abp.SettingManagement.EntityFrameworkCore/Volo/Abp/SettingManagement/EntityFrameworkCore/ISettingManagementDbContext.cs @@ -9,6 +9,6 @@ namespace Volo.Abp.SettingManagement.EntityFrameworkCore [ConnectionStringName(AbpSettingManagementDbProperties.ConnectionStringName)] public interface ISettingManagementDbContext : IEfCoreDbContext { - DbSet Settings { get; set; } + DbSet Settings { get; } } } diff --git a/modules/tenant-management/src/Volo.Abp.TenantManagement.EntityFrameworkCore/Volo/Abp/TenantManagement/EntityFrameworkCore/ITenantManagementDbContext.cs b/modules/tenant-management/src/Volo.Abp.TenantManagement.EntityFrameworkCore/Volo/Abp/TenantManagement/EntityFrameworkCore/ITenantManagementDbContext.cs index 0000e2827c..9d3d0f53c2 100644 --- a/modules/tenant-management/src/Volo.Abp.TenantManagement.EntityFrameworkCore/Volo/Abp/TenantManagement/EntityFrameworkCore/ITenantManagementDbContext.cs +++ b/modules/tenant-management/src/Volo.Abp.TenantManagement.EntityFrameworkCore/Volo/Abp/TenantManagement/EntityFrameworkCore/ITenantManagementDbContext.cs @@ -9,8 +9,8 @@ namespace Volo.Abp.TenantManagement.EntityFrameworkCore [ConnectionStringName(AbpTenantManagementDbProperties.ConnectionStringName)] public interface ITenantManagementDbContext : IEfCoreDbContext { - DbSet Tenants { get; set; } + DbSet Tenants { get; } - DbSet TenantConnectionStrings { get; set; } + DbSet TenantConnectionStrings { get; } } }