handler which can catch all entity modified messages

This commit is contained in:
2026-02-17 15:57:47 +01:00
parent 74023d2de6
commit 41b66dd2eb
5 changed files with 34 additions and 1 deletions
@@ -34,6 +34,7 @@ public partial class DbOperations : IDbOperations
}
Logger.LogInformation("{id} ({type}) successfully deleted", typeof(T), model.Id);
await EntityModifiedHandler.Deleted(model);
return Result<T>.Success();
}
@@ -93,6 +93,8 @@ public partial class DbOperations : IDbOperations
Logger.LogInformation(action + " {id}", model.Id);
}
await EntityModifiedHandler.Saved(model, update);
return Result<T>.Success(model);
}
@@ -114,5 +116,9 @@ public partial class DbOperations : IDbOperations
}
}
await Db.UpdateAllAsync(items);
foreach (T item in items)
{
await EntityModifiedHandler.Updated(item);
}
}
}
+5 -1
View File
@@ -8,6 +8,7 @@ using System.Security.Claims;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using ServiceStack.OrmLite;
using zero.Communication;
using zero.Context;
using zero.Models;
using zero.Utils;
@@ -27,14 +28,17 @@ public partial class DbOperations : IDbOperations
protected ILogger<IDbOperations> Logger { get; private set; }
protected IEntityModifiedHandler EntityModifiedHandler { get; private set; }
public DbOperations(StoreContext context, IDbConnection db, ILogger<IDbOperations> logger)
public DbOperations(StoreContext context, IDbConnection db, ILogger<IDbOperations> logger, IHandlerHolder handler)
{
Context = context.Context;
Services = context.Services;
Flavors = context.Options.For<FlavorOptions>();
Db = db;
Logger = logger;
EntityModifiedHandler = handler.Get<IEntityModifiedHandler>();
}
@@ -0,0 +1,21 @@
using System.Threading.Tasks;
using zero.Communication;
using zero.Models;
namespace zero.Sqlite;
public interface IEntityModifiedHandler : IHandler
{
Task Saved<T>(T model, bool update) where T : ZeroIdEntity, new() => update ? Updated(model) : Created(model);
Task Created<T>(T model) where T : ZeroIdEntity, new();
Task Updated<T>(T model) where T : ZeroIdEntity, new();
Task Deleted<T>(T model) where T : ZeroIdEntity, new();
}
public class EmptyEntityModifiedHandler : IEntityModifiedHandler
{
public Task Created<T>(T model) where T : ZeroIdEntity, new() => Task.CompletedTask;
public Task Updated<T>(T model) where T : ZeroIdEntity, new() => Task.CompletedTask;
public Task Deleted<T>(T model) where T : ZeroIdEntity, new() => Task.CompletedTask;
}
+1
View File
@@ -27,6 +27,7 @@ internal class ZeroSqliteModule : ZeroModule
services.AddScoped<IDbConnection>(CreateDbConnection);
services.AddScoped<IDbOperations, DbOperations>();
services.AddScoped<StoreContext>();
services.AddScoped<IEntityModifiedHandler, EmptyEntityModifiedHandler>();
services.AddOptions<FlavorOptions>();
services.AddOptions<SqliteOptions>().Bind(configuration.GetSection("Zero:Sqlite"));
services.ConfigureOptions<ConfigureFlavorJsonOptions>();