Question

So I've implemented objectDataSource custom paging in the DAL using stored proceedures like all the guides and tutorials describe, but I'm realizing that by putting your custom paging logic at the data access layer, you are limited on how much you can use your Domain model.

For instance, I have a webform with a gridview that I want to display every Customer (CustomerId) that's belongs to a specific Office (OfficeId) and the status of their Order (OrderId) if they have one.

So in my services layer I could fill a DTO list like (I'm a newbie when it comes to DDD so I'm sure this design isn't great but bear with me):

ObjectDataSource SelectMethod tied to:

public List<CustomerAndOrderDTO> GetCustomersByOffice(int officeId, int startPageIndex, int maxiumRows)
{
   List<CustomerAndOrderDTO> customerAndOrderDTO = null;
   List<Customer> customers = Customer.GetCustomersByOffice(int officeId);
   foreach (Customer customer in customers)
   {
      customerAndOrderDTO.Add(new CustomerAndOrderDTO(customer));
   }
   return customerAndOrderDTO;
}

And this is the DTO class:

    public CustomerAndOrderDTO
    {
       public int OrderId {get; set;}
       public int CustomerId {get; set;}

       public CustomerAndOrderDTO(Customer customer)
       {

          CustomerId = customer.Id;
          Order order = customer.GetOrder();
          OrderId = order.Id;
       }
    }    

Pretty simple code and you get all the flexibility and power of the domain model plus validation and persistence checking while staying in the BLL.

Even if you wanted to ignore the benefits of aggregating your objects in the domain and just combine the data at the stored procedure level like select * from Customers left join Orders on .... left join Office on ... where.... you'd end up writing a million extension methods that implement paging for every possible combination like CustomersOrdersByOfficePaged, CustomersOrdersByAccountDatePaged, etc...

So am I just doing it wrong to begin with? Or does paging at the Service layer make sense? If so, how does one implement it?

Was it helpful?

Solution

So, I'm sure, big picture-wise, this isn't the best architecture, but to implement paging at the service level you can just query your objects from the data access level and then implement the paging logic on your object list at your BLL level like so:

if ((startRowIndex != 0 && startRowIndex != null)
        && (maximumRows != 0 && maximumRows != null))
        {
            if ((audits.Count - startRowIndex) < maximumRows)
            {
                audits = audits.GetRange((int)startRowIndex, audits.Count - (int)startRowIndex);
            }
            else
            {
                audits = audits.GetRange((int)startRowIndex, (int)maximumRows);
            }
        }
        return audits;

The Count method required by the objectDataSource takes the same parameters as your main "Get" method and calls that "Get" method and then returns the .Count.

Again, not the best way but gets the job done.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top