Generic dapper method for lazyloading of property

c# dapper

Question

Im trying to make a generic method for Dapper.Query. Now i have run into trouble. Here is my example

   public IEnumerable<T1> Lazy<T1, T2>(T2 table) where T1 : EntityBase
    {

        using (IDbConnection cn = GetCn())
        {
            cn.Open();

            return cn.Query<T1, T2, T1>("query", (t1, t2) => { t1.???
        }
    }

As you se below, i need to now the properties in t1 to load it with t2. Is this even possible?

This example is not completed, of course the "query" string will be replaced, this is just for demo.

Accepted Answer

Dapper does not include lazy loading, and provides exactly zero support to help achieve lazy loading, because that isn't what it is trying to do - it is a utility, not a framework. However, your example suggests you are talking about horizontally partitioned data, for example, say we had:

select foo.*, bar.*
from foo inner join bar on bar.ParentId = foo.Id
where foo.Category = @category

and suppose for simplicity that both [foo] and [bar] have an Id column (which it handles automatically, although there are ways of specifying the partitioning rules manually). Then this would become:

return cn.Query<Foo, Bar, Foo>(query, args, (foo, bar) => {
    foo.Bar = bar;
    return foo;
});

or something similar. The point is: it materializes the Foo and the Bar separately (based on the partition), then lets you worry about how to combine them.



Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why