#230 Add autosave option to update and delete methods of the repository.

pull/272/head
Halil İbrahim Kalkan 8 years ago
parent f646ecb32e
commit 70a1bfe0c4

@ -1,6 +1,6 @@
using System;
namespace Volo.Abp.Uow
namespace Volo.Abp.Data
{
public class AbpDbConcurrencyException : AbpException
{

@ -22,16 +22,16 @@ namespace Volo.Abp.Domain.Repositories
return Task.FromResult(Insert(entity, autoSave));
}
public abstract TEntity Update(TEntity entity);
public abstract TEntity Update(TEntity entity, bool autoSave = false);
public virtual Task<TEntity> UpdateAsync(TEntity entity, CancellationToken cancellationToken = default)
public virtual Task<TEntity> UpdateAsync(TEntity entity, bool autoSave = false, CancellationToken cancellationToken = default)
{
return Task.FromResult(Update(entity));
}
public abstract void Delete(TEntity entity);
public abstract void Delete(TEntity entity, bool autoSave = false);
public virtual Task DeleteAsync(TEntity entity, CancellationToken cancellationToken = default)
public virtual Task DeleteAsync(TEntity entity, bool autoSave = false, CancellationToken cancellationToken = default)
{
Delete(entity);
return Task.CompletedTask;
@ -70,7 +70,7 @@ namespace Volo.Abp.Domain.Repositories
return Task.FromResult(Find(id));
}
public virtual void Delete(TKey id)
public virtual void Delete(TKey id, bool autoSave = false)
{
var entity = Find(id);
if (entity == null)
@ -81,7 +81,7 @@ namespace Volo.Abp.Domain.Repositories
Delete(entity);
}
public virtual Task DeleteAsync(TKey id, CancellationToken cancellationToken = default)
public virtual Task DeleteAsync(TKey id, bool autoSave = false, CancellationToken cancellationToken = default)
{
Delete(id);
return Task.CompletedTask;

@ -13,8 +13,8 @@ namespace Volo.Abp.Domain.Repositories
/// </summary>
/// <param name="entity">Inserted entity</param>
/// <param name="autoSave">
/// 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).
/// Set true to automatically save entity to database.
/// This is useful for ORMs / database APIs those only saves changes with an explicit method call.
/// </param>
[NotNull]
TEntity Insert([NotNull] TEntity entity, bool autoSave = false);
@ -24,7 +24,7 @@ namespace Volo.Abp.Domain.Repositories
/// </summary>
/// <param name="autoSave">
/// 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).
/// This is useful for ORMs / database APIs those only saves changes with an explicit method call.
/// </param>
/// <param name="cancellationToken">A <see cref="T:System.Threading.CancellationToken" /> to observe while waiting for the task to complete.</param>
/// <param name="entity">Inserted entity</param>
@ -35,29 +35,45 @@ namespace Volo.Abp.Domain.Repositories
/// Updates an existing entity.
/// </summary>
/// <param name="entity">Entity</param>
/// <param name="autoSave">
/// Set true to automatically save changes to database.
/// This is useful for ORMs / database APIs those only saves changes with an explicit method call.
/// </param>
[NotNull]
TEntity Update([NotNull] TEntity entity);
TEntity Update([NotNull] TEntity entity, bool autoSave = false);
/// <summary>
/// Updates an existing entity.
/// </summary>
/// <param name="autoSave">
/// Set true to automatically save changes to database.
/// This is useful for ORMs / database APIs those only saves changes with an explicit method call.
/// </param>
/// <param name="cancellationToken">A <see cref="T:System.Threading.CancellationToken" /> to observe while waiting for the task to complete.</param>
/// <param name="entity">Entity</param>
[NotNull]
Task<TEntity> UpdateAsync([NotNull] TEntity entity, CancellationToken cancellationToken = default);
Task<TEntity> UpdateAsync([NotNull] TEntity entity, bool autoSave = false, CancellationToken cancellationToken = default);
/// <summary>
/// Deletes an entity.
/// </summary>
/// <param name="entity">Entity to be deleted</param>
void Delete([NotNull] TEntity entity); //TODO: Return true if deleted
/// <param name="autoSave">
/// Set true to automatically save changes to database.
/// This is useful for ORMs / database APIs those only saves changes with an explicit method call.
/// </param>
void Delete([NotNull] TEntity entity, bool autoSave = false); //TODO: Return true if deleted
/// <summary>
/// Deletes an entity.
/// </summary>
/// <param name="cancellationToken">A <see cref="T:System.Threading.CancellationToken" /> to observe while waiting for the task to complete.</param>
/// <param name="entity">Entity to be deleted</param>
Task DeleteAsync([NotNull] TEntity entity, CancellationToken cancellationToken = default); //TODO: Return true if deleted
/// <param name="autoSave">
/// Set true to automatically save changes to database.
/// This is useful for ORMs / database APIs those only saves changes with an explicit method call.
/// </param>
/// <param name="cancellationToken">A <see cref="T:System.Threading.CancellationToken" /> to observe while waiting for the task to complete.</param>
Task DeleteAsync([NotNull] TEntity entity, bool autoSave = false, CancellationToken cancellationToken = default); //TODO: Return true if deleted
}
public interface IBasicRepository<TEntity, TKey> : IBasicRepository<TEntity>
@ -102,13 +118,21 @@ namespace Volo.Abp.Domain.Repositories
/// Deletes an entity by primary key.
/// </summary>
/// <param name="id">Primary key of the entity</param>
void Delete(TKey id); //TODO: Return true if deleted
/// <param name="autoSave">
/// Set true to automatically save changes to database.
/// This is useful for ORMs / database APIs those only saves changes with an explicit method call.
/// </param>
void Delete(TKey id, bool autoSave = false); //TODO: Return true if deleted
/// <summary>
/// Deletes an entity by primary key.
/// </summary>
/// <param name="cancellationToken">A <see cref="T:System.Threading.CancellationToken" /> to observe while waiting for the task to complete.</param>
/// <param name="id">Primary key of the entity</param>
Task DeleteAsync(TKey id, CancellationToken cancellationToken = default); //TODO: Return true if deleted
/// <param name="autoSave">
/// Set true to automatically save changes to database.
/// This is useful for ORMs / database APIs those only saves changes with an explicit method call.
/// </param>
/// <param name="cancellationToken">A <see cref="T:System.Threading.CancellationToken" /> to observe while waiting for the task to complete.</param>
Task DeleteAsync(TKey id, bool autoSave = false, CancellationToken cancellationToken = default); //TODO: Return true if deleted
}
}

