Created
August 29, 2021 04:11
-
-
Save felipebaltazar/15268e3084f254b77ccdfd4ce8fdfe1e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using MetricPlanner.Abstractions.Data; | |
using MetricPlanner.Abstractions.Data.Repositories; | |
using MetricPlanner.Models; | |
using LiteDB; | |
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Linq.Expressions; | |
using System.Threading.Tasks; | |
using MetricPlanner.Infrastructure.Helpers; | |
namespace MetricPlanner.Data.Repositories | |
{ | |
public abstract class BaseRepository<TEntity> : IRepository<TEntity> where TEntity : Base | |
{ | |
#region Fields | |
private static readonly object _dbContextLock = new object(); | |
private readonly IDbContext _dbContext; | |
#endregion | |
#region Constructors | |
protected BaseRepository(IDbContext context) | |
{ | |
_dbContext = context; | |
MapElements(BsonMapper.Global); | |
} | |
#endregion | |
#region IRepository | |
public virtual async ValueTask InsertAsync(TEntity entityToInsert) | |
{ | |
await Task.Yield(); | |
using (var locker = new SafeLocker(_dbContextLock)) | |
{ | |
using (var disposer = WithCollection()) | |
{ | |
disposer.Element.Insert(entityToInsert); | |
} | |
} | |
} | |
public virtual async ValueTask UpdateAsync(TEntity entityToUpdate, Expression<Func<TEntity, bool>> predicate) | |
{ | |
await Task.Yield(); | |
using (var locker = new SafeLocker(_dbContextLock)) | |
{ | |
using (var disposer = WithCollection()) | |
{ | |
disposer.Element.Update(entityToUpdate); | |
} | |
} | |
} | |
public virtual async ValueTask<TEntity> GetAsync(Expression<Func<TEntity, bool>> predicate) | |
{ | |
await Task.Yield(); | |
using (var locker = new SafeLocker(_dbContextLock)) | |
{ | |
using (var disposer = WithCollection()) | |
{ | |
return disposer.Element.Find(predicate).FirstOrDefault(); | |
} | |
} | |
} | |
public virtual async ValueTask<IEnumerable<TEntity>> GetManyAsync(Expression<Func<TEntity, bool>> predicate, int skip = 0, int limit = int.MaxValue) | |
{ | |
await Task.Yield(); | |
using (var locker = new SafeLocker(_dbContextLock)) | |
{ | |
using (var disposer = WithCollection()) | |
{ | |
return disposer.Element.Find(predicate, skip, limit).ToList(); | |
} | |
} | |
} | |
public virtual async ValueTask<TEntity> FirstOrDefaultAsync<TField>(Expression<Func<TEntity, TField>> orderBy) | |
{ | |
await Task.Yield(); | |
using (var locker = new SafeLocker(_dbContextLock)) | |
{ | |
using (var disposer = WithQuery()) | |
{ | |
var query = disposer.Element; | |
return query.OrderBy(orderBy).FirstOrDefault(); | |
} | |
} | |
} | |
public virtual async ValueTask<TEntity> LastOrDefaultAsync<TField>(Expression<Func<TEntity, TField>> orderBy) | |
{ | |
await Task.Yield(); | |
using (var locker = new SafeLocker(_dbContextLock)) | |
{ | |
using (var disposer = WithQuery()) | |
{ | |
var query = disposer.Element; | |
return query.OrderByDescending(orderBy).FirstOrDefault(); | |
} | |
} | |
} | |
public async ValueTask<bool> Exists(TEntity entity) | |
{ | |
await Task.Yield(); | |
return ExistsCore(entity); | |
} | |
public virtual async ValueTask<bool> DeleteAsync(TEntity entityToDelete) | |
{ | |
await Task.Yield(); | |
using (var locker = new SafeLocker(_dbContextLock)) | |
{ | |
using (var disposer = WithCollection()) | |
{ | |
return disposer.Element.Delete(entityToDelete.Id); | |
} | |
} | |
} | |
public int Count(Expression<Func<TEntity, bool>> predicate) | |
{ | |
using (var locker = new SafeLocker(_dbContextLock)) | |
{ | |
using (var disposer = WithCollection()) | |
{ | |
return disposer.Element.Count(predicate); | |
} | |
} | |
} | |
#endregion | |
#region Protected Methods | |
protected virtual void MapElements(BsonMapper mapper) { } | |
protected virtual bool ExistsCore(TEntity entity) | |
{ | |
using (var locker = new SafeLocker(_dbContextLock)) | |
{ | |
using (var disposer = WithCollection()) | |
{ | |
return disposer.Element.Find(e => e.Id == entity.Id)?.FirstOrDefault() != null; | |
} | |
} | |
} | |
protected Disposer<ILiteCollection<TEntity>> WithCollection() => | |
_dbContext.GetCollection<TEntity>(); | |
protected Disposer<ILiteQueryable<TEntity>> WithQuery() | |
{ | |
var disposer = _dbContext.GetCollection<TEntity>(); | |
return new Disposer<ILiteQueryable<TEntity>>(disposer.Element.Query(), disposer); | |
} | |
#endregion | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment