Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Michele Benolli 31 posts 149 karma points
    Aug 19, 2021 @ 06:59
    Michele Benolli
    0

    Order custom property not set

    I'm trying to add a custom property to an existing order in a RenderMvcController, with the following code:

    using (var uow = _uowProvider.Create())
    {
         var writableOrder = model.Order.AsWritable(uow);
         var time = DateTime.Now.AddMinutes(15);
         writableOrder.SetProperty("time", time.ToString());
         _orderService.SaveOrder(writableOrder);
         uow.Complete();
    }
    

    The custom property time is not added to the Properties collection and in the database. What am I doing wrong?

  • Matt Brailsford 4125 posts 22223 karma points MVP 9x c-trib
    Aug 19, 2021 @ 08:18
    Matt Brailsford
    0

    Hi Michele,

    What you have seems correct. Are there any other write operations happening at the same time? it's possible a) multiple updates are being written and so your change get's overwritten or b) another operation is taking place in the same Unit of Work that is not calling uow.Complete() and so the whole transaction scope is reverting as all open Units of Work must complete successfully for everything to be persisted.

    Matt

  • Michele Benolli 31 posts 149 karma points
    Aug 19, 2021 @ 09:16
    Michele Benolli
    0

    Not at the same time but immediately before. I add a payment method to the order if not set.

    // Set ZeroValue if a payment method is not set and if the event is free
    if (paymentMethod == null && model.Order.TotalPrice?.Value?.WithTax == 0)
    {
        using (var uow = _uowProvider.Create())
        {
            var paymentMethod = _paymentMethodService.GetPaymentMethod(order.StoreId, "zeroValue");
            var writableOrder = model.Order.AsWritable(uow);
            writableOrder.SetPaymentMethod(paymentMethod);
            _orderService.SaveOrder(writableOrder);
            uow.Complete();
        }
        model.Order = model.GetCurrentOrder(); // problem here?
    }
    // Here the code to add the custom property
    

    There are no other operations happening inside the UoW and the custom property is added after the code above.

  • Matt Brailsford 4125 posts 22223 karma points MVP 9x c-trib
    Aug 19, 2021 @ 09:25
    Matt Brailsford
    100

    Ok, I'm not sure if this is the issue or not, but it's definitely something you need to be careful of is calling AsWritable on the same read only order.

    Order objects are a snapshot in time. When you call AsWritable you create a writable clone of the read only entity. If you have a read only order entity, then convert it to writable and then persist those changes, but then convert the original readonly order to writable again, that is a new writable clone, with the order in the state of the original read only order, which is without the changes from your previous persist.

    Generally speaking, it's always wise to fetch the latest order before any conversion writable to make sure you are working with the most up to date entity. Also, it's also recommended that you perform all write operations in a single UoW to ensure they all happen, or none do, as what you don't want to happen is for some updates to be successful, but not others, potentially leaving your order in a middle / invalid state.

    Its very possibly that you have some code that is overwriting your changes in a previous write operation because you are using a stale order snapshot to create your writable instance.

    Matt

  • Michele Benolli 31 posts 149 karma points
    Aug 19, 2021 @ 09:52
    Michele Benolli
    1

    Thank you for the detailed explanation. I put both the write operations inside the same UoW using block, and now the order is correctly updated and the custom property is added.

Please Sign in or register to post replies

Write your reply to:

Draft