@ -97,7 +97,7 @@ namespace Volo.Abp.Domain.Repositories
return Task.FromResult(Find(id));
}
public virtual void Delete(TKey id)
public virtual void Delete(TKey id, bool autoSave = false)
{
var entity = Find(id);
if (entity == null)
@ -108,7 +108,7 @@ namespace Volo.Abp.Domain.Repositories
Delete(entity);
}
public virtual Task DeleteAsync(TKey id, CancellationToken cancellationToken = default)
public virtual Task DeleteAsync(TKey id, bool autoSave = false, CancellationToken cancellationToken = default)
{
Delete(id);
return Task.CompletedTask;

@ -51,15 +51,52 @@ namespace Volo.Abp.Domain.Repositories.EntityFrameworkCore
return savedEntity;
}
public override TEntity Update(TEntity entity)
public override TEntity Update(TEntity entity, bool autoSave = false)
{
DbContext.Attach(entity);
return DbContext.Update(entity).Entity;
var updatedEntity = DbContext.Update(entity).Entity;
if (autoSave)
{
DbContext.SaveChanges();
}
return updatedEntity;
}
public override async Task<TEntity> UpdateAsync(TEntity entity, bool autoSave = false, CancellationToken cancellationToken = default)
{
DbContext.Attach(entity);
var updatedEntity = DbContext.Update(entity).Entity;
if (autoSave)
{
await DbContext.SaveChangesAsync(cancellationToken);
}
return updatedEntity;
}
public override void Delete(TEntity entity)
public override void Delete(TEntity entity, bool autoSave = false)
{
DbSet.Remove(entity);
if (autoSave)
{
DbContext.SaveChanges();
}
}
public override async Task DeleteAsync(TEntity entity, bool autoSave = false, CancellationToken cancellationToken = default)
{
DbSet.Remove(entity);
if (autoSave)
{
await DbContext.SaveChangesAsync(cancellationToken);
}
}
protected override IQueryable<TEntity> GetQueryable()
@ -142,7 +179,7 @@ namespace Volo.Abp.Domain.Repositories.EntityFrameworkCore
return await DbSet.FindAsync(new object[] { id }, GetCancellationToken(cancellationToken));
}
public virtual void Delete(TKey id)
public virtual void Delete(TKey id, bool autoSave = false)
{
var entity = Find(id);
if (entity == null)
@ -150,13 +187,18 @@ namespace Volo.Abp.Domain.Repositories.EntityFrameworkCore
return;
}
Delete(entity);
Delete(entity, autoSave);
}
public virtual Task DeleteAsync(TKey id, CancellationToken cancellationToken = default)
public virtual async Task DeleteAsync(TKey id, bool autoSave = false, CancellationToken cancellationToken = default)
{
Delete(id);
return Task.CompletedTask;
var entity = await FindAsync(id, cancellationToken);
if (entity == null)
{
return;
}
await DeleteAsync(entity, autoSave, cancellationToken);
}
}
}

