资源项总计数计算(单独查询与集合)

im struggling with finding the best way to get total items of items when returning a resource collection im thinking of two approaches just want your opinion regarding whats best ?

scenario :

a user want to retrieve list of 10 vehicles from my 10000 vehicles with some cursors such as (limit, before, after, filters, etc ...)

option 1:

class VehiclesRepo()
{
    function getVehicles(){
        // get 10000 vehicles with one query; : returns collection
        // return 10000 vehicles and do filtration on transformation layer to keep total_count of vehicles 
    }
}

option 2:

class VehiclesRepo()
{
    function getVehicles(){
        // get 10 vehicles with one query including filtration; : returns collection
        // return 10 vehicles, and do another query for total_count
    }
}

please consider operation effect on memory, cpu circles ? also my system is fully ddd so im trying not to have a domain leaks

Both options are basically ok, but I think with 10'000 vehicles you have already a performance problem if you use option 1. So I'd go with option 2.

But there is probably another problem with your design:

I'm trying to do that without having to access infrastructure layer from outside the domain.

This comment suggests that you have the following layering:

 Application
      ⇣
   Domain
      ⇣
Infrastructure

If you don't use some kind of the Dependency Inversion Priciple that actually makes the infrastructure dependent on the domain layer, then you have a problem with this design. The domain should be as pure as possible. If the domain has a dependency on the infrastructure, this means that you cannot use your domain model independently of the infrastructure. This is bad, because the infrastructure is an artificial thing that does not exist in the real world of your domain.

So what you should do conceptually is this:

Application
   ⇣     ⇣
   ⇣    Infrastructure
   ⇣    ⇣
   Domain 

Then your repository implementations become natural and allow query operations like e.g.:

  • Give me Vehicle with ID X
  • Give me all Vehicles with more than Y wheels
  • Give me all Vehicles that match the filter Z

Note that these query operations should return actual business objects, not DTOs or database rows or alike.

If you want to read more about pros and cons of different architectures with DDD, I suggest you read the chapter "Architecture" in Implementing DDD by Vaughn Vernon.


Note on @plaxl's answer: The answer is specific to CQRS, which you don't mention in your question with a single word. So outside of a CQRS context, using the domain model for query operations is perfectly fine.

im trying to do that without having to access infrastructure layer from outside the domain

That's actually your problem. DDD aggregates are designed around command processing, transactional consistency and business invariants, not querying needs.

That's why CQRS is so popular these days. I would strongly advise you not to rely on your domain model for query needs.