[Architecture Pattern] Repository实做查询功能

[Architecture Pattern] Repository实做查询功能

范例下载

范例程序代码:点此下载sql

问题情景

在系统的BLL与DAL之间,加入Repository Pattern的设计,可以切割BLL与DAL之间的相依性,而且提供系统抽换DAL的能力。但在软件开发的过程当中,套用Repository Pattern最容易遇到的问题就是,如何在Repository中实做「查询」这个功能。像是在下列这个查询订单的页面,系统必需要依照用户输入的查询条件,来从DAL中查询出全部符合条件内容的Order对象集合,而且将这些Order对象逐一呈现给在系统页面给用户浏览。数据库

问题情景01

由于系统页面上的查询条件是可填、可不填,这对应到提供数据的Repository上,就变成Repository必须依照各类查询条件填或不填的各类组合,来提供对应的Method。但这样的查询功能设计,在查询条件少的情景可以正常的开发设计;但在查询条件较多的情景,就会发现为每种查询条件组合创建对应的Method,近乎是一项不可能的任务。(EX:每一个条件内容可填可不填,3个查询条件就须要2^3=8个Method、7个查询条件就须要2^7=128个Method。)网络

public interface IOrderRepository
{
    // Methods
    IEnumerable<Order> GetAllByCondition(string userId, OrderState state, DateTime startDate, DateTime endDate);

    IEnumerable<Order> GetAllByCondition(OrderState state, DateTime startDate, DateTime endDate);

    IEnumerable<Order> GetAllByCondition(string userId, DateTime startDate, DateTime endDate);

    IEnumerable<Order> GetAllByCondition(DateTime startDate, DateTime endDate);

    IEnumerable<Order> GetAllByCondition(string userId, OrderState state);

    IEnumerable<Order> GetAllByCondition(OrderState state);

    IEnumerable<Order> GetAllByCondition(string userId);

    IEnumerable<Order> GetAllByCondition();
}

这时最直觉的作法,会在Repository上加入GetAllBySql这个Method,让系统依照用户输入的查询条件来组合SQL指令,再交由实做Repository的DAL去数据库作查询。this

public interface IOrderRepository
{
    // Methods
    IEnumerable<Order> GetBySql(string sqlCommand, params object[] parameters);
}

Repository加入GetAllBySql的这个设计,的确能够知足用户需求、提供正确信息给用户。但仔细思考Repository加入GetAllBySql的这个设计,是让DAL的职责污染到了BLL,BLL必需要知道DAL所使用的数据表名称、数据库字段才能组合出SQL指令,也就是在程序代码中隐性的让BLL相依于DAL,这大幅下降了BLL的内聚力。而通常来讲只有关系数据库可以剖析SQL指令来提供数据,也就是DAL实做被绑死在关系数据库上,这也就大大下降了BLL的重用性。设计

接着,如下列这个开发情景:「系统的数据源,须要依照网络联机状态来决定使用本地数据库仍是使用外部API」,来思考Repository加入GetAllBySql的这个设计。当外部API不支持SQL指令查询,系统就没法创建外部API的GetAllBySql实做,这也就限制了BLL抽换DAL成为外部API的能力。(感谢91提供范例~^^)code

问题情景02

解决方案

IRepository设计

为了解决Repository实做查询功能的问题,回过头思考通常函式库、Web服务提供查询功能的方式。会发现不少查询功能的设计,会在查询功能中提供全部的查询条件,在这些条件内容中填null表明忽略这个条件、填值表明加入这个条件。对象

遵循这个设计原则,开发人员能够为Repository上加入GetAllByCondition这个Method,接着把每一个查询条件都设计为这个Method的函式参数;最后替不可为null的值类型参数(enum、DateTime...)加上「?」关键词,将这些值类型改成可输入null的Nullable类别。blog

public interface IOrderRepository
{
    // Methods
    IEnumerable<Order> GetAllByCondition(string userId, OrderState? state, DateTime? startDate, DateTime? endDate);
}

IRepository使用

完成GetAllByCondition的设计以后,系统就能够将用户在窗体中所输入的查询条件,对应到GetAllByCondition的每一个函式参数。(窗体中条件内容有填的对应为函式参数内容、窗体中条件内容没填的对应为函式参数null。)开发

