Displays the database column.

I use Insight.Databaseas our micro-ORM. I wanted to find out if there is a way to accept the following associations of POCO classes and map the results from one row to these objects.

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...
}

Here are the columns that are returned from our stored procedure:

Id
Name

// Should map to Source object.
SourceId
SourceName
SourceDateTime

// Should map to Destination object.
DestinationId
DestinationName
DestinationDateTime
+4
source share
2 answers

This is possible with a little customization on the request side. Not sure if this 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 this code is needed for it to work, but it definitely shows how powerful the query in Insight.Database can be.

+1
source

You can try

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)] , - . , .

, , .

0

Source: https://habr.com/ru/post/1657770/


All Articles