Skip to content

in memory repositories

vijay@envivo edited this page May 26, 2026 · 3 revisions

In-Memory Repositories for Prototyping

When you're sketching out a prototype, using real databases for persistence can slow you down.

To keep things moving fast, create a repository facade over an InMemoryRepository<T>. This example shows a ProductRepository that delegates all operations to an inner InMemoryRepository<Product>:

using Envivo.Fresnel.ModelTypes.Persistence;

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

	/// <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 also pre-load your repository by passing in a list of objects. This examples uses a DemoProductsBuilder to generate an initial list of Product objects:

public class ProductRepository : IRepository<Product>
{
    private readonly InMemoryRepository<Product> _InMemoryRepository;  //👈

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

	// other methods here
}

Clone this wiki locally