@ -7,10 +7,10 @@ using System.Threading.Tasks;
using JetBrains.Annotations;
using Microsoft.AspNetCore.Identity;
using Microsoft.Extensions.Logging;
using Volo.Abp.Data;
using Volo.Abp.DependencyInjection;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.Guids;
using Volo.Abp.Uow;
namespace Volo.Abp.Identity
{
@ -23,7 +23,6 @@ namespace Volo.Abp.Identity
ITransientDependency
{
private readonly IIdentityRoleRepository _roleRepository;
private readonly IUnitOfWorkManager _unitOfWorkManager;
private readonly ILogger<IdentityRoleStore> _logger;
private readonly IGuidGenerator _guidGenerator;
@ -31,13 +30,11 @@ namespace Volo.Abp.Identity
/// Constructs a new instance of <see cref="IdentityRoleStore"/>.
/// </summary>
public IdentityRoleStore(
IUnitOfWorkManager unitOfWorkManager,
IIdentityRoleRepository roleRepository,
ILogger<IdentityRoleStore> logger,
IGuidGenerator guidGenerator,
IdentityErrorDescriber describer = null)
{
_unitOfWorkManager = unitOfWorkManager;
_roleRepository = roleRepository;
_logger = logger;
_guidGenerator = guidGenerator;
@ -58,19 +55,6 @@ namespace Volo.Abp.Identity
/// </value>
public bool AutoSaveChanges { get; set; } = true;
/// <summary>Saves the current store.</summary>
/// <param name="cancellationToken">The <see cref="CancellationToken"/> used to propagate notifications that the operation should be canceled.</param>
/// <returns>The <see cref="Task"/> that represents the asynchronous operation.</returns>
protected Task SaveChanges(CancellationToken cancellationToken)
{
if (!AutoSaveChanges || _unitOfWorkManager.Current == null)
{
return Task.CompletedTask;
}
return _unitOfWorkManager.Current.SaveChangesAsync(cancellationToken);
}
/// <summary>
/// Creates a new role in a store as an asynchronous operation.
/// </summary>
@ -83,8 +67,7 @@ namespace Volo.Abp.Identity
Check.NotNull(role, nameof(role));
await _roleRepository.InsertAsync(role, cancellationToken: cancellationToken);
await SaveChanges(cancellationToken);
await _roleRepository.InsertAsync(role, AutoSaveChanges, cancellationToken);
return IdentityResult.Success;
}
@ -101,11 +84,9 @@ namespace Volo.Abp.Identity
Check.NotNull(role, nameof(role));
await _roleRepository.UpdateAsync(role, cancellationToken);
try
{
await SaveChanges(cancellationToken);
await _roleRepository.UpdateAsync(role, AutoSaveChanges, cancellationToken);
}
catch (AbpDbConcurrencyException ex)
{
@ -130,8 +111,7 @@ namespace Volo.Abp.Identity
try
{
await _roleRepository.DeleteAsync(role, cancellationToken);
await SaveChanges(cancellationToken);
await _roleRepository.DeleteAsync(role, AutoSaveChanges, cancellationToken);
}
catch (AbpDbConcurrencyException ex)
{

@ -8,10 +8,10 @@ using System.Threading.Tasks;
using JetBrains.Annotations;
using Microsoft.AspNetCore.Identity;
using Microsoft.Extensions.Logging;
using Volo.Abp.Data;
using Volo.Abp.DependencyInjection;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.Guids;
using Volo.Abp.Uow;
namespace Volo.Abp.Identity
{
@ -48,17 +48,14 @@ namespace Volo.Abp.Identity
private readonly IGuidGenerator _guidGenerator;
private readonly ILogger<IdentityRoleStore> _logger;
private readonly IIdentityUserRepository _userRepository;
private readonly IUnitOfWorkManager _unitOfWorkManager;
public IdentityUserStore(
IUnitOfWorkManager unitOfWorkManager,
IIdentityUserRepository userRepository,
IIdentityRoleRepository roleRepository,
IGuidGenerator guidGenerator,
ILogger<IdentityRoleStore> logger,
IdentityErrorDescriber describer = null)
{
_unitOfWorkManager = unitOfWorkManager;
_userRepository = userRepository;
_roleRepository = roleRepository;
_guidGenerator = guidGenerator;
@ -67,19 +64,6 @@ namespace Volo.Abp.Identity
ErrorDescriber = describer ?? new IdentityErrorDescriber();
}
/// <summary>Saves the current store.</summary>
/// <param name="cancellationToken">The <see cref="CancellationToken"/> used to propagate notifications that the operation should be canceled.</param>
/// <returns>The <see cref="Task"/> that represents the asynchronous operation.</returns>
protected Task SaveChanges(CancellationToken cancellationToken)
{
if (!AutoSaveChanges || _unitOfWorkManager.Current == null)
{
return Task.CompletedTask;
}
return _unitOfWorkManager.Current.SaveChangesAsync(cancellationToken);
}
/// <summary>
/// Gets the user identifier for the specified <paramref name="user"/>.
/// </summary>
@ -174,7 +158,6 @@ namespace Volo.Abp.Identity
Check.NotNull(user, nameof(user));
await _userRepository.InsertAsync(user, AutoSaveChanges, cancellationToken);
await SaveChanges(cancellationToken);
return IdentityResult.Success;
}
@ -193,8 +176,7 @@ namespace Volo.Abp.Identity
try
{
await _userRepository.UpdateAsync(user, cancellationToken);
await SaveChanges(cancellationToken);
await _userRepository.UpdateAsync(user, AutoSaveChanges, cancellationToken);
}
catch (AbpDbConcurrencyException ex)
{
@ -219,8 +201,7 @@ namespace Volo.Abp.Identity
try
{
await _userRepository.DeleteAsync(user, cancellationToken);
await SaveChanges(cancellationToken);
await _userRepository.DeleteAsync(user, AutoSaveChanges, cancellationToken);
}
catch (AbpDbConcurrencyException ex)
{

@ -29,12 +29,12 @@ namespace Volo.Abp.Domain.Repositories.MemoryDb
return entity;
}
public override TEntity Update(TEntity entity)
public override TEntity Update(TEntity entity, bool autoSave = false)
{
return entity;
}
public override void Delete(TEntity entity)
public override void Delete(TEntity entity, bool autoSave = false)
{
Collection.Remove(entity);
}
@ -98,7 +98,7 @@ namespace Volo.Abp.Domain.Repositories.MemoryDb
return Task.FromResult(Find(id));
}
public virtual void Delete(TKey id)
public virtual void Delete(TKey id, bool autoSave = false)
{
var entity = Find(id);
if (entity == null)
@ -109,7 +109,7 @@ namespace Volo.Abp.Domain.Repositories.MemoryDb
Delete(entity);
}
public virtual Task DeleteAsync(TKey id, CancellationToken cancellationToken = default)
public virtual Task DeleteAsync(TKey id, bool autoSave = false, CancellationToken cancellationToken = default)
{
Delete(id);
return Task.CompletedTask;

@ -38,24 +38,24 @@ namespace Volo.Abp.Domain.Repositories.MongoDB
return entity;
}
public override TEntity Update(TEntity entity)
public override TEntity Update(TEntity entity, bool autoSave = false)
{
Collection.ReplaceOne(CreateEntityFilter(entity), entity);
return entity;
}
public override async Task<TEntity> UpdateAsync(TEntity entity, CancellationToken cancellationToken = default)
public override async Task<TEntity> UpdateAsync(TEntity entity, bool autoSave = false, CancellationToken cancellationToken = default)
{
await Collection.ReplaceOneAsync(CreateEntityFilter(entity), entity, cancellationToken: cancellationToken);
return entity;
}
public override void Delete(TEntity entity)
public override void Delete(TEntity entity, bool autoSave = false)
{
Collection.DeleteOne(CreateEntityFilter(entity));
}
public override async Task DeleteAsync(TEntity entity, CancellationToken cancellationToken = default)
public override async Task DeleteAsync(TEntity entity, bool autoSave = false, CancellationToken cancellationToken = default)
{
await Collection.DeleteOneAsync(CreateEntityFilter(entity), cancellationToken);
}
@ -115,12 +115,12 @@ namespace Volo.Abp.Domain.Repositories.MongoDB
return entity;
}
public virtual void Delete(TKey id)
public virtual void Delete(TKey id, bool autoSave = false)
{
Collection.DeleteOne(CreateEntityFilter(id));
}
public virtual Task DeleteAsync(TKey id, CancellationToken cancellationToken = default)
public virtual Task DeleteAsync(TKey id, bool autoSave = false, CancellationToken cancellationToken = default)
{
return Collection.DeleteOneAsync(CreateEntityFilter(id), cancellationToken);
}

@ -158,12 +158,12 @@ namespace Volo.Abp.Domain.Repositories
throw new NotImplementedException();
}
public override TEntity Update(TEntity entity)
public override TEntity Update(TEntity entity, bool autoSave = false)
{
throw new NotImplementedException();
}
public override void Delete(TEntity entity)
public override void Delete(TEntity entity, bool autoSave = false)
{
throw new NotImplementedException();
}
@ -192,12 +192,12 @@ namespace Volo.Abp.Domain.Repositories
throw new NotImplementedException();
}
public void Delete(TKey id)
public void Delete(TKey id, bool autoSave = false)
{
throw new NotImplementedException();
}
public Task DeleteAsync(TKey id, CancellationToken cancellationToken = default)
public Task DeleteAsync(TKey id, bool autoSave = false, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}

Loading…
Cancel
Save