Search code examples
c#asp.net-corecqrsmediatr

How to handle an array of arrays in MediatR


I have a command

public class UpdateEntityCommand: IRequest<bool>
{
    public int Id { get; set; }
    public IEnumerable<OtherEntityDto> Instruments{ get; set; }
}

I need to update many entities in one request. Can I do something like this or there are a better way?

public class UpdateEntitiesCommand: IRequest<bool>
{
    public IEnumerable<UpdateEntityCommand> Commands { get; set; }
}

Solution

  • Supposing your OtherEntityDto is something like this:

    class OtherEntityDto 
    {
       public Guid Id {get;set;}
       public string Name {get;set;}
    }
    

    How I would do it is to have a command such as:

    public class UpdateInstrumentsCommand: IRequest<bool>
    {
       public IEnumerable<OtherEntityDto> UpdatedInstrumemnts {get;set}
    }
    

    and then, in the command handler I will match the persisted entities with what's in the UpdatedInstrumemnts based on the Id property and update their name accordingly. I hope this makes sense to you