From a4594137514c0f25406ca667405874631181d374 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Halil=20=C4=B0brahim=20Kalkan?= Date: Mon, 26 Dec 2016 10:09:19 +0300 Subject: [PATCH] #54 Remove InsertAndGetId from repository. --- .../EntityFrameworkCore/EfCoreRepository.cs | 29 ++++++++++------- .../Abp/Domain/Repositories/IRepository.cs | 31 ++++++------------- .../Abp/Domain/Repositories/RepositoryBase.cs | 16 ++-------- 3 files changed, 30 insertions(+), 46 deletions(-) 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 a5b880b421..5f57032272 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 @@ -63,23 +63,28 @@ namespace Volo.Abp.Domain.Repositories.EntityFrameworkCore return DbSet.FindAsync(new object[] { id }, cancellationToken); } - public override TEntity Insert(TEntity entity) + public override TEntity Insert(TEntity entity, bool autoSave = false) { - return DbSet.Add(entity).Entity; - } + var savedEntity = DbSet.Add(entity).Entity; - public override TPrimaryKey InsertAndGetId(TEntity entity) - { - var insertedEntity = Insert(entity); - DbContext.SaveChanges(); - return insertedEntity.Id; + if (autoSave) + { + DbContext.SaveChanges(); + } + + return savedEntity; } - public override async Task InsertAndGetIdAsync(TEntity entity, CancellationToken cancellationToken = default(CancellationToken)) + public override async Task InsertAsync(TEntity entity, bool autoSave = false, CancellationToken cancellationToken = new CancellationToken()) { - var insertedEntity = await InsertAsync(entity, cancellationToken); - await DbContext.SaveChangesAsync(cancellationToken); - return insertedEntity.Id; + var savedEntity = DbSet.Add(entity).Entity; + + if (autoSave) + { + await DbContext.SaveChangesAsync(cancellationToken); + } + + return savedEntity; } public override TEntity Update(TEntity entity) diff --git a/src/Volo.Abp/Volo/Abp/Domain/Repositories/IRepository.cs b/src/Volo.Abp/Volo/Abp/Domain/Repositories/IRepository.cs index 7945f5138b..8dbfa715b2 100644 --- a/src/Volo.Abp/Volo/Abp/Domain/Repositories/IRepository.cs +++ b/src/Volo.Abp/Volo/Abp/Domain/Repositories/IRepository.cs @@ -59,35 +59,24 @@ namespace Volo.Abp.Domain.Repositories /// Inserts a new entity. /// /// Inserted entity + /// + /// Set true to automatically save changes to database. + /// This can be used to set database generated Id of an entity for some ORMs (like Entity Framework). + /// [NotNull] - TEntity Insert([NotNull] TEntity entity); + TEntity Insert([NotNull] TEntity entity, bool autoSave = false); /// /// Inserts a new entity. /// + /// + /// Set true to automatically save changes to database. + /// This can be used to set database generated Id of an entity for some ORMs (like Entity Framework). + /// /// A to observe while waiting for the task to complete. /// Inserted entity [NotNull] - Task InsertAsync([NotNull] TEntity entity, CancellationToken cancellationToken = default(CancellationToken)); - - /// - /// Inserts a new entity and gets it's Id. - /// It may require to save current unit of work - /// to be able to retrieve id. - /// - /// Entity - /// Id of the entity - TPrimaryKey InsertAndGetId([NotNull] TEntity entity); - - /// - /// Inserts a new entity and gets it's Id. - /// It may require to save current unit of work - /// to be able to retrieve id. - /// - /// A to observe while waiting for the task to complete. - /// Entity - /// Id of the entity - Task InsertAndGetIdAsync([NotNull] TEntity entity, CancellationToken cancellationToken = default(CancellationToken)); + Task InsertAsync([NotNull] TEntity entity, bool autoSave = true, CancellationToken cancellationToken = default(CancellationToken)); /// /// Updates an existing entity. diff --git a/src/Volo.Abp/Volo/Abp/Domain/Repositories/RepositoryBase.cs b/src/Volo.Abp/Volo/Abp/Domain/Repositories/RepositoryBase.cs index 8883edcd11..290d74f60b 100644 --- a/src/Volo.Abp/Volo/Abp/Domain/Repositories/RepositoryBase.cs +++ b/src/Volo.Abp/Volo/Abp/Domain/Repositories/RepositoryBase.cs @@ -39,21 +39,11 @@ namespace Volo.Abp.Domain.Repositories return Task.FromResult(Find(id)); } - public abstract TEntity Insert(TEntity entity); + public abstract TEntity Insert(TEntity entity, bool autoSave = false); - public virtual Task InsertAsync(TEntity entity, CancellationToken cancellationToken = default(CancellationToken)) + public virtual Task InsertAsync(TEntity entity, bool autoSave = false, CancellationToken cancellationToken = default(CancellationToken)) { - return Task.FromResult(Insert(entity)); - } - - public virtual TPrimaryKey InsertAndGetId(TEntity entity) - { - return Insert(entity).Id; - } - - public virtual Task InsertAndGetIdAsync(TEntity entity, CancellationToken cancellationToken = default(CancellationToken)) - { - return Task.FromResult(InsertAndGetId(entity)); + return Task.FromResult(Insert(entity, autoSave)); } public abstract TEntity Update(TEntity entity);