diff --git a/src/Volo.Abp.Ddd/Microsoft/Extensions/DependencyInjection/ServiceCollectionRepositoryExtensions.cs b/src/Volo.Abp.Ddd/Microsoft/Extensions/DependencyInjection/ServiceCollectionRepositoryExtensions.cs index 34f16efc34..1e75094169 100644 --- a/src/Volo.Abp.Ddd/Microsoft/Extensions/DependencyInjection/ServiceCollectionRepositoryExtensions.cs +++ b/src/Volo.Abp.Ddd/Microsoft/Extensions/DependencyInjection/ServiceCollectionRepositoryExtensions.cs @@ -4,66 +4,49 @@ using Microsoft.Extensions.DependencyInjection.Extensions; using Volo.Abp; using Volo.Abp.Domain.Entities; using Volo.Abp.Domain.Repositories; -using Volo.Abp.Reflection; namespace Microsoft.Extensions.DependencyInjection { public static class ServiceCollectionRepositoryExtensions { - public static void AddDefaultRepository(this IServiceCollection services, Type entityType, Type repositoryImplementationType) + public static IServiceCollection AddDefaultRepository(this IServiceCollection services, Type entityType, Type repositoryImplementationType) { - AddDefaultRepositoryForGenericPrimaryKey(services, entityType, repositoryImplementationType); - - //if (BothSupportsDefaultPrimaryKey(entityType, repositoryImplementationType)) - //{ - // AddDefaultRepositoryForDefaultPrimaryKey(services, entityType, repositoryImplementationType); - //} - } - - private static void AddDefaultRepositoryForGenericPrimaryKey(IServiceCollection services, Type entityType, Type repositoryImplementationType) - { - var primaryKeyType = EntityHelper.GetPrimaryKeyType(entityType); - - //IRepository - var repositoryInterface = typeof(IRepository<,>).MakeGenericType(entityType, primaryKeyType); - if (!repositoryInterface.GetTypeInfo().IsAssignableFrom(repositoryImplementationType)) + //IRepository + var repositoryInterfaceWithoutPk = typeof(IRepository<>).MakeGenericType(entityType); + if (!repositoryInterfaceWithoutPk.IsAssignableFrom(repositoryImplementationType)) { - throw new AbpException($"Given repositoryImplementationType ({repositoryImplementationType}) must implement {repositoryInterface}"); + throw new AbpException($"Given repositoryImplementationType ({repositoryImplementationType}) must implement {repositoryInterfaceWithoutPk}"); } - services.TryAddTransient(repositoryInterface, repositoryImplementationType); + services.TryAddTransient(repositoryInterfaceWithoutPk, repositoryImplementationType); - //IQueryableRepository - var queryableRepositoryInterface = typeof(IQueryableRepository<,>).MakeGenericType(entityType, primaryKeyType); - if (queryableRepositoryInterface.GetTypeInfo().IsAssignableFrom(repositoryImplementationType)) + //IQueryableRepository + var queryableRepositoryInterfaceWithPk = typeof(IQueryableRepository<>).MakeGenericType(entityType); + if (repositoryInterfaceWithoutPk.IsAssignableFrom(repositoryImplementationType)) { - services.TryAddTransient(queryableRepositoryInterface, repositoryImplementationType); + services.TryAddTransient(queryableRepositoryInterfaceWithPk, repositoryImplementationType); } - } - - //private static void AddDefaultRepositoryForDefaultPrimaryKey(IServiceCollection services, Type entityType, Type repositoryImplementationType) - //{ - // //IRepository - // var repositoryInterfaceWithDefaultPrimaryKey = typeof(IRepository<>).MakeGenericType(entityType); - // if (!repositoryInterfaceWithDefaultPrimaryKey.GetTypeInfo().IsAssignableFrom(repositoryImplementationType)) - // { - // throw new AbpException($"Given repositoryImplementationType ({repositoryImplementationType}) must implement {repositoryInterfaceWithDefaultPrimaryKey}"); - // } - // services.TryAddTransient(repositoryInterfaceWithDefaultPrimaryKey, repositoryImplementationType); + var primaryKeyType = EntityHelper.FindPrimaryKeyType(entityType); - // //IQueryableRepository - // var queryableRepositoryInterfaceWithDefaultPrimaryKey = typeof(IQueryableRepository<>).MakeGenericType(entityType); - // if (queryableRepositoryInterfaceWithDefaultPrimaryKey.GetTypeInfo().IsAssignableFrom(repositoryImplementationType)) - // { - // services.TryAddTransient(queryableRepositoryInterfaceWithDefaultPrimaryKey, repositoryImplementationType); - // } - //} + if (primaryKeyType != null) + { + //IRepository + var repositoryInterface = typeof(IRepository<,>).MakeGenericType(entityType, primaryKeyType); + if (repositoryInterface.GetTypeInfo().IsAssignableFrom(repositoryImplementationType)) + { + services.TryAddTransient(repositoryInterface, repositoryImplementationType); + } + + //IQueryableRepository + var queryableRepositoryInterface = typeof(IQueryableRepository<,>).MakeGenericType(entityType, primaryKeyType); + if (queryableRepositoryInterface.GetTypeInfo().IsAssignableFrom(repositoryImplementationType)) + { + services.TryAddTransient(queryableRepositoryInterface, repositoryImplementationType); + } + } - //private static bool BothSupportsDefaultPrimaryKey(Type entityType, Type repositoryImplementationType) - //{ - // return typeof(IEntity).GetTypeInfo().IsAssignableFrom(entityType) && - // ReflectionHelper.IsAssignableToGenericType(repositoryImplementationType, typeof(IRepository<>)); - //} + return services; + } } } \ No newline at end of file diff --git a/src/Volo.Abp.Ddd/Volo/Abp/DependencyInjection/CommonDbContextRegistrationOptions.cs b/src/Volo.Abp.Ddd/Volo/Abp/DependencyInjection/CommonDbContextRegistrationOptions.cs index cced597bd4..e195a348bf 100644 --- a/src/Volo.Abp.Ddd/Volo/Abp/DependencyInjection/CommonDbContextRegistrationOptions.cs +++ b/src/Volo.Abp.Ddd/Volo/Abp/DependencyInjection/CommonDbContextRegistrationOptions.cs @@ -1,6 +1,5 @@ using System; using System.Collections.Generic; -using JetBrains.Annotations; using Volo.Abp.Domain.Entities; using Volo.Abp.Domain.Repositories; using Volo.Abp.Reflection; @@ -20,7 +19,7 @@ namespace Volo.Abp.DependencyInjection public Type DefaultRepositoryImplementationType { get; private set; } - //public Type DefaultRepositoryImplementationTypeWithDefaultPrimaryKey { get; private set; } + public Type DefaultRepositoryImplementationTypeWithoutPrimaryKey { get; private set; } public bool RegisterDefaultRepositories { get; private set; } @@ -28,7 +27,7 @@ namespace Volo.Abp.DependencyInjection public Dictionary CustomRepositories { get; } - public bool SpecifiedDefaultRepositoryTypes => DefaultRepositoryImplementationType != null; // && DefaultRepositoryImplementationTypeWithDefaultPrimaryKey != null; + public bool SpecifiedDefaultRepositoryTypes => DefaultRepositoryImplementationType != null && DefaultRepositoryImplementationTypeWithoutPrimaryKey != null; protected CommonDbContextRegistrationOptions(Type originalDbContextType) { @@ -80,32 +79,37 @@ namespace Volo.Abp.DependencyInjection return this; } - public ICommonDbContextRegistrationOptionsBuilder AddCustomRepository() + public ICommonDbContextRegistrationOptionsBuilder AddRepository() { - WithCustomRepository(typeof(TEntity), typeof(TRepository)); + AddCustomRepository(typeof(TEntity), typeof(TRepository)); return this; } - public ICommonDbContextRegistrationOptionsBuilder SetDefaultRepositoryClasses([NotNull] Type repositoryImplementationType) + public ICommonDbContextRegistrationOptionsBuilder SetDefaultRepositoryClasses( + Type repositoryImplementationType, + Type repositoryImplementationTypeWithoutPrimaryKey + ) { Check.NotNull(repositoryImplementationType, nameof(repositoryImplementationType)); + Check.NotNull(repositoryImplementationTypeWithoutPrimaryKey, nameof(repositoryImplementationTypeWithoutPrimaryKey)); DefaultRepositoryImplementationType = repositoryImplementationType; + DefaultRepositoryImplementationTypeWithoutPrimaryKey = repositoryImplementationTypeWithoutPrimaryKey; return this; } - private void WithCustomRepository(Type entityType, Type repositoryType) + private void AddCustomRepository(Type entityType, Type repositoryType) { - if (!ReflectionHelper.IsAssignableToGenericType(entityType, typeof(IEntity<>))) + if (!typeof(IEntity).IsAssignableFrom(entityType)) { throw new AbpException($"Given entityType is not an entity: {entityType.AssemblyQualifiedName}. It must implement {typeof(IEntity<>).AssemblyQualifiedName}."); } - if (!ReflectionHelper.IsAssignableToGenericType(repositoryType, typeof(IRepository<,>))) + if (!ReflectionHelper.IsAssignableToGenericType(repositoryType, typeof(IRepository<>))) { - throw new AbpException($"Given repositoryType is not a repository: {entityType.AssemblyQualifiedName}. It must implement {typeof(IRepository<,>).AssemblyQualifiedName}."); + throw new AbpException($"Given repositoryType is not a repository: {entityType.AssemblyQualifiedName}. It must implement {typeof(IRepository<>).AssemblyQualifiedName}."); } CustomRepositories[entityType] = repositoryType; @@ -123,7 +127,7 @@ namespace Volo.Abp.DependencyInjection return false; } - if (!IncludeAllEntitiesForDefaultRepositories && !ReflectionHelper.IsAssignableToGenericType(entityType, typeof(IAggregateRoot<>))) + if (!IncludeAllEntitiesForDefaultRepositories && !typeof(IAggregateRoot).IsAssignableFrom(entityType)) { return false; } diff --git a/src/Volo.Abp.Ddd/Volo/Abp/DependencyInjection/ICommonDbContextRegistrationOptionsBuilder.cs b/src/Volo.Abp.Ddd/Volo/Abp/DependencyInjection/ICommonDbContextRegistrationOptionsBuilder.cs index b78c2d7e67..c68b544ce2 100644 --- a/src/Volo.Abp.Ddd/Volo/Abp/DependencyInjection/ICommonDbContextRegistrationOptionsBuilder.cs +++ b/src/Volo.Abp.Ddd/Volo/Abp/DependencyInjection/ICommonDbContextRegistrationOptionsBuilder.cs @@ -1,4 +1,5 @@ using System; +using JetBrains.Annotations; namespace Volo.Abp.DependencyInjection { @@ -41,14 +42,15 @@ namespace Volo.Abp.DependencyInjection /// /// Entity type /// Repository type - ICommonDbContextRegistrationOptionsBuilder AddCustomRepository(); //TODO: Rename to AddRepository! + ICommonDbContextRegistrationOptionsBuilder AddRepository(); /// /// Uses given class(es) for default repositories. /// /// Repository implementation type + /// Repository implementation type (without primary key) /// - ICommonDbContextRegistrationOptionsBuilder SetDefaultRepositoryClasses(Type repositoryImplementationType); + ICommonDbContextRegistrationOptionsBuilder SetDefaultRepositoryClasses([NotNull] Type repositoryImplementationType, [NotNull] Type repositoryImplementationTypeWithoutPrimaryKey); /// /// Replaces given DbContext type with this DbContext type. diff --git a/src/Volo.Abp.Ddd/Volo/Abp/Domain/Entities/EntityHelper.cs b/src/Volo.Abp.Ddd/Volo/Abp/Domain/Entities/EntityHelper.cs index 2dd23b4a08..3dee5b0ee5 100644 --- a/src/Volo.Abp.Ddd/Volo/Abp/Domain/Entities/EntityHelper.cs +++ b/src/Volo.Abp.Ddd/Volo/Abp/Domain/Entities/EntityHelper.cs @@ -1,8 +1,8 @@ using System; using System.Collections.Generic; +using System.Linq.Expressions; using System.Reflection; using JetBrains.Annotations; -using Volo.Abp.Reflection; namespace Volo.Abp.Domain.Entities { @@ -13,18 +13,9 @@ namespace Volo.Abp.Domain.Entities { public static bool IsEntity([NotNull] Type type) { - return ReflectionHelper.IsAssignableToGenericType(type, typeof (IEntity<>)); + return typeof(IEntity).IsAssignableFrom(type); } - /// - /// Default implementation of . - /// - /// This method is not used normally if given entity is derived from . - /// Just directly call . - /// - /// This method is exists to help developers who want to directly implement - /// but want to use default IsTransient implementation as a shortcut. - /// public static bool IsTransient(IEntity entity) // TODO: Completely remove IsTransient { if (EqualityComparer.Default.Equals(entity.Id, default)) @@ -46,25 +37,50 @@ namespace Volo.Abp.Domain.Entities return false; } - public static Type GetPrimaryKeyType() + /// + /// Tries to find the primary key type of the given entity type. + /// May return null if given type does not implement + /// + [CanBeNull] + public static Type FindPrimaryKeyType() + where TEntity : IEntity { - return GetPrimaryKeyType(typeof (TEntity)); + return FindPrimaryKeyType(typeof(TEntity)); } /// - /// Gets primary key type of given entity type + /// Tries to find the primary key type of the given entity type. + /// May return null if given type does not implement /// - public static Type GetPrimaryKeyType([NotNull] Type entityType) + [CanBeNull] + public static Type FindPrimaryKeyType([NotNull] Type entityType) { + if (!typeof(IEntity).IsAssignableFrom(entityType)) + { + throw new AbpException($"Given {nameof(entityType)} is not an entity. It should implement {typeof(IEntity).AssemblyQualifiedName}!"); + } + foreach (var interfaceType in entityType.GetTypeInfo().GetInterfaces()) { - if (interfaceType.GetTypeInfo().IsGenericType && interfaceType.GetGenericTypeDefinition() == typeof (IEntity<>)) + if (interfaceType.GetTypeInfo().IsGenericType && interfaceType.GetGenericTypeDefinition() == typeof(IEntity<>)) { return interfaceType.GenericTypeArguments[0]; } } - throw new AbpException("Can not find primary key type of given entity type: " + entityType + ". Be sure that this entity type implements " + typeof(IEntity<>).AssemblyQualifiedName); + return null; + } + + public static Expression> CreateEqualityExpressionForId(TPrimaryKey id) + where TEntity : IEntity + { + var lambdaParam = Expression.Parameter(typeof(TEntity)); + var lambdaBody = Expression.Equal( + Expression.PropertyOrField(lambdaParam, "Id"), + Expression.Constant(id, typeof(TPrimaryKey)) + ); + + return Expression.Lambda>(lambdaBody, lambdaParam); } } } \ No newline at end of file diff --git a/src/Volo.Abp.Ddd/Volo/Abp/Domain/Entities/EntityNotFoundException.cs b/src/Volo.Abp.Ddd/Volo/Abp/Domain/Entities/EntityNotFoundException.cs index a33a9b8a41..510d0384c2 100644 --- a/src/Volo.Abp.Ddd/Volo/Abp/Domain/Entities/EntityNotFoundException.cs +++ b/src/Volo.Abp.Ddd/Volo/Abp/Domain/Entities/EntityNotFoundException.cs @@ -25,6 +25,15 @@ namespace Volo.Abp.Domain.Entities } + /// + /// Creates a new object. + /// + public EntityNotFoundException(Type entityType) + : this(entityType, null, null) + { + + } + /// /// Creates a new object. /// diff --git a/src/Volo.Abp.Ddd/Volo/Abp/Domain/Entities/IAggregateRoot.cs b/src/Volo.Abp.Ddd/Volo/Abp/Domain/Entities/IAggregateRoot.cs index 51d51a9677..5050df0cca 100644 --- a/src/Volo.Abp.Ddd/Volo/Abp/Domain/Entities/IAggregateRoot.cs +++ b/src/Volo.Abp.Ddd/Volo/Abp/Domain/Entities/IAggregateRoot.cs @@ -13,7 +13,7 @@ /// Defines an aggregate root with a single primary key with "Id" property. /// /// Type of the primary key of the entity - public interface IAggregateRoot : IAggregateRoot, IEntity + public interface IAggregateRoot : IEntity, IAggregateRoot { } diff --git a/src/Volo.Abp.Ddd/Volo/Abp/Domain/Repositories/QueryableRepositoryBase.cs b/src/Volo.Abp.Ddd/Volo/Abp/Domain/Repositories/QueryableRepositoryBase.cs index c3f67b0b33..00ef2c07be 100644 --- a/src/Volo.Abp.Ddd/Volo/Abp/Domain/Repositories/QueryableRepositoryBase.cs +++ b/src/Volo.Abp.Ddd/Volo/Abp/Domain/Repositories/QueryableRepositoryBase.cs @@ -14,6 +14,10 @@ namespace Volo.Abp.Domain.Repositories public abstract class QueryableRepositoryBase : RepositoryBase, IQueryableRepository where TEntity : class, IEntity { + public IDataFilter DataFilter { get; set; } + + public ICurrentTenant CurrentTenant { get; set; } + public virtual Type ElementType => GetQueryable().ElementType; public virtual Expression Expression => GetQueryable().Expression; @@ -45,18 +49,31 @@ namespace Volo.Abp.Domain.Repositories Delete(predicate); return Task.CompletedTask; } + + //TODO: Is that needed..? + protected virtual IQueryable ApplyDataFilters(IQueryable query) + { + if (typeof(ISoftDelete).IsAssignableFrom(typeof(TEntity))) + { + query = query.WhereIf(DataFilter.IsEnabled(), e => ((ISoftDelete)e).IsDeleted == false); + } + + if (typeof(IMultiTenant).IsAssignableFrom(typeof(TEntity))) + { + var tenantId = CurrentTenant.Id; + query = query.WhereIf(DataFilter.IsEnabled(), e => ((IMultiTenant)e).TenantId == tenantId); + } + + return query; + } } public abstract class QueryableRepositoryBase : QueryableRepositoryBase, IQueryableRepository where TEntity : class, IEntity { - public IDataFilter DataFilter { get; set; } - - public ICurrentTenant CurrentTenant { get; set; } - public virtual TEntity Find(TPrimaryKey id) { - return GetQueryable().FirstOrDefault(CreateEqualityExpressionForId(id)); + return GetQueryable().FirstOrDefault(EntityHelper.CreateEqualityExpressionForId(id)); } public virtual TEntity Get(TPrimaryKey id) @@ -97,32 +114,5 @@ namespace Volo.Abp.Domain.Repositories Delete(id); return Task.CompletedTask; } - - protected virtual IQueryable ApplyDataFilters(IQueryable query) - { - if (typeof(ISoftDelete).IsAssignableFrom(typeof(TEntity))) - { - query = query.WhereIf(DataFilter.IsEnabled(), e => ((ISoftDelete)e).IsDeleted == false); - } - - if (typeof(IMultiTenant).IsAssignableFrom(typeof(TEntity))) - { - var tenantId = CurrentTenant.Id; - query = query.WhereIf(DataFilter.IsEnabled(), e => ((IMultiTenant)e).TenantId == tenantId); - } - - return query; - } - - protected static Expression> CreateEqualityExpressionForId(TPrimaryKey id) - { - var lambdaParam = Expression.Parameter(typeof(TEntity)); - var lambdaBody = Expression.Equal( - Expression.PropertyOrField(lambdaParam, "Id"), - Expression.Constant(id, typeof(TPrimaryKey)) - ); - - return Expression.Lambda>(lambdaBody, lambdaParam); - } } } \ 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 544da02f10..9b5510de89 100644 --- a/src/Volo.Abp.Ddd/Volo/Abp/Domain/Repositories/RepositoryRegistrarBase.cs +++ b/src/Volo.Abp.Ddd/Volo/Abp/Domain/Repositories/RepositoryRegistrarBase.cs @@ -3,7 +3,6 @@ using System.Collections.Generic; using Microsoft.Extensions.DependencyInjection; using Volo.Abp.DependencyInjection; using Volo.Abp.Domain.Entities; -using Volo.Abp.Reflection; namespace Volo.Abp.Domain.Repositories { @@ -45,26 +44,26 @@ namespace Volo.Abp.Domain.Repositories protected void RegisterDefaultRepository(IServiceCollection services, Type entityType) { - var primaryKeyType = EntityHelper.GetPrimaryKeyType(entityType); - //var isDefaultPrimaryKey = primaryKeyType == typeof(Guid); + services.AddDefaultRepository( + entityType, + GetDefaultRepositoryImplementationType(entityType) + ); + } - Type repositoryImplementationType; - if (Options.SpecifiedDefaultRepositoryTypes) - { - repositoryImplementationType = Options.DefaultRepositoryImplementationType.MakeGenericType(entityType, primaryKeyType); - //repositoryImplementationType = isDefaultPrimaryKey - // ? Options.DefaultRepositoryImplementationTypeWithDefaultPrimaryKey.MakeGenericType(entityType) - // : Options.DefaultRepositoryImplementationType.MakeGenericType(entityType, primaryKeyType); - } - else + private Type GetDefaultRepositoryImplementationType(Type entityType) + { + var primaryKeyType = EntityHelper.FindPrimaryKeyType(entityType); + + if (primaryKeyType == null) { - repositoryImplementationType = GetRepositoryType(Options.DefaultRepositoryDbContextType, entityType, primaryKeyType); - //repositoryImplementationType = isDefaultPrimaryKey - // ? GetRepositoryTypeForDefaultPk(Options.DefaultRepositoryDbContextType, entityType) - // : GetRepositoryType(Options.DefaultRepositoryDbContextType, entityType, primaryKeyType); + return Options.SpecifiedDefaultRepositoryTypes + ? Options.DefaultRepositoryImplementationTypeWithoutPrimaryKey.MakeGenericType(entityType) + : GetRepositoryType(Options.DefaultRepositoryDbContextType, entityType); } - services.AddDefaultRepository(entityType, repositoryImplementationType); + return Options.SpecifiedDefaultRepositoryTypes + ? Options.DefaultRepositoryImplementationType.MakeGenericType(entityType, primaryKeyType) + : GetRepositoryType(Options.DefaultRepositoryDbContextType, entityType, primaryKeyType); } public bool ShouldRegisterDefaultRepositoryFor(Type entityType) @@ -79,7 +78,7 @@ namespace Volo.Abp.Domain.Repositories return false; } - if (!Options.IncludeAllEntitiesForDefaultRepositories && !ReflectionHelper.IsAssignableToGenericType(entityType, typeof(IAggregateRoot<>))) + if (!Options.IncludeAllEntitiesForDefaultRepositories && !typeof(IAggregateRoot).IsAssignableFrom(entityType)) { return false; } @@ -89,7 +88,7 @@ namespace Volo.Abp.Domain.Repositories protected abstract IEnumerable GetEntityTypes(Type dbContextType); - //protected abstract Type GetRepositoryTypeForDefaultPk(Type dbContextType, Type entityType); + protected abstract Type GetRepositoryType(Type dbContextType, Type entityType); protected abstract Type GetRepositoryType(Type dbContextType, Type entityType, Type primaryKeyType); } diff --git a/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/Domain/Repositories/EntityFrameworkCore/EfCoreRepository.cs b/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/Domain/Repositories/EntityFrameworkCore/EfCoreRepository.cs index 698df13738..9bd7245b7a 100644 --- a/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/Domain/Repositories/EntityFrameworkCore/EfCoreRepository.cs +++ b/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/Domain/Repositories/EntityFrameworkCore/EfCoreRepository.cs @@ -7,30 +7,16 @@ using System.Threading.Tasks; using Microsoft.EntityFrameworkCore; using Volo.Abp.Domain.Entities; using Volo.Abp.EntityFrameworkCore; -using Volo.Abp.Threading; namespace Volo.Abp.Domain.Repositories.EntityFrameworkCore { - //public class EfCoreRepository : EfCoreRepository, IEfCoreRepository - // where TDbContext : IEfCoreDbContext - // where TEntity : class, IEntity - //{ - // public EfCoreRepository(IDbContextProvider dbContextProvider) - // : base(dbContextProvider) - // { - // } - //} - - public class EfCoreRepository : QueryableRepositoryBase, - IEfCoreRepository, - ISupportsExplicitLoading - + public class EfCoreRepository : QueryableRepositoryBase, IEfCoreRepository where TDbContext : IEfCoreDbContext - where TEntity : class, IEntity + where TEntity : class, IEntity { public virtual DbSet DbSet => DbContext.Set(); - DbContext IEfCoreRepository.DbContext => DbContext.As(); + DbContext IEfCoreRepository.DbContext => DbContext.As(); protected virtual TDbContext DbContext => _dbContextProvider.GetDbContext(); @@ -40,34 +26,7 @@ namespace Volo.Abp.Domain.Repositories.EntityFrameworkCore { _dbContextProvider = dbContextProvider; } - - protected override IQueryable GetQueryable() - { - return DbSet.AsQueryable(); - } - - public override async Task GetAsync(TPrimaryKey id, CancellationToken cancellationToken = default) - { - var entity = await FindAsync(id, GetCancellationToken(cancellationToken)); - - if (entity == null) - { - throw new EntityNotFoundException(typeof(TEntity), id); - } - - return entity; - } - - public override TEntity Find(TPrimaryKey id) - { - return DbSet.Find(id); - } - - public override Task FindAsync(TPrimaryKey id, CancellationToken cancellationToken = default) - { - return DbSet.FindAsync(new object[] { id }, GetCancellationToken(cancellationToken)); - } - + public override TEntity Insert(TEntity entity, bool autoSave = false) { var savedEntity = DbSet.Add(entity).Entity; @@ -103,6 +62,11 @@ namespace Volo.Abp.Domain.Repositories.EntityFrameworkCore DbSet.Remove(entity); } + protected override IQueryable GetQueryable() + { + return DbSet.AsQueryable(); + } + public override async Task DeleteAsync(Expression> predicate, CancellationToken cancellationToken = default) { var entities = await GetQueryable().Where(predicate).ToListAsync(GetCancellationToken(cancellationToken)); @@ -130,4 +94,69 @@ namespace Volo.Abp.Domain.Repositories.EntityFrameworkCore return DbContext.Entry(entity).Reference(propertyExpression).LoadAsync(GetCancellationToken(cancellationToken)); } } + + public class EfCoreRepository : EfCoreRepository, + IEfCoreRepository, + ISupportsExplicitLoading + + where TDbContext : IEfCoreDbContext + where TEntity : class, IEntity + { + public EfCoreRepository(IDbContextProvider dbContextProvider) + : base(dbContextProvider) + { + + } + + public TEntity Get(TPrimaryKey id) + { + var entity = Find(id); + + if (entity == null) + { + throw new EntityNotFoundException(typeof(TEntity), id); + } + + return entity; + } + + public virtual async Task GetAsync(TPrimaryKey id, CancellationToken cancellationToken = default) + { + var entity = await FindAsync(id, GetCancellationToken(cancellationToken)); + + if (entity == null) + { + throw new EntityNotFoundException(typeof(TEntity), id); + } + + return entity; + } + + public virtual TEntity Find(TPrimaryKey id) + { + return DbSet.Find(id); + } + + public virtual Task FindAsync(TPrimaryKey id, CancellationToken cancellationToken = default) + { + return DbSet.FindAsync(new object[] { id }, GetCancellationToken(cancellationToken)); + } + + public virtual void Delete(TPrimaryKey id) + { + var entity = Find(id); + if (entity == null) + { + return; + } + + Delete(entity); + } + + public virtual Task DeleteAsync(TPrimaryKey id, CancellationToken cancellationToken = default) + { + Delete(id); + return Task.CompletedTask; + } + } } diff --git a/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/Domain/Repositories/EntityFrameworkCore/IEfCoreRepository.cs b/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/Domain/Repositories/EntityFrameworkCore/IEfCoreRepository.cs index d32912d1d4..8232f02fcd 100644 --- a/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/Domain/Repositories/EntityFrameworkCore/IEfCoreRepository.cs +++ b/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/Domain/Repositories/EntityFrameworkCore/IEfCoreRepository.cs @@ -1,20 +1,19 @@ -using System; using Microsoft.EntityFrameworkCore; using Volo.Abp.Domain.Entities; namespace Volo.Abp.Domain.Repositories.EntityFrameworkCore { - //public interface IEfCoreRepository : IEfCoreRepository, IQueryableRepository - // where TEntity : class, IEntity - //{ - - //} - - public interface IEfCoreRepository : IQueryableRepository - where TEntity : class, IEntity + public interface IEfCoreRepository : IQueryableRepository + where TEntity : class, IEntity { DbContext DbContext { get; } DbSet DbSet { get; } } + + public interface IEfCoreRepository : IEfCoreRepository, IQueryableRepository + where TEntity : class, IEntity + { + + } } \ No newline at end of file diff --git a/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/DbContextHelper.cs b/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/DbContextHelper.cs index 06887c83c8..7c3a8707d4 100644 --- a/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/DbContextHelper.cs +++ b/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/DbContextHelper.cs @@ -16,7 +16,7 @@ namespace Volo.Abp.EntityFrameworkCore from property in dbContextType.GetTypeInfo().GetProperties(BindingFlags.Public | BindingFlags.Instance) where ReflectionHelper.IsAssignableToGenericType(property.PropertyType, typeof(DbSet<>)) && - ReflectionHelper.IsAssignableToGenericType(property.PropertyType.GenericTypeArguments[0], typeof(IEntity<>)) + typeof(IEntity).IsAssignableFrom(property.PropertyType.GenericTypeArguments[0]) select property.PropertyType.GenericTypeArguments[0]; } } diff --git a/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/DependencyInjection/EfCoreRepositoryRegistrar.cs b/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/DependencyInjection/EfCoreRepositoryRegistrar.cs index db0dcdc13f..f75c4a0132 100644 --- a/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/DependencyInjection/EfCoreRepositoryRegistrar.cs +++ b/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/DependencyInjection/EfCoreRepositoryRegistrar.cs @@ -17,10 +17,10 @@ namespace Volo.Abp.EntityFrameworkCore.DependencyInjection return DbContextHelper.GetEntityTypes(dbContextType); } - //protected override Type GetRepositoryTypeForDefaultPk(Type dbContextType, Type entityType) - //{ - // return typeof(EfCoreRepository<,>).MakeGenericType(dbContextType, entityType); - //} + protected override Type GetRepositoryType(Type dbContextType, Type entityType) + { + return typeof(EfCoreRepository<,>).MakeGenericType(dbContextType, entityType); + } protected override Type GetRepositoryType(Type dbContextType, Type entityType, Type primaryKeyType) { 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 fb3d42cb30..9951d6564b 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 @@ -12,8 +12,8 @@ namespace Volo.Abp.Identity.EntityFrameworkCore services.AddAbpDbContext(options => { options.AddDefaultRepositories(); - options.AddCustomRepository(); - options.AddCustomRepository(); + options.AddRepository(); + options.AddRepository(); }); 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 1d53416680..d27afaf01d 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 @@ -14,7 +14,7 @@ namespace Volo.Abp.IdentityServer.EntityFrameworkCore services.AddAbpDbContext(options => { options.AddDefaultRepositories(); - options.AddCustomRepository(); + options.AddRepository(); }); services.AddAssemblyOf(); diff --git a/src/Volo.Abp.MemoryDb/Volo/Abp/Domain/Repositories/MemoryDb/IMemoryDbRepository.cs b/src/Volo.Abp.MemoryDb/Volo/Abp/Domain/Repositories/MemoryDb/IMemoryDbRepository.cs index 8eba8ede94..8d43180088 100644 --- a/src/Volo.Abp.MemoryDb/Volo/Abp/Domain/Repositories/MemoryDb/IMemoryDbRepository.cs +++ b/src/Volo.Abp.MemoryDb/Volo/Abp/Domain/Repositories/MemoryDb/IMemoryDbRepository.cs @@ -1,20 +1,19 @@ -using System; -using System.Collections.Generic; +using System.Collections.Generic; using Volo.Abp.Domain.Entities; namespace Volo.Abp.Domain.Repositories.MemoryDb { - //public interface IMemoryDbRepository : IMemoryDbRepository, IQueryableRepository - // where TEntity : class, IEntity - //{ - - //} - - public interface IMemoryDbRepository : IQueryableRepository - where TEntity : class, IEntity + public interface IMemoryDbRepository : IQueryableRepository + where TEntity : class, IEntity { IMemoryDatabase Database { get; } List Collection { get; } } + + public interface IMemoryDbRepository : IMemoryDbRepository, IQueryableRepository + where TEntity : class, IEntity + { + + } } diff --git a/src/Volo.Abp.MemoryDb/Volo/Abp/Domain/Repositories/MemoryDb/MemoryDbRepository.cs b/src/Volo.Abp.MemoryDb/Volo/Abp/Domain/Repositories/MemoryDb/MemoryDbRepository.cs index bd2a03b4bb..acd37028a4 100644 --- a/src/Volo.Abp.MemoryDb/Volo/Abp/Domain/Repositories/MemoryDb/MemoryDbRepository.cs +++ b/src/Volo.Abp.MemoryDb/Volo/Abp/Domain/Repositories/MemoryDb/MemoryDbRepository.cs @@ -1,24 +1,16 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Threading; +using System.Threading.Tasks; using Volo.Abp.Domain.Entities; using Volo.Abp.MemoryDb; namespace Volo.Abp.Domain.Repositories.MemoryDb { - //public class MemoryDbRepository : MemoryDbRepository, IMemoryDbRepository - // where TMemoryDbContext : MemoryDbContext - // where TEntity : class, IEntity - //{ - // public MemoryDbRepository(IMemoryDatabaseProvider databaseProvider) - // : base(databaseProvider) - // { - // } - //} - - public class MemoryDbRepository : QueryableRepositoryBase, IMemoryDbRepository + public class MemoryDbRepository : QueryableRepositoryBase, IMemoryDbRepository where TMemoryDbContext : MemoryDbContext - where TEntity : class, IEntity + where TEntity : class, IEntity { public virtual List Collection => Database.Collection(); @@ -33,11 +25,41 @@ namespace Volo.Abp.Domain.Repositories.MemoryDb public override TEntity Insert(TEntity entity, bool autoSave = false) { - SetIdIfNeeded(entity); Collection.Add(entity); return entity; } + public override TEntity Update(TEntity entity) + { + return entity; + } + + public override void Delete(TEntity entity) + { + Collection.Remove(entity); + } + + protected override IQueryable GetQueryable() + { + return ApplyDataFilters(Collection.AsQueryable()); + } + } + + public class MemoryDbRepository : MemoryDbRepository, IMemoryDbRepository + where TMemoryDbContext : MemoryDbContext + where TEntity : class, IEntity + { + public MemoryDbRepository(IMemoryDatabaseProvider databaseProvider) + : base(databaseProvider) + { + } + + public override TEntity Insert(TEntity entity, bool autoSave = false) + { + SetIdIfNeeded(entity); + return base.Insert(entity, autoSave); + } + private void SetIdIfNeeded(TEntity entity) { if (typeof(TPrimaryKey) == typeof(int) || typeof(TPrimaryKey) == typeof(long) || typeof(TPrimaryKey) == typeof(Guid)) @@ -49,19 +71,48 @@ namespace Volo.Abp.Domain.Repositories.MemoryDb } } - public override TEntity Update(TEntity entity) + public virtual TEntity Find(TPrimaryKey id) { + return GetQueryable().FirstOrDefault(EntityHelper.CreateEqualityExpressionForId(id)); + } + + public virtual TEntity Get(TPrimaryKey id) + { + var entity = Find(id); + + if (entity == null) + { + throw new EntityNotFoundException(typeof(TEntity), id); + } + return entity; } - - public override void Delete(TEntity entity) + + public virtual Task GetAsync(TPrimaryKey id, CancellationToken cancellationToken = default) { - Collection.Remove(entity); + return Task.FromResult(Get(id)); } - protected override IQueryable GetQueryable() + public virtual Task FindAsync(TPrimaryKey id, CancellationToken cancellationToken = default) { - return ApplyDataFilters(Collection.AsQueryable()); + return Task.FromResult(Find(id)); + } + + public virtual void Delete(TPrimaryKey id) + { + var entity = Find(id); + if (entity == null) + { + return; + } + + Delete(entity); + } + + public virtual Task DeleteAsync(TPrimaryKey id, CancellationToken cancellationToken = default) + { + Delete(id); + return Task.CompletedTask; } } } \ No newline at end of file diff --git a/src/Volo.Abp.MemoryDb/Volo/Abp/MemoryDb/DependencyInjection/MemoryDbRepositoryRegistrar.cs b/src/Volo.Abp.MemoryDb/Volo/Abp/MemoryDb/DependencyInjection/MemoryDbRepositoryRegistrar.cs index 08fa5d7a8f..fa238a780d 100644 --- a/src/Volo.Abp.MemoryDb/Volo/Abp/MemoryDb/DependencyInjection/MemoryDbRepositoryRegistrar.cs +++ b/src/Volo.Abp.MemoryDb/Volo/Abp/MemoryDb/DependencyInjection/MemoryDbRepositoryRegistrar.cs @@ -18,6 +18,11 @@ namespace Volo.Abp.MemoryDb.DependencyInjection return memoryDbContext.GetEntityTypes(); } + protected override Type GetRepositoryType(Type dbContextType, Type entityType) + { + return typeof(MemoryDbRepository<,>).MakeGenericType(dbContextType, entityType); + } + protected override Type GetRepositoryType(Type dbContextType, Type entityType, Type primaryKeyType) { return typeof(MemoryDbRepository<,,>).MakeGenericType(dbContextType, entityType, primaryKeyType); diff --git a/src/Volo.Abp.MongoDB/Volo/Abp/MongoDB/DependencyInjection/MongoDbRepositoryRegistrar.cs b/src/Volo.Abp.MongoDB/Volo/Abp/MongoDB/DependencyInjection/MongoDbRepositoryRegistrar.cs index 9b94969a59..6d9d76be77 100644 --- a/src/Volo.Abp.MongoDB/Volo/Abp/MongoDB/DependencyInjection/MongoDbRepositoryRegistrar.cs +++ b/src/Volo.Abp.MongoDB/Volo/Abp/MongoDB/DependencyInjection/MongoDbRepositoryRegistrar.cs @@ -19,6 +19,11 @@ namespace Volo.Abp.MongoDB.DependencyInjection return mongoDbContext.GetMappings().Select(m => m.EntityType); } + protected override Type GetRepositoryType(Type dbContextType, Type entityType) + { + throw new NotImplementedException(); + } + protected override Type GetRepositoryType(Type dbContextType, Type entityType, Type primaryKeyType) { return typeof(MongoDbRepository<,,>).MakeGenericType(dbContextType, entityType, primaryKeyType); diff --git a/test/Volo.Abp.Ddd.Tests/Volo.Abp.Ddd.Tests.csproj b/test/Volo.Abp.Ddd.Tests/Volo.Abp.Ddd.Tests.csproj index e3fd3e19f6..faf42e147e 100644 --- a/test/Volo.Abp.Ddd.Tests/Volo.Abp.Ddd.Tests.csproj +++ b/test/Volo.Abp.Ddd.Tests/Volo.Abp.Ddd.Tests.csproj @@ -2,6 +2,7 @@ netcoreapp2.0 + latest Volo.Abp.Ddd.Tests Volo.Abp.Ddd.Tests true diff --git a/test/Volo.Abp.Ddd.Tests/Volo.Abp.Ddd.Tests.csproj.DotSettings b/test/Volo.Abp.Ddd.Tests/Volo.Abp.Ddd.Tests.csproj.DotSettings new file mode 100644 index 0000000000..58ad6c8854 --- /dev/null +++ b/test/Volo.Abp.Ddd.Tests/Volo.Abp.Ddd.Tests.csproj.DotSettings @@ -0,0 +1,2 @@ + + CSharp71 \ No newline at end of file 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 f5554f00b0..372f3fb6b9 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 @@ -1,5 +1,7 @@ using System; using System.Collections.Generic; +using System.Threading; +using System.Threading.Tasks; using Microsoft.Extensions.DependencyInjection; using Volo.Abp.DependencyInjection; using Volo.Abp.Domain.Entities; @@ -25,8 +27,10 @@ namespace Volo.Abp.Domain.Repositories //Assert - //services.ShouldContainTransient(typeof(IRepository), typeof(MyTestDefaultRepository)); + services.ShouldContainTransient(typeof(IRepository), typeof(MyTestDefaultRepository)); + services.ShouldContainTransient(typeof(IRepository), typeof(MyTestDefaultRepository)); services.ShouldContainTransient(typeof(IRepository), typeof(MyTestDefaultRepository)); + services.ShouldNotContainService(typeof(IRepository)); } @@ -46,8 +50,10 @@ namespace Volo.Abp.Domain.Repositories //Assert - //services.ShouldContainTransient(typeof(IRepository), typeof(MyTestDefaultRepository)); + services.ShouldContainTransient(typeof(IRepository), typeof(MyTestDefaultRepository)); + services.ShouldContainTransient(typeof(IRepository), typeof(MyTestDefaultRepository)); services.ShouldContainTransient(typeof(IRepository), typeof(MyTestDefaultRepository)); + services.ShouldContainTransient(typeof(IRepository), typeof(MyTestDefaultRepository)); services.ShouldContainTransient(typeof(IRepository), typeof(MyTestDefaultRepository)); } @@ -61,7 +67,7 @@ namespace Volo.Abp.Domain.Repositories var options = new TestDbContextRegistrationOptions(typeof(MyFakeDbContext)); options .AddDefaultRepositories(true) - .AddCustomRepository(); + .AddRepository(); //Act @@ -69,7 +75,8 @@ namespace Volo.Abp.Domain.Repositories //Assert - //services.ShouldContainTransient(typeof(IRepository), typeof(MyTestAggregateRootWithDefaultPkCustomRepository)); + services.ShouldContainTransient(typeof(IRepository), typeof(MyTestDefaultRepository)); + services.ShouldContainTransient(typeof(IRepository), typeof(MyTestAggregateRootWithDefaultPkCustomRepository)); services.ShouldContainTransient(typeof(IRepository), typeof(MyTestAggregateRootWithDefaultPkCustomRepository)); services.ShouldContainTransient(typeof(IRepository), typeof(MyTestDefaultRepository)); } @@ -84,7 +91,7 @@ namespace Volo.Abp.Domain.Repositories var options = new TestDbContextRegistrationOptions(typeof(MyFakeDbContext)); options .AddDefaultRepositories(true) - .SetDefaultRepositoryClasses(typeof(MyTestCustomBaseRepository<,>)); + .SetDefaultRepositoryClasses(typeof(MyTestCustomBaseRepository<,>), typeof(MyTestCustomBaseRepository<>)); //Act @@ -92,14 +99,15 @@ namespace Volo.Abp.Domain.Repositories //Assert - //services.ShouldContainTransient(typeof(IRepository), typeof(MyTestCustomBaseRepository)); + services.ShouldContainTransient(typeof(IRepository), typeof(MyTestCustomBaseRepository)); + services.ShouldContainTransient(typeof(IRepository), typeof(MyTestCustomBaseRepository)); services.ShouldContainTransient(typeof(IRepository), typeof(MyTestCustomBaseRepository)); services.ShouldContainTransient(typeof(IRepository), typeof(MyTestCustomBaseRepository)); } public class MyTestRepositoryRegistrar : RepositoryRegistrarBase { - public MyTestRepositoryRegistrar(CommonDbContextRegistrationOptions options) + public MyTestRepositoryRegistrar(CommonDbContextRegistrationOptions options) : base(options) { } @@ -109,14 +117,15 @@ namespace Volo.Abp.Domain.Repositories return new[] { typeof(MyTestEntityWithInt32Pk), - typeof(MyTestAggregateRootWithGuidPk) + typeof(MyTestAggregateRootWithGuidPk), + typeof(MyTestAggregateRootWithoutPk) }; } - //protected override Type GetRepositoryTypeForDefaultPk(Type dbContextType, Type entityType) - //{ - // return typeof(MyTestDefaultRepository<>).MakeGenericType(entityType); - //} + protected override Type GetRepositoryType(Type dbContextType, Type entityType) + { + return typeof(MyTestDefaultRepository<>).MakeGenericType(entityType); + } protected override Type GetRepositoryType(Type dbContextType, Type entityType, Type primaryKeyType) { @@ -128,7 +137,7 @@ namespace Volo.Abp.Domain.Repositories public class MyTestAggregateRootWithGuidPk : AggregateRoot { - + } public class MyTestEntityWithInt32Pk : Entity @@ -136,31 +145,59 @@ namespace Volo.Abp.Domain.Repositories } - //public class MyTestDefaultRepository : MyTestDefaultRepository - // where TEntity : class, IEntity - //{ - - //} + public class MyTestAggregateRootWithoutPk : AggregateRoot + { + public string MyId { get; set; } + } + + public class MyTestDefaultRepository : RepositoryBase + where TEntity : class, IEntity + { + public override TEntity Insert(TEntity entity, bool autoSave = false) + { + throw new NotImplementedException(); + } + + public override TEntity Update(TEntity entity) + { + throw new NotImplementedException(); + } + + public override void Delete(TEntity entity) + { + throw new NotImplementedException(); + } + } - public class MyTestDefaultRepository : RepositoryBase + public class MyTestDefaultRepository : MyTestDefaultRepository, IRepository where TEntity : class, IEntity { - public override TEntity Find(TPrimaryKey id) + public TEntity Get(TPrimaryKey id) { throw new NotImplementedException(); } - public override TEntity Insert(TEntity entity, bool autoSave = false) + public Task GetAsync(TPrimaryKey id, CancellationToken cancellationToken = default) { throw new NotImplementedException(); } - public override TEntity Update(TEntity entity) + public TEntity Find(TPrimaryKey id) { throw new NotImplementedException(); } - public override void Delete(TEntity entity) + public Task FindAsync(TPrimaryKey id, CancellationToken cancellationToken = default) + { + throw new NotImplementedException(); + } + + public void Delete(TPrimaryKey id) + { + throw new NotImplementedException(); + } + + public Task DeleteAsync(TPrimaryKey id, CancellationToken cancellationToken = default) { throw new NotImplementedException(); } @@ -171,6 +208,12 @@ namespace Volo.Abp.Domain.Repositories } + public class MyTestCustomBaseRepository : MyTestDefaultRepository + where TEntity : class, IEntity + { + + } + public class MyTestCustomBaseRepository : MyTestDefaultRepository where TEntity : class, IEntity { @@ -179,7 +222,7 @@ namespace Volo.Abp.Domain.Repositories public class TestDbContextRegistrationOptions : CommonDbContextRegistrationOptions { - public TestDbContextRegistrationOptions(Type originalDbContextType) + public TestDbContextRegistrationOptions(Type originalDbContextType) : base(originalDbContextType) { } diff --git a/test/Volo.Abp.EntityFrameworkCore.Tests.SecondContext/Volo/Abp/EntityFrameworkCore/TestApp/SecondContext/PhoneInSecondDbContext.cs b/test/Volo.Abp.EntityFrameworkCore.Tests.SecondContext/Volo/Abp/EntityFrameworkCore/TestApp/SecondContext/PhoneInSecondDbContext.cs index 7c4c317c00..568c8b6ba2 100644 --- a/test/Volo.Abp.EntityFrameworkCore.Tests.SecondContext/Volo/Abp/EntityFrameworkCore/TestApp/SecondContext/PhoneInSecondDbContext.cs +++ b/test/Volo.Abp.EntityFrameworkCore.Tests.SecondContext/Volo/Abp/EntityFrameworkCore/TestApp/SecondContext/PhoneInSecondDbContext.cs @@ -1,11 +1,14 @@ -using System.ComponentModel.DataAnnotations.Schema; +using System; +using System.ComponentModel.DataAnnotations.Schema; using Volo.Abp.Domain.Entities; namespace Volo.Abp.EntityFrameworkCore.TestApp.SecondContext { [Table("AppPhones")] - public class PhoneInSecondDbContext : AggregateRoot + public class PhoneInSecondDbContext : AggregateRoot { + public virtual Guid PersonId { get; set; } + public virtual string Number { get; set; } } } \ No newline at end of file diff --git a/test/Volo.Abp.EntityFrameworkCore.Tests.SecondContext/Volo/Abp/EntityFrameworkCore/TestApp/SecondContext/SecondDbContext.cs b/test/Volo.Abp.EntityFrameworkCore.Tests.SecondContext/Volo/Abp/EntityFrameworkCore/TestApp/SecondContext/SecondDbContext.cs index d2b54bc699..f07ffbc5a3 100644 --- a/test/Volo.Abp.EntityFrameworkCore.Tests.SecondContext/Volo/Abp/EntityFrameworkCore/TestApp/SecondContext/SecondDbContext.cs +++ b/test/Volo.Abp.EntityFrameworkCore.Tests.SecondContext/Volo/Abp/EntityFrameworkCore/TestApp/SecondContext/SecondDbContext.cs @@ -12,5 +12,15 @@ namespace Volo.Abp.EntityFrameworkCore.TestApp.SecondContext : base(options) { } + + protected override void OnModelCreating(ModelBuilder modelBuilder) + { + base.OnModelCreating(modelBuilder); + + modelBuilder.Entity(b => + { + b.HasKey(p => new { p.PersonId, p.Number }); + }); + } } } \ 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 b6b77d26f8..9496844b0b 100644 --- a/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/AbpEntityFrameworkCoreTestModule.cs +++ b/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/AbpEntityFrameworkCoreTestModule.cs @@ -22,7 +22,7 @@ namespace Volo.Abp.EntityFrameworkCore services.AddAbpDbContext(options => { - options.AddDefaultRepositories(); + options.AddDefaultRepositories(true); options.ReplaceDbContext(); }); diff --git a/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/Repositories/Basic_Repository_Tests.cs b/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/Repositories/Repository_Tests.cs similarity index 89% rename from test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/Repositories/Basic_Repository_Tests.cs rename to test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/Repositories/Repository_Tests.cs index 480a698cb2..7a98204652 100644 --- a/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/Repositories/Basic_Repository_Tests.cs +++ b/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/Repositories/Repository_Tests.cs @@ -10,17 +10,17 @@ using Xunit; namespace Volo.Abp.EntityFrameworkCore.Repositories { - public class Basic_Repository_Tests : EntityFrameworkCoreTestBase + public class Repository_Tests : EntityFrameworkCoreTestBase { private readonly IQueryableRepository _personRepository; private readonly IQueryableRepository _bookRepository; - private readonly IQueryableRepository _phoneInSecondDbContextRepository; + private readonly IQueryableRepository _phoneInSecondDbContextRepository; - public Basic_Repository_Tests() + public Repository_Tests() { _personRepository = ServiceProvider.GetRequiredService>(); _bookRepository = ServiceProvider.GetRequiredService>(); - _phoneInSecondDbContextRepository = ServiceProvider.GetRequiredService>(); + _phoneInSecondDbContextRepository = ServiceProvider.GetRequiredService>(); } [Fact]