Petapoco的多重映射可以处理多个JOIN吗?

Lar*_*rry 8 mapping join dapper petapoco

我有一个列表对象,我用它填充使用Petapoco.

类属性和名称正在加工数据库模式.主类是Issue,它与另外两个类相关,名称和属性也与数据库模式匹配:ConditionSeverityLevel.

public class Issue
{
    public int Id { get; set; } // Primary key

    /* Some properties... */
    public DateTime? CreatedOn { get; set; }
    public string CreatedBy { get; set; }
    public DateTime? ModifiedOn { get; set; }
    public string ModifiedBy { get; set; }

    /* ... */

    // Related source and target conditions
    [PetaPoco.Ignore]
    public Condition SourceCondition { get; set; }

    [PetaPoco.Ignore]
    public Condition TargetCondition { get; set; }

    // Related severity level
    [PetaPoco.Ignore]
    public SeverityLevel CurrentSeverityLevel { get; set; }
}

public class Condition
{
    public int Id { get; set; } // Primary Key
    public string Description  { get; set; }
}

public class SeverityLevel
{
    public int Id { get; set; } // Primary key
    public string Description { get; set; }
    public string HexColorDisplay { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

实际上,当我检索问题列表时,我使用多重映射功能使用单个命令检索问题列表和相关的SeverityLevel:

var Results = Db.Fetch<Issue, SeverityLevel, Issue>(
    (i, sl) => { 
        i.CurrentSeverityLevel = sl;
        return i;
    },
    "SELECT /* ..shortened.. */ FROM Issue " + 
    "LEFT JOIN SeverityLevel ON SeverityLevel.Id = Issue.SeverityLevelId " + 
    "WHERE Issue.Id=@0", issueId);
Run Code Online (Sandbox Code Playgroud)

现在,因为Petapoco似乎没有处理多个JOINS,所以我需要做第二步,将SourceConditionTargetCondition附加到我检索到的每个问题上.

为此,我可以:

  • 在foreach循环中,在Read之后附加Source和Target条件,
  • 或检索整个条件列表,然后使用相同类型的for-each附加到每个Issue.

目前,我正在使用第二种解决方案,因为数据库中有一组有限的条件.

无论如何,这样做对我来说听起来有点沉重,因为它需要几乎与添加JOINED表一样多的查询.

我想知道我能不能做到这样的工作:

var Results = Db.Fetch</* ????? */>(
    /* ???? */
    "SELECT /* ..shortened.. */ FROM Issue " + 
    "LEFT JOIN SeverityLevel ON SeverityLevel.Id = Issue.SeverityLevelId " + 
    "LEFT JOIN Condition Con1 ON Con1.Id = Issue.SourceConditionId " + 
    "LEFT JOIN Condition Con2 ON Con2.Id = Issue.TargetConditionId " + 
    "WHERE Issue.Id=@0", issueId);
Run Code Online (Sandbox Code Playgroud)

尊敬的Petapoco作者,亲爱的Petapoco用户,这是一种处理这个问题的方法吗?

我可以使用Dapper来处理这个问题(如果它可以...),appart我绝对想保留Petapoco用于我的更新/插入操作?

Sch*_*ime 13

这应该可以做到.

var Results = Db.Fetch<Issue, SeverityLevel, Condition, Condition, Issue>(
    (i, sl, c1, c2) => { 
        i.CurrentSeverityLevel = sl;
        i.SourceCondition = c1;
        i.TargetCondition = c2;
        return i;
    },
    "SELECT Issue.*, SeverityLevel.*, Con1.*, Con2.* FROM Issue " + 
    "LEFT JOIN SeverityLevel ON SeverityLevel.Id = Issue.SeverityLevelId " + 
    "LEFT JOIN Condition Con1 ON Con1.Id = Issue.SourceConditionId " + 
    "LEFT JOIN Condition Con2 ON Con2.Id = Issue.TargetConditionId " + 
    "WHERE Issue.Id=@0", issueId);
Run Code Online (Sandbox Code Playgroud)

我没有测试过这个.我也在努力实现自动化.

这是真正重要的是选择了列在同一顺序的参数类型.