Skip to content

Instantly share code, notes, and snippets.

@felipebaltazar
Created November 28, 2020 20:29
Show Gist options
  • Save felipebaltazar/f1ce42d136bf00f3207af057fc812a9b to your computer and use it in GitHub Desktop.
Save felipebaltazar/f1ce42d136bf00f3207af057fc812a9b to your computer and use it in GitHub Desktop.
using MyProject.Abstractions.Data;
using MyProject.Abstractions.Data.Repositories;
using MyProject.Models;
using LiteDB;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;
namespace MyProject.Data.Repositories
{
public abstract class BaseRepository<TEntity> : IRepository<TEntity> where TEntity : Base
{
#region Fields
private readonly IDbContext _dbContext;
#endregion
#region Constructors
protected BaseRepository(IDbContext context) => _dbContext = context;
#endregion
#region IRepository
public virtual Task InsertAsync(TEntity entityToInsert) =>
Task.FromResult(WithCollection().Insert(entityToInsert));
public virtual Task UpdateAsync(TEntity entityToUpdate, Expression<Func<TEntity, bool>> predicate) =>
Task.FromResult(WithCollection().Update(entityToUpdate));
public virtual Task<TEntity> GetAsync(Expression<Func<TEntity, bool>> predicate) =>
Task.FromResult(WithCollection().Find(predicate).FirstOrDefault());
public virtual Task<IEnumerable<TEntity>> GetManyAsync(Expression<Func<TEntity, bool>> predicate, int skip = 0, int limit = int.MaxValue) =>
Task.FromResult(WithCollection().Find(predicate, skip, limit));
public virtual Task<TEntity> FirstOrDefaultAsync<TField>(Expression<Func<TEntity, TField>> orderBy) =>
Task.FromResult(WithQuery().OrderBy(orderBy).FirstOrDefault());
public virtual Task<TEntity> LastOrDefaultAsync<TField>(Expression<Func<TEntity, TField>> orderBy) =>
Task.FromResult(WithQuery().OrderByDescending(orderBy).FirstOrDefault());
public Task<bool> Exists(TEntity entity) =>
Task.FromResult(ExistsCore(entity));
public virtual Task<bool> DeleteAsync(TEntity entityToDelete) =>
Task.FromResult(WithCollection().Delete(entityToDelete.Id));
public int Count(Expression<Func<TEntity, bool>> predicate) =>
WithCollection().Count(predicate);
#endregion
#region Protected Methods
protected virtual bool ExistsCore(TEntity entity) =>
WithCollection().Find(e => e.Id == entity.Id)?.FirstOrDefault() != null;
protected ILiteCollection<TEntity> WithCollection() =>
_dbContext.GetCollection<TEntity>();
protected ILiteQueryable<TEntity> WithQuery() =>
_dbContext.GetCollection<TEntity>().Query();
#endregion
}
}
using MyProject.Abstractions;
using MyProject.Abstractions.Data;
using MyProject.Models;
using LiteDB;
namespace MyProject.Data
{
public sealed class LiteDbContext : IDbContext
{
private readonly LiteDatabase _dataBase;
public LiteDbContext(IDeviceInfoService deviceSettingsService)
{
BsonMapper.Global.EnumAsInteger = true;
_dataBase = new LiteDatabase(deviceSettingsService.GetLocalDatabaseStream("MyProject.db"));
}
public ILiteCollection<TEntity> GetCollection<TEntity>() where TEntity : Base =>
_dataBase.GetCollection<TEntity>();
}
}
using MyProject.Abstractions.Data;
using MyProject.Abstractions.Data.Repositories;
using MyProject.Models;
namespace MyProject.Data.Repositories
{
public sealed class MyModelRepository : BaseRepository<MyModel>, IMyModelRepository
{
public MyModelRepository(IDbContext dbContext) : base(dbContext)
{
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment