#54 Remove InsertAndGetId from repository.

pull/81/head
Halil İbrahim Kalkan 9 years ago
parent ea13ec2c76
commit a459413751

@ -63,23 +63,28 @@ namespace Volo.Abp.Domain.Repositories.EntityFrameworkCore
return DbSet.FindAsync(new object[] { id }, cancellationToken); 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) if (autoSave)
{ {
var insertedEntity = Insert(entity);
DbContext.SaveChanges(); DbContext.SaveChanges();
return insertedEntity.Id;
} }
public override async Task<TPrimaryKey> InsertAndGetIdAsync(TEntity entity, CancellationToken cancellationToken = default(CancellationToken)) return savedEntity;
}
public override async Task<TEntity> InsertAsync(TEntity entity, bool autoSave = false, CancellationToken cancellationToken = new CancellationToken())
{
var savedEntity = DbSet.Add(entity).Entity;
if (autoSave)
{ {
var insertedEntity = await InsertAsync(entity, cancellationToken);
await DbContext.SaveChangesAsync(cancellationToken); await DbContext.SaveChangesAsync(cancellationToken);
return insertedEntity.Id; }
return savedEntity;
} }
public override TEntity Update(TEntity entity) public override TEntity Update(TEntity entity)

@ -59,35 +59,24 @@ namespace Volo.Abp.Domain.Repositories
/// Inserts a new entity. /// Inserts a new entity.
/// </summary> /// </summary>
/// <param name="entity">Inserted entity</param> /// <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).
/// </param>
[NotNull] [NotNull]
TEntity Insert([NotNull] TEntity entity); TEntity Insert([NotNull] TEntity entity, bool autoSave = false);
/// <summary> /// <summary>
/// Inserts a new entity. /// Inserts a new entity.
/// </summary> /// </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).
/// </param>
/// <param name="cancellationToken">A <see cref="T:System.Threading.CancellationToken" /> to observe while waiting for the task to complete.</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> /// <param name="entity">Inserted entity</param>
[NotNull] [NotNull]
Task<TEntity> InsertAsync([NotNull] TEntity entity, CancellationToken cancellationToken = default(CancellationToken)); Task<TEntity> InsertAsync([NotNull] TEntity entity, bool autoSave = true, CancellationToken cancellationToken = default(CancellationToken));
/// <summary>
/// Inserts a new entity and gets it's Id.
/// It may require to save current unit of work
/// to be able to retrieve id.
/// </summary>
/// <param name="entity">Entity</param>
/// <returns>Id of the entity</returns>
TPrimaryKey InsertAndGetId([NotNull] TEntity entity);
/// <summary>
/// Inserts a new entity and gets it's Id.
/// It may require to save current unit of work
/// to be able to retrieve id.
/// </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</param>
/// <returns>Id of the entity</returns>
Task<TPrimaryKey> InsertAndGetIdAsync([NotNull] TEntity entity, CancellationToken cancellationToken = default(CancellationToken));
/// <summary> /// <summary>
/// Updates an existing entity. /// Updates an existing entity.

@ -39,21 +39,11 @@ namespace Volo.Abp.Domain.Repositories
return Task.FromResult(Find(id)); return Task.FromResult(Find(id));
} }
public abstract TEntity Insert(TEntity entity); public abstract TEntity Insert(TEntity entity, bool autoSave = false);
public virtual Task<TEntity> InsertAsync(TEntity entity, CancellationToken cancellationToken = default(CancellationToken)) public virtual Task<TEntity> InsertAsync(TEntity entity, bool autoSave = false, CancellationToken cancellationToken = default(CancellationToken))
{ {
return Task.FromResult(Insert(entity)); return Task.FromResult(Insert(entity, autoSave));
}
public virtual TPrimaryKey InsertAndGetId(TEntity entity)
{
return Insert(entity).Id;
}
public virtual Task<TPrimaryKey> InsertAndGetIdAsync(TEntity entity, CancellationToken cancellationToken = default(CancellationToken))
{
return Task.FromResult(InsertAndGetId(entity));
} }
public abstract TEntity Update(TEntity entity); public abstract TEntity Update(TEntity entity);

Loading…
Cancel
Save