diff --git a/src/Volo.Abp.MongoDB/Volo/Abp/Domain/Repositories/MongoDB/MongoDbRepository.cs b/src/Volo.Abp.MongoDB/Volo/Abp/Domain/Repositories/MongoDB/MongoDbRepository.cs index 30956a3e43..647081ce04 100644 --- a/src/Volo.Abp.MongoDB/Volo/Abp/Domain/Repositories/MongoDB/MongoDbRepository.cs +++ b/src/Volo.Abp.MongoDB/Volo/Abp/Domain/Repositories/MongoDB/MongoDbRepository.cs @@ -9,12 +9,13 @@ using MongoDB.Driver.Linq; using Volo.Abp.Domain.Entities; using Volo.Abp.Domain.Entities.Events; using Volo.Abp.EventBus; +using Volo.Abp.Guids; using Volo.Abp.MongoDB; using Volo.Abp.MultiTenancy; namespace Volo.Abp.Domain.Repositories.MongoDB { - public class MongoDbRepository + public class MongoDbRepository : RepositoryBase, IMongoDbRepository where TMongoDbContext : IAbpMongoDbContext @@ -32,6 +33,8 @@ namespace Volo.Abp.Domain.Repositories.MongoDB public IEntityChangeEventHelper EntityChangeEventHelper { get; set; } + public IGuidGenerator GuidGenerator { get; set; } + public MongoDbRepository(IMongoDbContextProvider dbContextProvider) { DbContextProvider = dbContextProvider; @@ -55,8 +58,8 @@ namespace Volo.Abp.Domain.Repositories.MongoDB } public override async Task InsertAsync( - TEntity entity, - bool autoSave = false, + TEntity entity, + bool autoSave = false, CancellationToken cancellationToken = default) { ApplyAbpConceptsForAddedEntity(entity); //TODO: async? @@ -82,8 +85,8 @@ namespace Volo.Abp.Domain.Repositories.MongoDB } public override async Task UpdateAsync( - TEntity entity, - bool autoSave = false, + TEntity entity, + bool autoSave = false, CancellationToken cancellationToken = default) { ApplyAbpConceptsForUpdatedEntity(entity); @@ -107,8 +110,8 @@ namespace Volo.Abp.Domain.Repositories.MongoDB } public override async Task DeleteAsync( - TEntity entity, - bool autoSave = false, + TEntity entity, + bool autoSave = false, CancellationToken cancellationToken = default) { ApplyAbpConceptsForDeletedEntity(entity); @@ -132,8 +135,8 @@ namespace Volo.Abp.Domain.Repositories.MongoDB } public override async Task DeleteAsync( - Expression> predicate, - bool autoSave = false, + Expression> predicate, + bool autoSave = false, CancellationToken cancellationToken = default) { var entities = await GetMongoQueryable() @@ -170,6 +173,7 @@ namespace Volo.Abp.Domain.Repositories.MongoDB protected virtual void ApplyAbpConceptsForAddedEntity(TEntity entity) { + CheckAndSetId(entity); EntityChangeEventHelper.TriggerEntityCreatedEventOnUowCompleted(entity); EntityChangeEventHelper.TriggerEntityCreatingEvent(entity); TriggerDomainEvents(entity); @@ -189,6 +193,14 @@ namespace Volo.Abp.Domain.Repositories.MongoDB TriggerDomainEvents(entity); } + protected virtual void CheckAndSetId(TEntity entity) + { + if (entity is IEntity entityWithGuidId && entityWithGuidId.Id == default) + { + entityWithGuidId.Id = GuidGenerator.Create(); + } + } + protected virtual void TriggerDomainEvents(object entity) //TODO: TriggerDomainEventsAsync..? { var generatesDomainEventsEntity = entity as IGeneratesDomainEvents; @@ -212,8 +224,8 @@ namespace Volo.Abp.Domain.Repositories.MongoDB } } - public class MongoDbRepository - : MongoDbRepository, + public class MongoDbRepository + : MongoDbRepository, IMongoDbRepository where TMongoDbContext : IAbpMongoDbContext where TEntity : class, IEntity @@ -237,8 +249,8 @@ namespace Volo.Abp.Domain.Repositories.MongoDB } public virtual async Task GetAsync( - TKey id, - bool includeDetails = true, + TKey id, + bool includeDetails = true, CancellationToken cancellationToken = default) { var entity = await FindAsync(id, includeDetails, cancellationToken); @@ -252,8 +264,8 @@ namespace Volo.Abp.Domain.Repositories.MongoDB } public virtual async Task FindAsync( - TKey id, - bool includeDetails = true, + TKey id, + bool includeDetails = true, CancellationToken cancellationToken = default) { return await Collection @@ -272,8 +284,8 @@ namespace Volo.Abp.Domain.Repositories.MongoDB } public virtual Task DeleteAsync( - TKey id, - bool autoSave = false, + TKey id, + bool autoSave = false, CancellationToken cancellationToken = default) { return Collection.DeleteOneAsync( @@ -306,13 +318,13 @@ namespace Volo.Abp.Domain.Repositories.MongoDB { if (typeof(ISoftDelete).IsAssignableFrom(typeof(TEntity)) && DataFilter.IsEnabled()) { - filters.Add(Builders.Filter.Eq(e => ((ISoftDelete) e).IsDeleted, false)); + filters.Add(Builders.Filter.Eq(e => ((ISoftDelete)e).IsDeleted, false)); } if (typeof(IMultiTenant).IsAssignableFrom(typeof(TEntity))) { var tenantId = CurrentTenant.Id; - filters.Add(Builders.Filter.Eq(e => ((IMultiTenant) e).TenantId, tenantId)); + filters.Add(Builders.Filter.Eq(e => ((IMultiTenant)e).TenantId, tenantId)); } } }