lundi 21 février 2022

Uploading files when creating new entities

I'm wondering if there is a common solution/pattern for the following situation:

Let's say I have a webshop application. There you can create orders, which consists of items. Each order item can have one or more files as attachments. (May not make sense, but it's just an example). Pseudocode:

Order
{
    Guid Id
    List<OrderItem> Items
}

OrderItem
{
    Guid Id
    List<Attachment> Attachments
}

Attachment
{
    Guid Id
    string Filename
    byte[] Content
}

The technology is ASP.Net MVC + React. When creating an order, I cannot send the files in the same request due to request length limits. So I have to first send a request to create an order, and then send the files one by one. How do I get the correct OrderItem id (in frontend) for each attachment?

When creating an order, none of the items have ids. CreateOrder method returns the created order and items with ids, but how can I map items in the result to items in the request that don't have ids? Result items may even be sorted differently.

One option could be to add a separate id field to OrderItem dto, e.g. NewItemId. In that case, that id would be generated by the frontend and returned by the backend in the result. However, due to how the application is structured, that would require including the property in a domain class. Is there any more elegant solution?

Aucun commentaire:

Enregistrer un commentaire