unit of work - Envivo-Software/Envivo.Fresnel GitHub Wiki
The default IRepository<T>.Save() method is fine for rapid prototyping, but has limitations when fine-grained persistence is needed.
The IUnitOfWorkAdapter allows for greater granularity, allowing you to identify changed objects and any relationships that were created (or broken) when an object is saved. This is particularly useful when you need to control exactly how object links are saved in your persistence store.
The steps to enable this are:
-
Enable the Unit of Work feature in the bootstrap code:
builder.AddFresnel(opt => { opt .WithFeature(Feature.Persistence_UnitOfWork, FeatureState.On) //👈 ... ; });
-
Implement
IUnitOfWorkAdapteron your Repository implementation:/// <summary> /// Repository for managing Products in a data store /// </summary> public class ProductRepository : IRepository<Product>, IUnitOfWorkAdapter //👈 { ... public Task<IEnumerable<object>> SaveAsync(IUnitOfWork unitOfWork) //👈 { // Your implementation here } }
When the user triggers the "Save" action from the UI, Fresnel will invoke the SaveAsync(unitOfWork) method.
| Property | Description | Type | Default |
|---|---|---|---|
| RootObject | The primary aggregate being saved | object | |
| NewObjects | Any new objects within the aggregate | IReadOnlyCollection<object> | |
| ModifiedObjects | Any modified objects within the aggregate | IReadOnlyCollection<object> | |
| DeletedObjects | Any objects deleted from the aggregate | IReadOnlyCollection<object> | |
| NewRelationships | Any new relationships created between objects | IReadOnlyCollection<Relationship> | |
| OldRelationships | Any relationships that were broken between objects | IReadOnlyCollection<Relationship> |
| Property | Description | Type | Default |
|---|---|---|---|
| FromObject | The source of the relationship | object | |
| ViaProperty | The property that relates the source object to the target object | PropertyInfo | |
| ToObject | The target of the relationship | object |