in memory repositories - Envivo-Software/Envivo.Fresnel GitHub Wiki

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>:

/// <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 IQueryable<Product> GetQuery()
    {
        return _InMemoryRepository.GetQuery();
    }

    /// <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());  //👈
    }

    ...
}
⚠️ **GitHub.com Fallback** ⚠️