objects and persistence - Envivo-Software/Envivo.Fresnel GitHub Wiki
By default, all objects are transient – they only exist in memory. To allow instances to be saved to a persistent data store, you'll need the two following items:
- Add the
System.ComponentModel.DataAnnotations.KeyAttributeto theIdproperty:
...
using System.ComponentModel.DataAnnotations;
namespace Acme.OnlineShopping.Stock
{
/// <summary>
/// A Product that is sold on the web site
/// </summary>
public class Product
{
/// <summary>
/// <inheritdoc/>
/// </summary>
[Key] //👈
public Guid Id { get; set; }
...
}- Add a Repository for the entity/aggregate:
/// <summary>
/// Repository for managing Products in a data store
/// </summary>
public class ProductRepository : IRepository<Product> //👈
{
// Replace this with your persistent store:
private static readonly InMemoryRepository<Product> _InMemoryRepository = new();
public IQueryable<Product> GetQuery()
{
return _InMemoryRepository.GetQuery();
}
public Task<Product> LoadAsync(Guid id)
{
return _InMemoryRepository.LoadAsync(id);
}
public Task<int> SaveAsync(Product aggregateRoot, IEnumerable<object> newObjects, IEnumerable<object> modifiedObjects, IEnumerable<object> deletedObjects)
{
return _InMemoryRepository.SaveAsync(aggregateRoot, newObjects, modifiedObjects, deletedObjects);
}
public Task DeleteAsync(Product aggregateRoot)
{
return _InMemoryRepository.DeleteAsync(aggregateRoot);
}
}These steps enable the Save option within the UI:

There's not much point in saving objects if they can't be retrieved again. All persistable objects therefore have a Search option in the main navigation menu.

This opens a simple search dialog where you can find and explore previously saved objects.
Use the InMemoryRepository while your model evolves. That way your model can change freely without worrying about schema changes and data migrations.
If you need to persist to the filesystem, consider using a simple document database like LiteDB.