UnitofWork [aspnetboilerplate] Transaction Management

aspnetboilerplate entity-framework entity-framework-6 transactions

Question

I am currently working on implementing aspnetboilerplate's transaction management

Below is the method I am using to insert a order and products associated with the order

public class OrderController
{
    IOrderAppService _orderAppService;
    public OrderController(IOrderAppService orderAppService)
    {
        _orderAppService = orderAppService;               
    }
    public void TestOrder()
    {
        _orderAppService.TestTransaction();
    }
}


public class OrderAppService : IOrderAppService
{
    //repositories are injected here

    public void TestTransaction()
    {
        //Created 'order' and 'products' here

        //Committing the created objects
        CommitOrderTransaction();

    }

    private void CommitOrderTransaction()
    {
        using (var unitOfWork = _unitOfWorkManager.Begin())
        { 
            //Inserts the Order record
            CommitInsertOrderHeader(); // Order Header is saved in database by using SaveChanges() method

            //Inserts the Product records associated with OrderId
            CommitInsertOrderDetails(); 

            unitOfWork.Complete();                
        }
    }
}

As the aspnetboilerplate documentation tells that, "if current unit of work is transactional, all changes in the transaction are rolled back if an exception occurs, even saved changes."

In my case when an exception occurs on inserting the OrderDetails, I would like the header record to be rolled back as well but I still have the Order header record in database.

1
0
12/4/2017 1:07:39 PM

Popular Answer

you don't need to handle transaction manually. ABP handles it for you! All application service methods are automatically set as UnitOfWork. It's an atomic operation. So if any exception occurs in the middle of transactions all the db operations are being rolled back.

further information check out https://aspnetboilerplate.com/Pages/Documents/Unit-Of-Work

1
12/4/2017 9:35:15 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow