簡體   English   中英

Insight.Database列映射到對象

[英]Insight.Database column mapping to object

我正在使用Insight.Database作為我們的微型ORM 我想弄清楚是否有辦法采用以下POCO類關聯並將單行結果映射到這些對象中。

public class Rule
{
    public int Id { get; set; }
    public string Name { get; set; }
    public RuleDetail Source { get; set; }
    public RuleDetail Destination { get; set; }
}

public class RuleDetail
{
    public int Id { get; set; }
    public Name { get; set; }
    public Date DateTime { get; set; }
    // omitted...
}

這是從我們的存儲過程返回的列:

Id
Name

// Should map to Source object.
SourceId
SourceName
SourceDateTime

// Should map to Destination object.
DestinationId
DestinationName
DestinationDateTime

這可以通過查詢端的一些設置來實現。 不確定屬性是否可行。

var returns = Query.Returns(
    new OneToOne<Rule, RuleDetail, RuleDetail>(
        callback: (rule, source, destination) => {
            rule.Source = source;
            rule.Destination = destination;
        },
        columnOverride: new ColumnOverride[] {
            new ColumnOverride<RuleDetail>("SourceId", "Id"),
            new ColumnOverride<RuleDetail>("SourceName", "Name"),
            new ColumnOverride<RuleDetail>("SourceDateTime", "DateTime"),
            new ColumnOverride<RuleDetail>("DestinationId", "Id"),
            new ColumnOverride<RuleDetail>("DestinationName", "Name"),
            new ColumnOverride<RuleDetail>("DestinationDateTime", "DateTime"),
        },
        splitColumns: new Dictionary<Type, string>() {
            { typeof(Rule), "Id" },
            { typeof(RuleDetail), "SourceId" },
            { typeof(RuleDetail), "DestinationId" },
        }
    )
);
return Db.Query(procedure, params, returns);

我不確定是否需要所有代碼來使其工作,但它肯定顯示了Insight.Database中的查詢功能有多強大。

你可以試試

public interface IRepo
{
    [Recordset(1, typeof(RuleDetail), into="Source", IsChild=true)]
    [Recordset(2, typeof(RuleDetail), into="Destination", IsChild=true)] 
    Rule GetFullyPopulatedRuleByIdStoredProcedure(int id);
}

你需要返回三個記錄集 - 第一個[Recordset(0)]是你的規則,另外兩個包含Source和Destination。 我經常使用這種安排,對我來說效果很好。

如果您在單個記錄集中返回單行,我不知道有任何方法可以執行此操作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM