Combine data from 2 bounded context in a single REST call

Viewed 69

I am working on a Rest API using .Net Core that have many different endpoints that returns the specific data. For example I have an endpoint the returns PagedList of customers (api/customers). Internally, I am using Mediatr to perform this request, so the query for customer currently looks something like this

    public async Task<PaginatedList<CustomerDto>> Handle(GetCustomersQuery query, CancellationToken cancellationToken)
{
    return await _context.Customers
        .AsNoTracking()
        .OrderBy(query.SortBy)
        .ProjectTo<CustomerDto>(_mapper.ConfigurationProvider)
        .PaginatedListAsync(query.PageNumber, query.PageSize, cancellationToken: cancellationToken);
}

This returns the list of Customers that are currently displayed in a datagrid on the UI. Now all of a sudden the requirement came in that in the customer datagrid, I need to show the Number/Count of Orders made by each customer. Now I do have another api for Orders by customer(/api/Customer/{customerId}/Orders). Also in the domain layer, Customer and Order are in different bounded context. Order (AggregateRoot) has CustomerId. But Customer (AggregateRoot) is not referencing Order entity (since it's belong to different bounded context)

My question is

  1. Is it good practice that in the GetCustomersQueryHandler I make a Database call (using EF) to get both Customer and Order (counts) data and combine them into CustomerDto? That means I am returning data from 2 bounded context in one single call.

  2. From REST best practice, is it ok to return a second resource (which in my case is Order) when Customer api is called? Or should it be UI responsiblity that for each Customer, request the Order api to get the order.

0 Answers
Related