Insight.Database列映射到对象

时间:2016-10-14 13:38:35

标签: c# sql orm insight.database

我使用Insight.Database作为我们的 micro-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

2 个答案:

答案 0 :(得分:1)

This is possible with a little bit of setup on the query side. Not sure if it is possible with attributes.

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

I'm not sure if all of that code is required to make it work, but it definitely shows how powerful the querying in Insight.Database can be.

答案 1 :(得分:0)

你可以尝试

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。我经常使用这种安排,对我来说效果很好。

如果您在单个记录集中返回单行,我不知道有任何方法可以执行此操作。