Design patterns Repository Pattern Read-only repositories (C#)


Example

A repository pattern can be used to encapsulate data storage specific code in designated components. The part of your application, that needs the data will only work with the repositories. You will want to create a repository for each combination of item you store and your database technology.

Read only repositories can be used to create repositories that are not allowed to manipulate data.

The interfaces

public interface IReadOnlyRepository<TEntity, TKey> : IRepository
{
    IEnumerable<TEntity> Get(Expression<Func<TEntity, bool>> filter);

    TEntity Get(TKey id);
}

public interface IRepository<TEntity, TKey> : IReadOnlyRepository<TEntity, TKey>
{
    TKey Add(TEntity entity);

    bool Delete(TKey id);

    TEntity Update(TKey id, TEntity entity);
}

An example implementation using ElasticSearch as technology (with NEST)

public abstract class ElasticReadRepository<TModel> : IReadOnlyRepository<TModel, string>
    where TModel : class
{

    protected ElasticClient Client;

    public ElasticReadRepository()
    {
        Client = Connect();
    }

    protected abstract ElasticClient Connect();

    public TModel Get(string id)
    {
        return Client.Get<TModel>(id).Source;
    }

    public IEnumerable<TModel> Get(Expression<Func<TModel, bool>> filter)
    {
        /* To much code for this example */
        throw new NotImplementedException();
    }
}

public abstract class ElasticRepository<TModel>
    : ElasticReadRepository<TModel>, IRepository<TModel, string>
    where TModel : class
{
    public string Add(TModel entity)
    {
        return Client.Index(entity).Id;
    }

    public bool Delete(string id)
    {
        return Client.Delete<TModel>(id).Found;
    }

    public TModel Update(string id, TModel entity)
    {
        return Connector.Client.Update<TModel>(
            id,
            update => update.Doc(entity)
        ).Get.Source;
    }
}

Using this implementation, you can now create specific Repositories for the items you want to store or access. When using elastic search, it is common, that some components should only read the data, thus read-only repositories should be used.