【问题标题】:Implementing custom paging logic in BLL (not in stored procedures) for ObjectDataSource在 BLL(而不是存储过程)中为 ObjectDataSource 实现自定义分页逻辑
【发布时间】:2013-04-25 22:58:51
【问题描述】:

所以我已经使用存储过程在 DAL 中实现了 objectDataSource 自定义分页,就像所有指南和教程中描述的那样,但我意识到通过将自定义分页逻辑放在数据访问层,你会受到限制可以使用您的域模型。

例如,我有一个带有网格视图的网络表单,我想显示属于特定办公室 (OfficeId) 的每个客户 (CustomerId) 以及他们的订单状态 (OrderId)(如果有的话)。

所以在我的服务层中,我可以填写一个 DTO 列表,例如(我是 DDD 的新手,所以我确信这个设计不是很好,但请耐心等待):

ObjectDataSource SelectMethod 绑定到:

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;
}

这是 DTO 类:

    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;
       }
    }    

非常简单的代码,您可以在 BLL 中获得域模型的所有灵活性和强大功能以及验证和持久性检查。

即使您想忽略在域中聚合对象的好处,而只是在存储过程级别组合数据,例如 select * from Customers left join Orders on .... left join Office on ... where。 ...您最终会编写一百万个扩展方法,为各种可能的组合实现分页,例如 CustomersOrdersByOfficePaged、CustomersOrdersByAccountDatePaged 等...

所以我一开始就做错了吗?或者服务层的分页有意义吗?如果有,如何实现?

【问题讨论】:

    标签: asp.net pagination domain-driven-design dto objectdatasource


    【解决方案1】:

    所以,我敢肯定,从整体上看,这不是最好的架构,但要在服务级别实现分页,您只需从数据访问级别查询对象,然后在您的设备上实现分页逻辑BLL 级别的对象列表,如下所示:

    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;
    

    objectDataSource 所需的 Count 方法采用与主“Get”方法相同的参数,并调用该“Get”方法,然后返回 .Count。

    同样,这不是最好的方法,但可以完成工作。

    【讨论】:

      【解决方案2】:
      猜你喜欢
      • 2023-03-30
      • 1970-01-01
      • 2015-01-01
      • 1970-01-01
      • 2011-03-28
      • 2021-08-24
      • 1970-01-01
      • 2019-04-03
      • 2013-07-01
      相关资源
      最近更新 更多