Skip to content

Commit

Permalink
Merge pull request #4634 from abpframework/maliming/setIdInInsert
Browse files Browse the repository at this point in the history
Try to set GUID Id in the InsertAsync method of the EF Core repository.
  • Loading branch information
hikalkan authored Jul 17, 2020
2 parents 7974985 + 4af6d60 commit 95babec
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using Volo.Abp.Domain.Entities;
using Volo.Abp.EntityFrameworkCore;
using Volo.Abp.EntityFrameworkCore.DependencyInjection;
using Volo.Abp.Guids;

namespace Volo.Abp.Domain.Repositories.EntityFrameworkCore
{
Expand All @@ -29,9 +30,12 @@ public class EfCoreRepository<TDbContext, TEntity> : RepositoryBase<TEntity>, IE
private readonly IDbContextProvider<TDbContext> _dbContextProvider;
private readonly Lazy<AbpEntityOptions<TEntity>> _entityOptionsLazy;

protected virtual IGuidGenerator GuidGenerator { get; set; }

public EfCoreRepository(IDbContextProvider<TDbContext> dbContextProvider)
{
_dbContextProvider = dbContextProvider;
GuidGenerator = SimpleGuidGenerator.Instance;

_entityOptionsLazy = new Lazy<AbpEntityOptions<TEntity>>(
() => ServiceProvider
Expand All @@ -40,9 +44,11 @@ public EfCoreRepository(IDbContextProvider<TDbContext> dbContextProvider)
.GetOrNull<TEntity>() ?? AbpEntityOptions<TEntity>.Empty
);
}

public override async Task<TEntity> InsertAsync(TEntity entity, bool autoSave = false, CancellationToken cancellationToken = default)
{
CheckAndSetId(entity);

var savedEntity = DbSet.Add(entity).Entity;

if (autoSave)
Expand All @@ -66,7 +72,7 @@ public override async Task<TEntity> UpdateAsync(TEntity entity, bool autoSave =

return updatedEntity;
}

public override async Task DeleteAsync(TEntity entity, bool autoSave = false, CancellationToken cancellationToken = default)
{
DbSet.Remove(entity);
Expand Down Expand Up @@ -110,7 +116,7 @@ protected override IQueryable<TEntity> GetQueryable()
}

public override async Task<TEntity> FindAsync(
Expression<Func<TEntity, bool>> predicate,
Expression<Func<TEntity, bool>> predicate,
bool includeDetails = true,
CancellationToken cancellationToken = default)
{
Expand Down Expand Up @@ -188,16 +194,38 @@ public override IQueryable<TEntity> WithDetails(params Expression<Func<TEntity,

return query;
}

protected virtual void CheckAndSetId(TEntity entity)
{
if (entity is IEntity<Guid> entityWithGuidId)
{
TrySetGuidId(entityWithGuidId);
}
}

protected virtual void TrySetGuidId(IEntity<Guid> entity)
{
if (entity.Id != default)
{
return;
}

EntityHelper.TrySetId(
entity,
() => GuidGenerator.Create(),
true
);
}
}

public class EfCoreRepository<TDbContext, TEntity, TKey> : EfCoreRepository<TDbContext, TEntity>,
public class EfCoreRepository<TDbContext, TEntity, TKey> : EfCoreRepository<TDbContext, TEntity>,
IEfCoreRepository<TEntity, TKey>,
ISupportsExplicitLoading<TEntity, TKey>

where TDbContext : IEfCoreDbContext
where TEntity : class, IEntity<TKey>
{
public EfCoreRepository(IDbContextProvider<TDbContext> dbContextProvider)
public EfCoreRepository(IDbContextProvider<TDbContext> dbContextProvider)
: base(dbContextProvider)
{

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ public MongoDbRepository(IMongoDbContextProvider<TMongoDbContext> dbContextProvi
LocalEventBus = NullLocalEventBus.Instance;
DistributedEventBus = NullDistributedEventBus.Instance;
EntityChangeEventHelper = NullEntityChangeEventHelper.Instance;
GuidGenerator = SimpleGuidGenerator.Instance;
}

public override async Task<TEntity> InsertAsync(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,5 +64,15 @@ public override async Task InsertAsync()
person.Phones.Count.ShouldBe(1);
person.Phones.Any(p => p.PersonId == person.Id && p.Number == "1234567890").ShouldBeTrue();
}

[Fact]
public async Task Insert_Should_Set_Guid_Id()
{
var person = new Person(Guid.Empty, "New Person", 35);

await PersonRepository.InsertAsync(person);

person.Id.ShouldNotBe(Guid.Empty);
}
}
}

0 comments on commit 95babec

Please sign in to comment.