vendredi 1 novembre 2019

Repository pattern for heterogeneous stores?

I had a repository class used for Entity framework core. However, now some part of the data need to be saved in a text file instead.

public class Repository<T> : IRepository<T> where T : class
{
    private readonly DbSet<T> _entities;

    public Repository(DbContext dbContext)
    {
        if (dbContext is null)
        {
            throw new ArgumentNullException(nameof(dbContext));
        }
        _entities = dbContext.Set<T>();
    }

    public void Add(T entity) => _entities.Add(entity);

    public void AddRange(IEnumerable<T> entities) => _entities.AddRange(entities);

    public IEnumerable<T> Find(Expression<Func<T, bool>> predicate) => _entities.Where(predicate);

    public IEnumerable<T> GetAll() => _entities.ToList();

    public void Remove(T entity) => _entities.Remove(entity);

    public void RemoveRange(IEnumerable<T> entities) => _entities.RemoveRange(entities);
}

Should a new class for flat file be created? Or adding methods for flat file to the above class? And I may need to save some data to different database servers too.

Aucun commentaire:

Enregistrer un commentaire