标签 EFCore 下的文章

最近在不经意之间写了一个涉及EFCore事务性的bug。起因是某一个业务方法SubmitFlowAsync()中要复用一段流程引擎的接口,而这个业务类中除了这个业务方法之外的其它所有方法都和流程引擎无关,所以我下意识地从之前流程引擎的单元测试中拷贝了一段程序并稍加修改,变成了下面的代码:


// 通过依赖注入得到的字段  
private readonly IServiceScopeFactory _ssf;
  
// ...

// 一个业务方法
public async Task<FSharpResult<IEnumerable<FlowInvocationRecord>, IDomainErr>> SubmitFlowAsync(string definitionCode, string instanceCode)
{
    using var scope = this._ssf.CreateScope();
    var sp = scope.ServiceProvider;
    IFlowDefinitionRepo defRepo = sp.GetRequiredService<IFlowDefinitionRepo>(); // 流程引擎特有接口
    IFlowInstanceRepo instRepo = sp.GetRequiredService<IFlowInstanceRepo>();    // 流程引擎特有接口

    var q = from definition in this.LoadDefinitionAsync(definitionCode)
            from instance in this.CreateFlowInstance(instRepo, definition, Guid.NewGuid().ToString(), instanceCode)
            from records in this._flowInvoker.BeginAsync(sp, instRepo, definition, instance)
                .SelectError(e => {
                    IDomainErr err = new DummyErr("400", e.ToString() ?? this._localizer["流程启动错误"]);
                    return err;
                })
            select records;
    return await q;
}

调用代码类似于:

阅读剩余部分