From dfaad47bb1f6a4293febe9df03e89dc2b7563e4f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Halil=20=C4=B0brahim=20Kalkan?= Date: Mon, 12 Mar 2018 16:49:25 +0300 Subject: [PATCH] Added autoSave to delete, added clock to some base classes. --- .../Volo/Abp/AspNetCore/Mvc/AbpController.cs | 3 +++ .../AspNetCore/Mvc/RazorPages/AbpPageModel.cs | 5 ++++- .../Application/Services/ApplicationService.cs | 3 +++ .../Volo.Abp.Ddd.Domain.csproj | 1 + .../Volo/Abp/Domain/AbpDddDomainModule.cs | 5 +++-- .../Abp/Domain/Repositories/IBasicRepository.cs | 16 ++++++++-------- .../Volo/Abp/Domain/Repositories/IRepository.cs | 14 +++++++++++--- .../Abp/Domain/Repositories/RepositoryBase.cs | 4 ++-- .../Volo/Abp/Domain/Services/DomainService.cs | 3 +++ .../EntityFrameworkCore/EfCoreRepository.cs | 17 ++++++++++++++++- .../Repositories/MongoDB/MongoDbRepository.cs | 4 ++-- 11 files changed, 56 insertions(+), 19 deletions(-) diff --git a/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/AbpController.cs b/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/AbpController.cs index 2f78ec1c2d..704b880afd 100644 --- a/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/AbpController.cs +++ b/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/AbpController.cs @@ -6,6 +6,7 @@ using Volo.Abp.Guids; using Volo.Abp.MultiTenancy; using Volo.Abp.ObjectMapping; using Volo.Abp.Session; +using Volo.Abp.Timing; using Volo.Abp.Uow; namespace Volo.Abp.AspNetCore.Mvc @@ -26,6 +27,8 @@ namespace Volo.Abp.AspNetCore.Mvc protected IUnitOfWork CurrentUnitOfWork => UnitOfWorkManager?.Current; + public IClock Clock { get; set; } + protected ILogger Logger => _lazyLogger.Value; private Lazy _lazyLogger => new Lazy(() => LoggerFactory?.CreateLogger(GetType().FullName) ?? NullLogger.Instance, true); } diff --git a/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/RazorPages/AbpPageModel.cs b/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/RazorPages/AbpPageModel.cs index c22a354b71..9af2327e3f 100644 --- a/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/RazorPages/AbpPageModel.cs +++ b/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/RazorPages/AbpPageModel.cs @@ -11,6 +11,7 @@ using Volo.Abp.Guids; using Volo.Abp.MultiTenancy; using Volo.Abp.ObjectMapping; using Volo.Abp.Session; +using Volo.Abp.Timing; using Volo.Abp.Uow; namespace Volo.Abp.AspNetCore.Mvc.RazorPages @@ -34,7 +35,9 @@ namespace Volo.Abp.AspNetCore.Mvc.RazorPages public IAuthorizationService AuthorizationService { get; set; } protected IUnitOfWork CurrentUnitOfWork => UnitOfWorkManager?.Current; - + + public IClock Clock { get; set; } + protected ILogger Logger => _lazyLogger.Value; private Lazy _lazyLogger => new Lazy(() => LoggerFactory?.CreateLogger(GetType().FullName) ?? NullLogger.Instance, true); diff --git a/src/Volo.Abp.Ddd.Application/Volo/Abp/Application/Services/ApplicationService.cs b/src/Volo.Abp.Ddd.Application/Volo/Abp/Application/Services/ApplicationService.cs index 692a11c2d7..95c128bda0 100644 --- a/src/Volo.Abp.Ddd.Application/Volo/Abp/Application/Services/ApplicationService.cs +++ b/src/Volo.Abp.Ddd.Application/Volo/Abp/Application/Services/ApplicationService.cs @@ -8,6 +8,7 @@ using Volo.Abp.Guids; using Volo.Abp.MultiTenancy; using Volo.Abp.ObjectMapping; using Volo.Abp.Session; +using Volo.Abp.Timing; using Volo.Abp.Uow; namespace Volo.Abp.Application.Services @@ -33,6 +34,8 @@ namespace Volo.Abp.Application.Services public ICurrentUser CurrentUser { get; set; } + public IClock Clock { get; set; } + public IAuthorizationService AuthorizationService { get; set; } protected IUnitOfWork CurrentUnitOfWork => UnitOfWorkManager?.Current; diff --git a/src/Volo.Abp.Ddd.Domain/Volo.Abp.Ddd.Domain.csproj b/src/Volo.Abp.Ddd.Domain/Volo.Abp.Ddd.Domain.csproj index 9f2959b973..582e812143 100644 --- a/src/Volo.Abp.Ddd.Domain/Volo.Abp.Ddd.Domain.csproj +++ b/src/Volo.Abp.Ddd.Domain/Volo.Abp.Ddd.Domain.csproj @@ -20,6 +20,7 @@ + diff --git a/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/AbpDddDomainModule.cs b/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/AbpDddDomainModule.cs index a142ce627e..f840fbc126 100644 --- a/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/AbpDddDomainModule.cs +++ b/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/AbpDddDomainModule.cs @@ -5,6 +5,7 @@ using Volo.Abp.Guids; using Volo.Abp.Modularity; using Volo.Abp.MultiTenancy; using Volo.Abp.Threading; +using Volo.Abp.Timing; using Volo.Abp.Uow; namespace Volo.Abp.Domain @@ -15,8 +16,8 @@ namespace Volo.Abp.Domain typeof(AbpGuidsModule), typeof(AbpMultiTenancyAbstractionsModule), typeof(AbpThreadingModule), - typeof(AbpUnitOfWorkModule) - )] + typeof(AbpTimingModule), + typeof(AbpUnitOfWorkModule))] public class AbpDddDomainModule : AbpModule { public override void ConfigureServices(IServiceCollection services) diff --git a/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/Repositories/IBasicRepository.cs b/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/Repositories/IBasicRepository.cs index daaa19176c..4f6ca13269 100644 --- a/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/Repositories/IBasicRepository.cs +++ b/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/Repositories/IBasicRepository.cs @@ -14,7 +14,7 @@ namespace Volo.Abp.Domain.Repositories /// Inserted entity /// /// Set true to automatically save entity to database. - /// This is useful for ORMs / database APIs those only saves changes with an explicit method call. + /// This is useful for ORMs / database APIs those only save changes with an explicit method call, but you need to immediately save changes to the database. /// [NotNull] TEntity Insert([NotNull] TEntity entity, bool autoSave = false); @@ -24,7 +24,7 @@ namespace Volo.Abp.Domain.Repositories /// /// /// Set true to automatically save changes to database. - /// This is useful for ORMs / database APIs those only saves changes with an explicit method call. + /// This is useful for ORMs / database APIs those only save changes with an explicit method call, but you need to immediately save changes to the database. /// /// A to observe while waiting for the task to complete. /// Inserted entity @@ -37,7 +37,7 @@ namespace Volo.Abp.Domain.Repositories /// Entity /// /// Set true to automatically save changes to database. - /// This is useful for ORMs / database APIs those only saves changes with an explicit method call. + /// This is useful for ORMs / database APIs those only save changes with an explicit method call, but you need to immediately save changes to the database. /// [NotNull] TEntity Update([NotNull] TEntity entity, bool autoSave = false); @@ -47,7 +47,7 @@ namespace Volo.Abp.Domain.Repositories /// /// /// Set true to automatically save changes to database. - /// This is useful for ORMs / database APIs those only saves changes with an explicit method call. + /// This is useful for ORMs / database APIs those only save changes with an explicit method call, but you need to immediately save changes to the database. /// /// A to observe while waiting for the task to complete. /// Entity @@ -60,7 +60,7 @@ namespace Volo.Abp.Domain.Repositories /// Entity to be deleted /// /// Set true to automatically save changes to database. - /// This is useful for ORMs / database APIs those only saves changes with an explicit method call. + /// This is useful for ORMs / database APIs those only save changes with an explicit method call, but you need to immediately save changes to the database. /// void Delete([NotNull] TEntity entity, bool autoSave = false); //TODO: Return true if deleted @@ -70,7 +70,7 @@ namespace Volo.Abp.Domain.Repositories /// Entity to be deleted /// /// Set true to automatically save changes to database. - /// This is useful for ORMs / database APIs those only saves changes with an explicit method call. + /// This is useful for ORMs / database APIs those only save changes with an explicit method call, but you need to immediately save changes to the database. /// /// A to observe while waiting for the task to complete. Task DeleteAsync([NotNull] TEntity entity, bool autoSave = false, CancellationToken cancellationToken = default); //TODO: Return true if deleted @@ -120,7 +120,7 @@ namespace Volo.Abp.Domain.Repositories /// Primary key of the entity /// /// Set true to automatically save changes to database. - /// This is useful for ORMs / database APIs those only saves changes with an explicit method call. + /// This is useful for ORMs / database APIs those only save changes with an explicit method call, but you need to immediately save changes to the database. /// void Delete(TKey id, bool autoSave = false); //TODO: Return true if deleted @@ -130,7 +130,7 @@ namespace Volo.Abp.Domain.Repositories /// Primary key of the entity /// /// Set true to automatically save changes to database. - /// This is useful for ORMs / database APIs those only saves changes with an explicit method call. + /// This is useful for ORMs / database APIs those only save changes with an explicit method call, but you need to immediately save changes to the database. /// /// A to observe while waiting for the task to complete. Task DeleteAsync(TKey id, bool autoSave = false, CancellationToken cancellationToken = default); //TODO: Return true if deleted diff --git a/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/Repositories/IRepository.cs b/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/Repositories/IRepository.cs index 72151436c4..b994db40cf 100644 --- a/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/Repositories/IRepository.cs +++ b/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/Repositories/IRepository.cs @@ -28,7 +28,11 @@ namespace Volo.Abp.Domain.Repositories /// given predicate. /// /// A condition to filter entities - void Delete([NotNull] Expression> predicate); + /// + /// Set true to automatically save changes to database. + /// This is useful for ORMs / database APIs those only save changes with an explicit method call, but you need to immediately save changes to the database. + /// + void Delete([NotNull] Expression> predicate, bool autoSave = false); /// /// Deletes many entities by function. @@ -36,9 +40,13 @@ namespace Volo.Abp.Domain.Repositories /// This may cause major performance problems if there are too many entities with /// given predicate. /// - /// A to observe while waiting for the task to complete. /// A condition to filter entities - Task DeleteAsync([NotNull] Expression> predicate, CancellationToken cancellationToken = default); + /// + /// Set true to automatically save changes to database. + /// This is useful for ORMs / database APIs those only save changes with an explicit method call, but you need to immediately save changes to the database. + /// + /// A to observe while waiting for the task to complete. + Task DeleteAsync([NotNull] Expression> predicate, bool autoSave = false, CancellationToken cancellationToken = default); } public interface IRepository : IRepository, IBasicRepository diff --git a/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/Repositories/RepositoryBase.cs b/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/Repositories/RepositoryBase.cs index a3c8d52989..7495f2df88 100644 --- a/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/Repositories/RepositoryBase.cs +++ b/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/Repositories/RepositoryBase.cs @@ -36,7 +36,7 @@ namespace Volo.Abp.Domain.Repositories protected abstract IQueryable GetQueryable(); - public virtual void Delete(Expression> predicate) + public virtual void Delete(Expression> predicate, bool autoSave = false) { foreach (var entity in GetQueryable().Where(predicate).ToList()) { @@ -44,7 +44,7 @@ namespace Volo.Abp.Domain.Repositories } } - public virtual Task DeleteAsync(Expression> predicate, CancellationToken cancellationToken = default) + public virtual Task DeleteAsync(Expression> predicate, bool autoSave = false, CancellationToken cancellationToken = default) { Delete(predicate); return Task.CompletedTask; diff --git a/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/Services/DomainService.cs b/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/Services/DomainService.cs index 60a77b0840..a74445e4f5 100644 --- a/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/Services/DomainService.cs +++ b/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/Services/DomainService.cs @@ -2,11 +2,14 @@ using System; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging.Abstractions; using Volo.Abp.Guids; +using Volo.Abp.Timing; namespace Volo.Abp.Domain.Services { public abstract class DomainService : IDomainService { + public IClock Clock { get; set; } + public IGuidGenerator GuidGenerator { get; set; } public ILoggerFactory LoggerFactory { get; set; } 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 d24124687f..d45355e90c 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 @@ -104,13 +104,28 @@ namespace Volo.Abp.Domain.Repositories.EntityFrameworkCore return DbSet.AsQueryable(); } - public override async Task DeleteAsync(Expression> predicate, CancellationToken cancellationToken = default) + public override void Delete(Expression> predicate, bool autoSave = false) + { + base.Delete(predicate, autoSave); + + if (autoSave) + { + DbContext.SaveChanges(); + } + } + + public override async Task DeleteAsync(Expression> predicate, bool autoSave = false, CancellationToken cancellationToken = default) { var entities = await GetQueryable().Where(predicate).ToListAsync(GetCancellationToken(cancellationToken)); foreach (var entity in entities) { DbSet.Remove(entity); } + + if (autoSave) + { + await DbContext.SaveChangesAsync(cancellationToken); + } } public virtual async Task EnsureCollectionLoadedAsync( 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 79f1f19c5f..2a76f7f177 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 @@ -60,12 +60,12 @@ namespace Volo.Abp.Domain.Repositories.MongoDB await Collection.DeleteOneAsync(CreateEntityFilter(entity), cancellationToken); } - public override void Delete(Expression> predicate) + public override void Delete(Expression> predicate, bool autoSave = false) { Collection.DeleteMany(Builders.Filter.Where(predicate)); } - public override async Task DeleteAsync(Expression> predicate, CancellationToken cancellationToken = default) + public override async Task DeleteAsync(Expression> predicate, bool autoSave = false, CancellationToken cancellationToken = default) { await Collection.DeleteManyAsync(Builders.Filter.Where(predicate), cancellationToken); }