Skip to content

repositories

vijay@envivo edited this page Oct 20, 2026 · 17 revisions

Repositories

Repositories provide a convenient facade over your persistent data store. Instead of scattering database access code throughout your model, use Repositories to encapsulate that logic.

To create a repository, implement the IRepository<T> interface. The following shows a simple example that keeps the objects in memory:

/// <summary>
/// Repository for managing Products in a data store
/// </summary>
public class ProductRepository : IRepository<Product> //👈
{
    private readonly InMemoryRepository<Product> _InMemoryRepository;

    public ProductRepository(DemoProductsBuilder demoProductsBuilder)
    {
        _InMemoryRepository = new InMemoryRepository<Product>(demoProductsBuilder.Build());
    }

	/// <summary>
    /// <inheritdoc/>
    /// </summary>
    /// <returns></returns>
	public Task<QueryResult<Product>> GetResultsAsync(QueryFilter<Product> filter, CancellationToken cancellationToken)
	{
        return _InMemoryRepository.GetResultsAsync(filter, cancellationToken);
    }
	
    /// <summary>
    /// <inheritdoc/>
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>
    public Task<Product> LoadAsync(Guid id)
    {
        return _InMemoryRepository.LoadAsync(id);
    }

    /// <summary>
    /// <inheritdoc/>
    /// </summary>
    /// <param name="aggregateRoot"></param>
    /// <param name="newObjects"></param>
    /// <param name="modifiedObjects"></param>
    /// <param name="deletedObjects"></param>
    /// <returns></returns>
    public Task<int> SaveAsync(Product aggregateRoot, IEnumerable<object> newObjects, IEnumerable<object> modifiedObjects, IEnumerable<object> deletedObjects)
    {
        return _InMemoryRepository.SaveAsync(aggregateRoot, newObjects, modifiedObjects, deletedObjects);
    }

    /// <summary>
    /// <inheritdoc/>
    /// </summary>
    /// <param name="aggregateRoot"></param>
    /// <returns></returns>
    public Task DeleteAsync(Product aggregateRoot)
    {
        return _InMemoryRepository.DeleteAsync(aggregateRoot);
    }
}

You can inject this repository into any other dependency, and thus guarantee data access via this centralised class.

In the following example, we refactored the ProductQuerySpecification (from the Query Specifications section) to use ProductRepository, instead of using an internal list:

/// <summary>
/// Used to query for Products
/// </summary>
public class ProductQuerySpecification : IQuerySpecification<Product>
{
    private readonly IRepository<Product> _ProductRepo;

    /// <summary>
    /// <inheritdoc/>
    /// </summary>
    /// <param name="productRepo"></param>
    public ProductQuerySpecification(IRepository<Product> productRepo)👈
    {
        _ProductRepo = productRepo;
    }

    /// <summary>
    /// <inheritdoc/>
    /// </summary>
    /// <returns></returns>
    public async Task<IEnumerable<Product>> GetResultsAsync(CancellationToken cancellationToken)
    {
		var queryFilter = new QueryFilter<Product>(where: p => p.IsStillAvailable);
		var queryResult = await _ProductRepo.GetResultsAsync(queryFilter, cancellationToken);
		
        return queryResult.Items;
    }
}

Requirements for persistable objects

  • Must have a constructor with zero arguments
  • All properties must have setters
  • If a property MUST be read-only you should:
    • Mark that property as [IgnoreDataMember]
    • Use another hidden internal property/field to provide that value

Paging with QueryFilter

GetResultsAsync always returns one page of results. A QueryFilter created without paging arguments returns only the first page, which holds 20 items. Code that needs every matching item must ask for each page in turn.

The constructor takes these optional arguments, all of which can be passed by name:

Argument Meaning
pageNo The page number, starting at 1.
pageSize The number of items per page.
where An optional predicate that filters the items.
orderBys An optional array of (key, ascending) tuples used for sorting.
materialiserFunc An optional delegate that fully materialises the query results.

The returned QueryResult<T> reports Items, PageNumber, PageSize, TotalItems (every matching item, across all pages) and TotalPages.

To read every matching item, loop until the last page:

var all = new List<Product>();
for (var pageNo = 1; ; pageNo++)
{
    var filter = new QueryFilter<Product>(pageNo: pageNo, pageSize: 1000, where: p => p.IsProductInStock);
    var result = await _ProductRepo.GetResultsAsync(filter, cancellationToken); //👈
    all.AddRange(result.Items);

    if (pageNo >= result.TotalPages)
    {
        break;
    }
}

💡 Developer Tips

If your domain model requires lots of custom database queries, use separate Query Specifications instead of adding methods to your Repositories.

Clone this wiki locally