4

我正在使用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
4

2 回答 2

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.

于 2017-10-30T15:30:45.997 回答
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。我经常使用这种安排,它对我很有效。

如果您在单个记录集中返回单行,我不知道有什么方法可以做到这一点。

于 2016-10-21T14:16:26.357 回答