// UserId
string userId = null;
if(string.IsNullOrEmpty(this.UserIdTextBox.Text) == false) 
{
    userId = this.UserIdTextBox.Text.Trim();
}

// State
OrderState? state = null;
if (this.StateComboBox.SelectedValue != null)
{
    if (this.StateComboBox.SelectedValue.ToString() != "All")
    {
        state = Enum.Parse(typeof(OrderState), this.StateComboBox.SelectedValue.ToString()) as OrderState?;
    }
}

// StartDate
DateTime? startDate = null;
if(string.IsNullOrEmpty(this.StartDateTextBox.Text) == false)
{
    startDate = DateTime.Parse(this.StartDateTextBox.Text) as DateTime?;
}

// EndDate
DateTime? endDate = null;
if (string.IsNullOrEmpty(this.EndDateTextBox.Text) == false)
{
    endDate = DateTime.Parse(this.EndDateTextBox.Text) as DateTime?;
}

// Query
var orderCollection = _orderRepository.GetAllByCondition(userId, state, startDate, endDate);

// Display
this.OrderGridView.DataSource = orderCollection;
  • 执行范例(All)get

    解决方案01

    解决方案02

  • 执行范例(userId=A123)

    解决方案03

    解决方案04

  • 执行范例(userId=A123, state=Completed)

    解决方案05

    解决方案06

SqlRepository实做

接着设计封装本地数据库的Repository实做,GetAllByCondition函式就能够依照这些函式参数是否为null、不为null的参数内容,来组合查询条件的SQL指令、提交给本地数据库而且回传查询结果。

  • 依照条件内容是否为null,来组合SQL指令的Where条件。

    // CommandText
    command.CommandText = @"SELECT USER_ID, STATE, DATE FROM Orders";
    
    // ConditionText
    var conditionList = new List<string>();
    if (string.IsNullOrEmpty(userId) == false) conditionList.Add("USER_ID = @USER_ID");
    if (state.HasValue == true) conditionList.Add("STATE = @STATE");
    if (startDate.HasValue == true && endDate.HasValue == true) conditionList.Add("Date >= @START_DATE");
    if (startDate.HasValue == true && endDate.HasValue == true) conditionList.Add("Date <= @END_DATE");
    var conditionString = string.Join(" AND ", conditionList);
    if (string.IsNullOrEmpty(conditionString) == false) command.CommandText += " WHERE " + conditionString;
  • 依照条件内容是否为null,来加入Command.Parameters。

    // CommandParameters
    if (string.IsNullOrEmpty(userId) == false) command.Parameters.Add(new SqlParameter("@USER_ID", userId));
    if (state.HasValue == true) command.Parameters.Add(new SqlParameter("@STATE", state.ToString()));
    if (startDate.HasValue == true && endDate.HasValue == true) command.Parameters.Add(new SqlParameter("@START_DATE", startDate.Value));
    if (startDate.HasValue == true && endDate.HasValue == true) command.Parameters.Add(new SqlParameter("@END_DATE", endDate.Value));
  • 执行范例(All)

    解决方案07

    解决方案08

  • 执行范例(userId=A123)

    解决方案09

    解决方案10

  • 执行范例(userId=A123, state=Completed)

    解决方案11

    解决方案12

IRepository查询

完成上列这些步骤以后,也就完成了Repository实做查询功能的开发工做,用户就能在系统页面上填写查询条件,来从系统中查询全部符合条件内容的数据对象集合。

  • 执行范例(All)

    解决方案13

  • 执行范例(userId=A123)

    解决方案14

  • 执行范例(userId=A123, state=Completed)

    解决方案15

后记

Repository实做查询功能的开发工做套用本篇的解决方案,能在BLL中彻底不须要牵扯DAL的信息,只须要单纯传递C#类别来作为查询条件,这部分提升了BLL的内聚力。而GetAllByCondition的设计,由于单纯使用C#类别来传递查询条件,这让DAL实做不会被绑死在特定数据源上,也大幅提升了BLL的重用性。开发人员设计系统时遇到须要Repository实做查询功能的开发工做,参考本篇提供的解决方案应该就能知足大部分的开发需求。

相关文章
相关标签/搜索