Browse Source

Use GetCancellationToken() method in the mongodb repository. Also reformatted code.

pull/272/head
Halil İbrahim Kalkan 9 years ago
parent
commit
3edf2f878f
  1. 9
      src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/AbpDbContext.cs
  2. 104
      src/Volo.Abp.MongoDB/Volo/Abp/Domain/Repositories/MongoDB/MongoDbRepository.cs

9
src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/AbpDbContext.cs

@ -39,7 +39,12 @@ namespace Volo.Abp.EntityFrameworkCore
public IAuditPropertySetter AuditPropertySetter { get; set; } public IAuditPropertySetter AuditPropertySetter { get; set; }
private static readonly MethodInfo ConfigureGlobalFiltersMethodInfo = typeof(AbpDbContext<TDbContext>).GetMethod(nameof(ConfigureGlobalFilters), BindingFlags.Instance | BindingFlags.NonPublic); private static readonly MethodInfo ConfigureGlobalFiltersMethodInfo
= typeof(AbpDbContext<TDbContext>)
.GetMethod(
nameof(ConfigureGlobalFilters),
BindingFlags.Instance | BindingFlags.NonPublic
);
protected AbpDbContext(DbContextOptions<TDbContext> options) protected AbpDbContext(DbContextOptions<TDbContext> options)
: base(options) : base(options)
@ -121,8 +126,6 @@ namespace Volo.Abp.EntityFrameworkCore
protected virtual EntityChangeReport ApplyAbpConcepts() protected virtual EntityChangeReport ApplyAbpConcepts()
{ {
/* Implement other concepts from ABP v1.x */
var changeReport = new EntityChangeReport(); var changeReport = new EntityChangeReport();
foreach (var entry in ChangeTracker.Entries().ToList()) foreach (var entry in ChangeTracker.Entries().ToList())

104
src/Volo.Abp.MongoDB/Volo/Abp/Domain/Repositories/MongoDB/MongoDbRepository.cs

@ -12,9 +12,9 @@ using Volo.Abp.MultiTenancy;
namespace Volo.Abp.Domain.Repositories.MongoDB namespace Volo.Abp.Domain.Repositories.MongoDB
{ {
//TODO: Use GetCancellationToken() method! public class MongoDbRepository<TMongoDbContext, TEntity>
: RepositoryBase<TEntity>,
public class MongoDbRepository<TMongoDbContext, TEntity> : RepositoryBase<TEntity>, IMongoDbRepository<TEntity> IMongoDbRepository<TEntity>
where TMongoDbContext : IAbpMongoDbContext where TMongoDbContext : IAbpMongoDbContext
where TEntity : class, IEntity where TEntity : class, IEntity
{ {
@ -37,61 +37,105 @@ namespace Volo.Abp.Domain.Repositories.MongoDB
return entity; return entity;
} }
public override async Task<TEntity> InsertAsync(TEntity entity, bool autoSave = false, CancellationToken cancellationToken = default) public override async Task<TEntity> InsertAsync(
TEntity entity,
bool autoSave = false,
CancellationToken cancellationToken = default)
{ {
await Collection.InsertOneAsync(entity, cancellationToken: cancellationToken); await Collection.InsertOneAsync(
entity,
cancellationToken: GetCancellationToken(cancellationToken)
);
return entity; return entity;
} }
public override TEntity Update(TEntity entity, bool autoSave = false) public override TEntity Update(TEntity entity, bool autoSave = false)
{ {
Collection.ReplaceOne(CreateEntityFilter(entity), entity); Collection.ReplaceOne(
CreateEntityFilter(entity),
entity
);
return entity; return entity;
} }
public override async Task<TEntity> UpdateAsync(TEntity entity, bool autoSave = false, 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); await Collection.ReplaceOneAsync(
CreateEntityFilter(entity),
entity,
cancellationToken: GetCancellationToken(cancellationToken)
);
return entity; return entity;
} }
public override void Delete(TEntity entity, bool autoSave = false) public override void Delete(TEntity entity, bool autoSave = false)
{ {
Collection.DeleteOne(CreateEntityFilter(entity)); Collection.DeleteOne(
CreateEntityFilter(entity)
);
} }
public override async Task DeleteAsync(TEntity entity, bool autoSave = false, CancellationToken cancellationToken = default) public override async Task DeleteAsync(
TEntity entity,
bool autoSave = false,
CancellationToken cancellationToken = default)
{ {
await Collection.DeleteOneAsync(CreateEntityFilter(entity), cancellationToken); await Collection.DeleteOneAsync(
CreateEntityFilter(entity),
GetCancellationToken(cancellationToken)
);
} }
public override void Delete(Expression<Func<TEntity, bool>> predicate, bool autoSave = false) public override void Delete(Expression<Func<TEntity, bool>> predicate, bool autoSave = false)
{ {
Collection.DeleteMany(Builders<TEntity>.Filter.Where(predicate)); Collection.DeleteMany(
Builders<TEntity>.Filter.Where(predicate)
);
} }
public override async Task DeleteAsync(Expression<Func<TEntity, bool>> predicate, bool autoSave = false, CancellationToken cancellationToken = default) public override async Task DeleteAsync(
Expression<Func<TEntity, bool>> predicate,
bool autoSave = false,
CancellationToken cancellationToken = default)
{ {
await Collection.DeleteManyAsync(Builders<TEntity>.Filter.Where(predicate), cancellationToken); await Collection.DeleteManyAsync(
Builders<TEntity>.Filter.Where(predicate),
GetCancellationToken(cancellationToken)
);
} }
protected override IQueryable<TEntity> GetQueryable() protected override IQueryable<TEntity> GetQueryable()
{ {
return ApplyDataFilters(Collection.AsQueryable()); return ApplyDataFilters(
Collection.AsQueryable()
);
} }
protected virtual IMongoQueryable<TEntity> GetMongoQueryable() protected virtual IMongoQueryable<TEntity> GetMongoQueryable()
{ {
return ApplyDataFilters(Collection.AsQueryable()); return ApplyDataFilters(
Collection.AsQueryable()
);
} }
protected virtual FilterDefinition<TEntity> CreateEntityFilter(TEntity entity) protected virtual FilterDefinition<TEntity> CreateEntityFilter(TEntity entity)
{ {
throw new NotImplementedException("CreateEntityFilter is not implemented for MongoDb by default. It should be overrided and implemented by deriving classes!"); throw new NotImplementedException(
$"{nameof(CreateEntityFilter)} is not implemented for MongoDB by default. " +
$"It should be overrided and implemented by the deriving class!"
);
} }
} }
public class MongoDbRepository<TMongoDbContext, TEntity, TKey> : MongoDbRepository<TMongoDbContext, TEntity>, IMongoDbRepository<TEntity, TKey> public class MongoDbRepository<TMongoDbContext, TEntity, TKey>
: MongoDbRepository<TMongoDbContext, TEntity>,
IMongoDbRepository<TEntity, TKey>
where TMongoDbContext : IAbpMongoDbContext where TMongoDbContext : IAbpMongoDbContext
where TEntity : class, IEntity<TKey> where TEntity : class, IEntity<TKey>
{ {
@ -113,7 +157,10 @@ namespace Volo.Abp.Domain.Repositories.MongoDB
return entity; return entity;
} }
public virtual async Task<TEntity> GetAsync(TKey id, bool includeDetails = true, CancellationToken cancellationToken = default) public virtual async Task<TEntity> GetAsync(
TKey id,
bool includeDetails = true,
CancellationToken cancellationToken = default)
{ {
var entity = await FindAsync(id, includeDetails, cancellationToken); var entity = await FindAsync(id, includeDetails, cancellationToken);
@ -125,9 +172,14 @@ namespace Volo.Abp.Domain.Repositories.MongoDB
return entity; return entity;
} }
public virtual async Task<TEntity> FindAsync(TKey id, bool includeDetails = true, CancellationToken cancellationToken = default) public virtual async Task<TEntity> FindAsync(
TKey id,
bool includeDetails = true,
CancellationToken cancellationToken = default)
{ {
return await Collection.Find(CreateEntityFilter(id, true)).FirstOrDefaultAsync(cancellationToken); return await Collection
.Find(CreateEntityFilter(id, true))
.FirstOrDefaultAsync(GetCancellationToken(cancellationToken));
} }
public virtual TEntity Find(TKey id, bool includeDetails = true) public virtual TEntity Find(TKey id, bool includeDetails = true)
@ -140,9 +192,15 @@ namespace Volo.Abp.Domain.Repositories.MongoDB
Collection.DeleteOne(CreateEntityFilter(id)); Collection.DeleteOne(CreateEntityFilter(id));
} }
public virtual Task DeleteAsync(TKey id, bool autoSave = false, CancellationToken cancellationToken = default) public virtual Task DeleteAsync(
TKey id,
bool autoSave = false,
CancellationToken cancellationToken = default)
{ {
return Collection.DeleteOneAsync(CreateEntityFilter(id), cancellationToken); return Collection.DeleteOneAsync(
CreateEntityFilter(id),
GetCancellationToken(cancellationToken)
);
} }
protected override FilterDefinition<TEntity> CreateEntityFilter(TEntity entity) protected override FilterDefinition<TEntity> CreateEntityFilter(TEntity entity)

Loading…
Cancel
Save