ServiceStack AutoQuery - 使用“?Fields =”

时间:2016-03-22 18:04:03

标签: servicestack ormlite-servicestack

我们注意到在版本4.0.55中使用“?Fields =”(在MyGet上预发布)时出现异常。

我们有一个包含三个1:1关系的Employee表 - EmployeeType,Department和Title:

public partial class Employee {
    [PrimaryKey]
    [AutoIncrement]
    public int ID { get; set; }

    [References(typeof(EmployeeType))]
    public int EmployeeTypeID { get; set; }

    [References(typeof(Department))]
    public int DepartmentID { get; set; }

    [References(typeof(Title))]
    public int TitleID { get; set; }
    .
    .
    .
}

public class EmployeeType {
    [PrimaryKey]
    public int ID { get; set; }
    public string Name { get; set; }
}

public class Department {
    [PrimaryKey]
    public int ID { get; set; }
    public string Name { get; set; }

    [Reference]
    public List<Title> Titles { get; set; }
}

public class Title {
    [PrimaryKey]
    public int ID { get; set; }
    [References(typeof(Department))]
    public int DepartmentID { get; set; }
    public string Name { get; set; }
}

4.0.55的最新更新允许在查询字符串上使用?Fields=请求相关的DTO,如下所示:

/employees?fields=id,firstname,lastname,departmentid,department

请注意,请求相关DTO(部门)的“正确”方式是还请求外键字段(在本例中为departmentid)。

我们想知道是否有办法返回所有Employee表字段而只返回选定的相关DTO,因此在测试中我们发现此请求有效:

/employees?fields=department

我们收回所有员工表字段以及相关的部门DTO - 有一件奇怪的事情 - 员工的ID字段填充了员工的 TitleID 值。

在请求中指定外键字段可修复异常:

/employees?fields=id,departmentid,department

但我们失去了所有其他员工字段。

有没有办法获取所有员工字段和选定的相关DTO?

这是我们的AutoQuery DTO:

[Route("/employees", "GET")]
public class FindEmployeesRequest : QueryDb<Employee>,
    IJoin<Employee, EmployeeType>,
    IJoin<Employee, Department>,
    IJoin<Employee, Title> {

    public int? ID { get; set; }
    public int[] IDs { get; set; }
    public string UserID { get; set; }
    public string[] UserIDs { get; set; }
    public int? EmployeeTypeID { get; set; }
    public int[] EmployeeTypeIDs { get; set; }
    public int? DepartmentID { get; set; }
    public int[] DepartmentIDs { get; set; }
    public int? TitleID { get; set; }
    public int[] TitleIDs { get; set; }
    public string LastNameStartsWith { get; set; }
    public DateTime[] DateOfBirthBetween { get; set; }
    public DateTime[] HireDateBetween { get; set; }
    public bool? IsActive { get; set; }

    [QueryDbField(Template = "(MONTH({Field}) = {Value})", Field = "DateOfBirth")]
    public int? BirthMonth { get; set; }

    [QueryDbField(Template = "(DAY({Field}) = {Value})", Field = "DateOfBirth")]
    public int? BirthDay { get; set; }

    [QueryDbField(Template = "(FirstName LIKE {Value} OR LastName LIKE {Value} OR PreferredName LIKE {Value})", ValueFormat = "%{0}%", Field = "ID")]
    public string NameSearch { get; set; }

    [QueryDbField(Template = "(FirstName LIKE {Value} OR LastName LIKE {Value} OR PreferredName LIKE {Value} OR Department.Name LIKE {Value} OR Title.Name LIKE {Value})", ValueFormat = "%{0}%", Field = "ID")]
    public string BasicSearch { get; set; }

    [QueryDbField(Template = "({Field} LIKE {Value})", Field = "EmployeeTypeName", ValueFormat = "%{0}%")]
    public string EmployeeTypeSearch { get; set; }

    [QueryDbField(Template = "({Field} LIKE {Value})", Field = "DepartmentName", ValueFormat = "%{0}%")]
    public string DepartmentSearch { get; set; }

    [QueryDbField(Template = "({Field} LIKE {Value})", Field = "TitleName", ValueFormat = "%{0}%")]
    public string TitleSearch { get; set; }
}

1 个答案:

答案 0 :(得分:0)

添加了对通配符自定义字段列表的支持in this commit,您可以通过添加.*后缀来指定主表或联接表的所有字段,例如:

?fields=id,departmentid,department,employee.*

它基本上作为替代占位符,将其替换为指定表中的所有字段。

此更改现在可从v4.0.55获得,现在为available on MyGet