Код: Выделить всё
public async Task CreateA(CreateARequest req)
{
BDto createdB;
try
{
// Create parent B object that a depends on
createdB = await bService.CreateB(req.CreateBReq);
}
catch (Exception)
{
/* Handle exception */
...
}
/*
By this point BService created the B object through BRepository,
which calls SaveChanges() on each CRUD operation
*/
ADto createdA;
try
{
A entityObj = CreateEntityFromReq(req);
createdA = await repo.Create(A);
}
catch (Exception)
{
/*
Here I would have to delete the created B object as part of
exception handling. I'd like to turn the whole thing into a
single transaction to not have to do this, but services can't
orchestrate transactions cleanly since they don't have access
to the DbContext.
*/
...
}
...
}
< /code>
До сих пор я думал о двух идеях, чтобы справиться с этим: < /p>
[list]
[*] Создать класс TransactionManager (dbContext db) < /code>, которые службы могут получать в качестве зависимости. Этот класс будет поручено только инициировать, совершать и откатить транзакции DB. В конце метода Createa
[/list]
Но я не уверен, какая из этих идей лучше или если кто -либо из них вообще вообще. Я открыт для предложений о том, как лучше справиться с этим.
Подробнее здесь: https://stackoverflow.com/questions/797 ... core-ef-co