DapperLib / DapperAOT

Build time tools in the flavor of Dapper
Other
359 stars 19 forks source link

Handling Relationships, their mappers as well as their configuration #1

Closed TwentyFourMinutes closed 3 years ago

TwentyFourMinutes commented 3 years ago

I have thought of generating the parser/materializers AOT as well for quite some time even more with the rise of Source Generators. However I was never able to figure out how one would implement it. Therefor I am curious on how you would solve this, especially in terms of the configuration.

mgravell commented 3 years ago

Historically, we've just used "split on" for this. We could enforce that the correct number of split indicators (nominal or ordinal) is included, for example:

[Command(...)]
[SplitOn("Id")]
List<(Customer, Order)> GetCustomerOrders(...)

If we're talking about different things, maybe add an example?

TwentyFourMinutes commented 3 years ago

That is almost what I meant, the examples on this website show it pretty good. Here would be a snippet of it:

using (var connection = new SQLiteConnection(connString))
{
    var sql = @"select productid, productname, p.categoryid, categoryname 
                from products p 
                inner join categories c on p.categoryid = c.categoryid";

    var products = await connection.QueryAsync<Product, Category, Product>(sql, (product, category) => {
        product.Category = category; // This is the important part.
        return product;
    }, splitOn: "CategoryId" );

    products.ToList().ForEach(product => Console.WriteLine($"Product: {product.ProductName}, Category: {product.Category.CategoryName}"));

    Console.ReadLine();
}

Parsers defined through the QueryAsync API, would need some place to go to.

mgravell commented 3 years ago

Fun thing; we had to do that at the time because there wasn't good value tuple support at the time, which meant we had to offer this to allow you to return one value. In all honesty, if I was writing that API today (which I sort-of-am, in DapperAOT), I would just so it like I showed above. If you want to do the mapping bit: that's up to you.

TwentyFourMinutes commented 3 years ago

Oh I see, that makes a lot of sense. Thanks for the insights though!