如何将实体与业务规则保持一致?

时间:2018-09-04 07:36:05

标签: c# entity-framework asp.net-core domain-driven-design aspnetboilerplate

我正在为项目使用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实体上的计算,因此是我幼稚甚至错误的方法的原因。

请问,解决此类问题的正确方法是什么?如何保持同一实体内的计算或操作所导致的实体状态?

1 个答案:

答案 0 :(得分:2)

您可以公开internal个更改状态的方法:

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);