是否可以扩展 IEntityHistoryStore 以存储每个 table 而不是在同一个 table 中跟踪所有内容?

Is it possible to extend IEntityHistoryStore to store per-table rather than track all in the same table?

在asp.net样板中,我们需要跟踪对实体的更改。我看到 ABP 有实体跟踪(新),但它将跨实体的所有更改存储在一个 table 中,这对我们不起作用。是否可以创建自定义 IEntityHistoryStore 来存储每个实体的这些更改(在它们自己的 table 中)?

例如,假设有一个实体 Task

[Table("Task", Schema = "Tasks")]
public class Task : Entity<int>
{
    [Column(TypeName = "varchar(255)")]
    public string Description { get; set; }
    // some more poco properties
}

然后我想定义的是另一个 table, zzLog.Tasks_Task 我希望 table 看起来像这样:

╔════════╦════════════════════════╦══════╦══════════╦════════════════════╗
║ TaskId ║      Description       ║ lgId ║ lgAction ║       lgTime       ║
╠════════╬════════════════════════╬══════╬══════════╬════════════════════╣
║      1 ║ Some description       ║    1 ║        1 ║ 2019-05-30 9:05 AM ║
║      1 ║ Some other description ║    2 ║        2 ║ 2019-05-30 9:06 AM ║
║      1 ║ Changed again          ║    3 ║        2 ║ 2019-05-30 9:07 AM ║
║      1 ║ Changed again          ║    4 ║        3 ║ 2019-05-30 9:08 AM ║
╚════════╩════════════════════════╩══════╩══════════╩════════════════════╝

lgAction 是枚举,1 = 创建,2 = 更新,3 = 删除

这可能吗?本能说不,因为 IEntityHistoryStore 可能无法以这种方式工作。

是的,来自https://aspnetboilerplate.com/Pages/Documents/Entity-History#about-ientityhistorystore

The Entity History tracking system uses IEntityHistoryStore to save change information. ... you can implement it in your own way ...

实施MyEntityHistoryStore:

public class MyEntityHistoryStore : IEntityHistoryStore
{
    private readonly IRepository<zzLog.Tasks_Task> _taskLogRepository;

    public EntityHistoryStore(IRepository<zzLog.Tasks_Task> taskLogRepository)
    {
        _taskLogRepository = taskLogRepository;
    }

    public virtual async Task SaveAsync(EntityChangeSet changeSet)
    {
        foreach (var entityChange in changeSet.EntityChanges)
        {
            var taskLog = new zzLog.Tasks_Task()
            {
                TaskId = entityChange.EntityId,
                Description = entityChange.PropertyChanges
                    .Where(pc => pc.PropertyName == "Description")
                    .FirstOrDefault()?
                    .NewValue,
                // lgId = 0,  // Auto-increment
                lgAction =
                    entityChange.ChangeType == EntityChangeType.Created ? 1 :
                    entityChange.ChangeType == EntityChangeType.Updated ? 2 :
                    entityChange.ChangeType == EntityChangeType.Deleted ? 3 : 0,
                lgTime = entityChange.ChangeTime
            };

            await _taskLogRepository.InsertAsync(taskLog);
        }
    }
}

在模块的 PreInitialize 方法中替换 IEntityHistoryStore 的服务:

// using Abp.Configuration.Startup;

Configuration.ReplaceService<IEntityHistoryStore, MyEntityHistoryStore>(DependencyLifeStyle.Transient);