【发布时间】:2018-09-04 07:36:05
【问题描述】:
我在我的项目中使用ASP.NET Boilerplate。我有一个实体,如下面的代码 sn-p 所示。
public class Transaction : FullAuditedEntity<Guid>, IMustHaveTenant
{
protected Transaction()
{
TransactionState = TransactionState.Uncompleted;
}
public TransactionState TransactionState { get; protected set; }
public virtual Loan Loan { get; protected set; }
public int TenantId { get; set; }
// ...
public async Task CompleteAsync(ICoreBankingService coreBankingService, IRepository<Transaction, Guid> transactionRepository)
{
try
{
// Perform a series of compulsory actions in some given sequence with coreBankingService that might throw exception
Loan.SetSomeStuffThatOriginatedFromMotherTransaction();
TransactionState = TransactionState.Completed;
}
catch (Exception ex)
{
// Log the exception and set this Transaction entity state appropriately
TransactionState = TransactionState.Failed;
}
finally
{
// Make sure by all means to persist the resulting the entity within itself
await transactionRepository.UpdateAsync(this);
}
}
}
我知道我应该将持久性与实体分开(顺便说一下,这是由ASP.NET Boilerplate 提供的开箱即用的架构!使用Application Services)。
但是,我需要确保我使用coreBankingService按照给定顺序执行一系列强制操作,并在这些计算的每个阶段持续更改Transaction 实体,因此是我幼稚且可能是错误方法的原因。
请问,解决此类问题的正确方法是什么?如何持久化由同一实体内的计算或操作产生的实体状态?
【问题讨论】:
标签: c# entity-framework asp.net-core domain-driven-design aspnetboilerplate