eShopOnContainers icon indicating copy to clipboard operation
eShopOnContainers copied to clipboard

Why the repository of order has not a method for update?

Open ComptonAlvaro opened this issue 3 years ago • 0 comments

I am seeing this code in which the status of an order is changed.

Then handle in the application layer is this:

public async Task<bool> Handle(ShipOrderCommand command, CancellationToken cancellationToken)
        {
            var orderToUpdate = await _orderRepository.GetAsync(command.OrderNumber);
            if (orderToUpdate == null)
            {
                return false;
            }

            orderToUpdate.SetShippedStatus();
            return await _orderRepository.UnitOfWork.SaveEntitiesAsync(cancellationToken);
        }

And the source class is this:

https://github.com/dotnet-architecture/eShopOnContainers/blob/main/src/Services/Ordering/Ordering.API/Application/Commands/ShipOrderCommandHandler.cs

I have seen that the IOrderRepository doesn't define an update method, like Update(Order updatedOrder). But this is my doubt.

If I am not wrong, this code works because the controller knows that the implementation of the IOrderRepository uses Entity Core, so it is not needed to notify that the order was updated, EF Core does it for us.

But if I am not wrong, the main purpose of an interface it is to hide to the consumer the implementation, so if the consumer doesn't know the implementation, it could use an implementation that doesn't track itself the changes, for example, I could implement a way that when I load the entity to update, I put it in a collection with the actual data, the consumer update the data of the order, but I need to know that the order was changed, for exmple with a Update(Order OrderWithUpdatedData), so in this Update method I would search for the order and I can marked it as changed. if the implmentation use EF Core, then this method would be empty, because the own implementation that uses EF Core knows that EF will do it itself.

So the code to use could be this:

            orderToUpdate.SetShippedStatus();
            _orderRepository.Update(orderToUpdate);
            return await _orderRepository.UnitOfWork.SaveEntitiesAsync(cancellationToken);

So I would like to know if I am right and if not, why not.

Thanks.

ComptonAlvaro avatar Sep 08 '22 11:09 ComptonAlvaro