gpt4 book ai didi

c# - 如何使用业务规则来持久化实体?

转载 作者:行者123 更新时间:2023-11-30 22:59:28 29 4
gpt4 key购买 nike

我正在使用 ASP.NET Boilerplate为我的项目。我有一个实体,如下面的代码片段所示。

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 实体上的这些计算阶段,因此我采用幼稚且可能是错误的方法的原因。

请问,解决此类问题的正确方法是什么?我如何保留由同一实体内的计算或操作产生的实体状态?

最佳答案

您可以公开改变状态的内部方法:

public class Transaction : FullAuditedEntity<Guid>, IMustHaveTenant
{
protected Transaction()
{
TransactionState = TransactionState.Uncompleted;
}

public TransactionState TransactionState { get; protected set; }
public virtual Loan Loan { get; protected set; }
// ...

internal void Abort()
{
TransactionState = TransactionState.Failed;
}

internal void Complete()
{
TransactionState = TransactionState.Completed;
}
}

并在同一个程序集中定义域服务:

public class TransactionManager : DomainService
{
private readonly ICoreBankingService _coreBankingService;
private readonly LoanManager _loanManager;
private readonly IRepository<Transaction, Guid> _transactionRepository;

public TransactionManager(
ICoreBankingService coreBankingService,
LoanManager loanManager,
IRepository<Transaction, Guid> transactionRepository)
{
_coreBankingService = coreBankingService;
_loanManager = loanManager;
_transactionRepository = transactionRepository;
}

public async Task TryCompleteAsync(Transaction transaction)
{
try
{
// Use _coreBankingService to do something that might throw exception
_coreBankingService.DoSomething();
_loanManager.SetSomeStuffThatOriginatedFromMotherTransaction(transaction.Loan);
transaction.Complete();
}
catch (Exception ex)
{
// Log the exception and abort the Transaction
transaction.Abort();
}
finally
{
// Make sure by all means to persist the resulting the entity
await _transactionRepository.UpdateAsync(transaction);
}
}
}

用法:

await _transactionManager.TryCompleteAsync(transaction);

关于c# - 如何使用业务规则来持久化实体?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52161129/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com