我正在尝试扩展ASPNETBOILETPLATE中的AuditLog
实体框架,以便为其添加一些新属性。我试图扩展AuditLog
类(ExtendedAuditInfo
)并实现AuditStore
类(ExtendedAuditStore
)的定制版本。但是,我无法在构造函数中注入新的ExtendedAuditInfo
,并收到关于Constructor
和SaveAsync
方法中不匹配输入参数的两条错误消息。
public class ExtendedAuditInfo : AuditInfo
{
// Some properties
}
<<p>类strong> ExtendedAuditStore :public class ExtendedAuditStore : AuditingStore
{
public ExtendedAuditStore(IRepository<ExtendedAuditInfo, long> auditLogRepository)
: base(auditLogRepository)
{
}
public override Task SaveAsync(ExtendedAuditInfo auditInfo)
{
if (!string.IsNullOrEmpty(auditInfo.Parameters) && auditInfo.Parameters != "{}")
{
var parameters = JsonConvert.DeserializeObject<AuditParameterInput>(auditInfo.Parameters);
if (parameters != null)
auditInfo.CustomData = parameters.Input.Id.ToString();
}
return base.SaveAsync(auditInfo);
}
}
错误如下:
无法从' abp . domain . repository . irepository
我根据如何扩展现有实体的官方文档找到了解决方案。
为了扩展
AuditLog
类,必须使用继承。因此,一个新的类,假设ExtendedAuditInfo
需要从AuditLog
继承。public class ExtendedAuditLog : AuditLog { public ExtendedAuditLog() { } public ExtendedAuditLog(AuditInfo auditInfo) { this.BrowserInfo = auditInfo.BrowserInfo; this.ClientIpAddress = auditInfo.ClientIpAddress; this.ClientName = auditInfo.ClientName; this.CustomData = auditInfo.CustomData; this.Exception = auditInfo.Exception?.Message.ToString() + ""; this.ExecutionDuration = auditInfo.ExecutionDuration; this.ExecutionTime = auditInfo.ExecutionTime; this.ImpersonatorTenantId = auditInfo.ImpersonatorTenantId; this.ImpersonatorUserId = auditInfo.ImpersonatorUserId; this.MethodName = auditInfo.MethodName; this.Parameters = auditInfo.Parameters; this.ReturnValue = auditInfo.ReturnValue; this.ServiceName = auditInfo.ServiceName; this.TenantId = auditInfo.TenantId; this.UserId = auditInfo.UserId; } //new properties }
该类必须添加到上下文中,显然,为了添加新属性,需要运行新的迁移。
最后,在ExtendedAuditStore类中,public class ProjectDbContext : AbpZeroDbContext<Tenant, Role, User, ProjectDbContext > { /* Define a DbSet for each entity of the application */ public SerafinaDbContext(DbContextOptions<SerafinaDbContext> options) : base(options) { } public virtual DbSet<County> Counties { get; set; } public virtual DbSet<Country> Countries { get; set; } public virtual DbSet<Currency> Currencies { get; set; } public virtual DbSet<OrganisationType> OrganisationTypes { get; set; } public virtual DbSet<ExtendedAuditLog> ExtendedAuditLogs { get; set; } }
IRepository<ExtendedAuditLog, long> _extendedAuditLogRepository
必须作为构造函数的第二个参数注入,并可用于插入扩展实体。public class ExtendedAuditStore : AuditingStore { IRepository<ExtendedAuditLog, long> _extendedAuditLogRepository; public ExtendedAuditStore( IRepository<AuditLog, long> auditLogRepository, IRepository<ExtendedAuditLog, long> extendedAuditLogRepository ) : base(auditLogRepository) { _extendedAuditLogRepository = extendedAuditLogRepository; } public override async Task SaveAsync(AuditInfo auditInfo) { if (auditInfo.Exception != null) await base.SaveAsync(auditInfo); var auditLog = new ExtendedAuditLog(auditInfo); //new properties can be set here await _extendedAuditLogRepository.InsertAsync(auditLog); } }
此外,可以创建IAuditingStore的新实现并将其注入到应用程序服务中,而不是继承AuditingStore。
更新:
最后,您所需要的就是替换
StartUp
类中的默认AuditingStore
:public IServiceProvider ConfigureServices(IServiceCollection services) { services.AddTransient<IAuditingStore, ExtendedAuditStore>(); }